MANNING Amit Rathore IN ACTION Elegant applications on the JVM www.it-ebooks.info Clojure in Action www.it-ebooks.info www.it-ebooks.info Clojure in Action AMIT RATHORE MANNING S HELTER I SLAND www.it-ebooks.info To my parents, my son, and my wonderful wife For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email: orders@manning.com ©2012 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editor: Susan Harkins 20 Baldwin Road Copyeditors: Linda Recktenwald PO Box 261 Typesetter: Dennis Dalinnik Shelter Island, NY 11964 Cover designer: Marija Tudor ISBN: 9781935182597 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 17 16 15 14 13 12 11 www.it-ebooks.info v brief contents P ART 1 G ETTING STARTED 1 1 ■ Introduction to Clojure 3 2 ■ A whirlwind tour 30 3 ■ Building blocks of Clojure 60 4 ■ Polymorphism with multimethods 90 5 ■ Clojure and Java interop 106 6 ■ State and the concurrent world 122 7 ■ Evolving Clojure through macros 148 P ART 2 G ETTING REAL 167 8 ■ Test-driven development and more 169 9 ■ Data storage with Clojure 189 10 ■ Clojure and the web 221 11 ■ Scaling through messaging 240 12 ■ Data processing with Clojure 273 13 ■ More on functional programming 307 14 ■ Protocols, records, and types 339 15 ■ More macros and DSLs 367 www.it-ebooks.info www.it-ebooks.info vii contents preface xiii acknowledgments xvi about this book xviii P ART 1 G ETTING STARTED 1 1 Introduction to Clojure 3 1.1 What is Clojure? 4 Clojure—the reincarnation of Lisp 4 ■ How we got here 5 How this book teaches Clojure 5 1.2 Understanding Clojure syntax 6 XML and parentheses 7 ■ Lists, vectors, and hashes 9 1.3 The sources of Clojure’s power 10 Clojure and Lisp 10 ■ Clojure and functional programming 11 Clojure and the JVM 11 ■ Clojure as a Lisp 11 More advantages of Clojure 18 ■ Clojure as a functional language 18 ■ Clojure as a JVM-based language 23 1.4 Clojure—beyond object orientation 26 1.5 Summary 28 www.it-ebooks.info CONTENTS viii 2 A whirlwind tour 30 2.1 Getting started 30 Installing Clojure 31 ■ The Clojure REPL 31 Hello, world 32 ■ doc and find-doc 33 ■ A few more points on Clojure syntax 34 2.2 Program structure 36 Functions 36 ■ The let form 37 ■ Side effects with do 39 try/catch/finally and throw 40 ■ Reader macros 41 2.3 Program flow 42 Conditionals 43 ■ Functional iteration 45 The threading macros 50 2.4 Clojure data structures 52 nil, truth, and falsehood 52 ■ Chars, strings, and numbers 52 Keywords and symbols 53 ■ Sequences 53 2.5 Summary 58 3 Building blocks of Clojure 60 3.1 Functions 61 Defining functions 61 ■ Calling functions 67 Higher-order functions 67 ■ Anonymous functions 70 Keywords and symbols 71 3.2 Scope 73 Vars and binding 74 ■ The let form revisited 78 Lexical closures 79 3.3 Namespaces 80 ns 80 ■ Working with namespaces 83 3.4 Destructuring 83 Vector bindings 84 ■ Map bindings 86 3.5 Metadata 88 3.6 Summary 89 4 Polymorphism with multimethods 90 4.1 Polymorphism 91 Subtype polymorphism 91 ■ Duck typing 92 4.2 Method dispatch 92 Single and double dispatch 93 ■ The visitor pattern (and simulating double dispatch) 95 ■ Multiple dispatch 96 www.it-ebooks.info CONTENTS ix 4.3 Multimethods 97 Without multimethods 97 ■ Using multimethods 97 Multiple dispatch 99 ■ Ad hoc hierarchies 100 Redis-clojure 103 4.4 Summary 104 5 Clojure and Java interop 106 5.1 Calling Java from Clojure 107 Importing Java classes into Clojure 107 ■ Creating instances and accessing methods and fields 108 ■ memfn 112 bean 113 ■ Arrays 113 ■ Implementing interfaces and extending classes 114 5.2 Compiling Clojure code to Java byte code 115 Example–a tale of two calculators 115 ■ Creating Java classes and interfaces using gen-class and gen-interface 117 5.3 Calling Clojure from Java 120 5.4 Summary 121 6 State and the concurrent world 122 6.1 The problem with state 123 Common problems with shared state 123 The traditional solution 124 6.2 Identities and values 125 Immutable values 126 ■ Objects and time 127 Immutability and concurrency 128 6.3 The Clojure way 129 Requirements for immutability 130 ■ Managed references 131 6.4 Refs 131 Mutating refs 132 ■ Software transactional memory 134 6.5 Agents 136 Mutating agents 136 ■ Working with agents 137 Side effects in STM transactions 139 6.6 Atoms 140 Mutating atoms 141 6.7 Vars 142 6.8 State and its unified access model 143 6.9 Watching for mutation 144 www.it-ebooks.info [...]... Defining the slave 293 ■ ■ www.it-ebooks.info CONTENTS xii Using the master-slave framework 295 Running a job 296 Seeing task errors 298 Rerunning the job 300 ■ ■ 12.3 13 Summary 306 More on functional programming 307 13.1 Using higher-order functions 308 Collecting results of functions 308 Reducing lists of things 310 Filtering lists of things 311 ■ ■ 13.2 Partial application and currying Adapting... Calling all workers 266 Additional features 271 ■ 253 ■ ■ 11.4 12 Summary 272 Data processing with Clojure 273 12.1 The map/reduce paradigm 274 Getting started with map/reduce—counting words 274 Generalizing the map/reduce 276 Parsing logs 279 Analyzing Rails sessions 285 Large-scale data processing 288 ■ ■ 12.2 Master/slave parallelization 289 Defining the job 290 Maintaining status 290 Dispatching... 197 ■ Using Clojure to access HBase www.it-ebooks.info 200 191 CONTENTS 9.3 Redis xi 210 Installing Redis 210 Accessing Redis from Clojure programs 210 A Redis data mapper 212 ■ ■ 9.4 10 Summary 219 Clojure and the web 221 10.1 An HTTP interface from scratch 222 The HTTP engine 222 10.2 Ring 229 Understanding Ring 10.3 11 232 Generating HTML clj-html 236 10.5 ■ Middleware 230 Compojure 232 Using Compojure... of installing and getting started with Clojure It then dives into the most fundamental concept in Clojure that of a first-class function It completes the introduction by addressing program flow and the core data-structures Chapter 3 is about going deeper: it addresses functions in more depth, and then describes the scoping rules of the language You’ll discover that in addition to lexical scoping, Clojure. .. at http:/ /www.manning.com/ClojureinAction Author Online The purchase of Clojure in Action includes free access to a private forum run by Manning Publications where you can make comments about the book, ask technical questions, and receive help from the author and other users You can access and subscribe to the forum at http:/ /www.manning.com/ClojureinAction This page provides information on how to... about when starting to learn a Lisp-like language—the syntax 1.1.3 How this book teaches Clojure The philosophy of this book rests on two main pillars: emphasizing Clojure s first principles and taking a hands-on approach to understanding those principles You’ll see plenty of code examples that illustrate these concepts Programming in Clojure www.it-ebooks.info 6 CHAPTER 1 Introduction to Clojure requires... same time, people hear of Lisp being used for some cutting-edge software systems in various domains: NASA’s Pathfinder mission-planning software, algorithmic trading of hedge funds, airline reservations, data mining, natural language processing, expert systems, bio-informatics, robotics, electronic design automation, and so on Lisp has the reputation of being a dark art; indeed, it has been referred to... things in our world Indeed, Fred Brooks wrote about complexity in a paper as early as 1986 He drew a distinction between essential complexity and accidental complexity Essential complexity is inherent in the problem domain, whereas accidental complexity is introduced by things xiii www.it-ebooks.info xiv PREFACE external to the problem domain For example, in a software project that deals with filing... downloads All code in the book is presented in a fixed-width font like this to separate it from ordinary text Code annotations accompany many of the listings, highlighting important concepts In some cases, numbered bullets link to explanations that follow the listing Please see chapter 2 for instructions on how to download and install Clojure You will find the full code for all the examples in the book available... explain this mysticism by talking about a new Lisp called Clojure This new computer programming language is not only a practical Lisp, but it has added to its effectiveness by embracing the functional paradigm, by incorporating concurrency semantics into its core, and by being hosted on the Java Virtual Machine At the end of this discussion, you won’t be surprised to learn that Clojure is being used in . MANNING Amit Rathore IN ACTION Elegant applications on the JVM www.it-ebooks.info Clojure in Action www.it-ebooks.info www.it-ebooks.info Clojure in Action AMIT. Summary 104 5 Clojure and Java interop 106 5.1 Calling Java from Clojure 107 Importing Java classes into Clojure 107 ■ Creating instances and accessing methods