Programming Clojure
Second Edition
by Stuart Halloway, Aaron Bedra
Version: P1.0 (April 2012)
Copyright 2012 The Pragmatic Programmers, LLC. This book is licensed tothe individual who purchased it. We don't copy-protect itbecause that would limit your ability to use it for yourown purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copywith other members of your team, with friends, or via file sharing services. Thanks.
Dave & Andy.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.
Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com.
In loving memory of my father and mentor, Craig Bedra, who taught me the value of learning by exploration and that there is no such thing as magic.Aaron
Table of Contents
Copyright 2012, The Pragmatic Bookshelf.
What Readers Are Saying About Programming Clojure, Second Edition
Clojure is one of the most interesting languages out there right now, and the best way of learning Clojure just got better. The secondedition of Programming Clojure adds up-to-date information, plenty ofpractical examples, and a ton of useful tips on how to learn, work with,and succeed with Clojure.
Ola Bini |
Creator of Ioke language, developer, ThoughtWorks |
Intimidated by Clojure? You wont be after you read this book. Written in a clear and enjoyable style, it teaches the language one small piece at a time in a very accessible way.
Tim Berglund |
Founder and Principal, August Technology Group |
The authors have charted the smoothest path yet to Clojure fluency with this well-organized and easy-to-read book. They have a knack for creating simple
and effective examples that demonstrate how the languages unique features
fit together.
Chris Houser |
Primary Clojure contributor and library author |
Clojure is a beautiful, elegant, and very powerful language on the JVM. Its
like a cathedral: you could wander into it, but youd prefer the company of a knowledgeable guide who can give you their perspectives, to help you grasp and appreciate the architecture and the art. In this book you can enjoy and benefit from the company of not one, but two seasoned developers who have the depth of knowledge and the perspective you need.
Dr. Venkat Subramaniam |
Award-winning author and founder, Agile Developer, Inc. |
Foreword for the Second Edition
A lot has changed since the first edition of the book. Yes, the language has had some enhancements, such as protocols and records. Most significant, though, is that Clojure has seen adoption across a wide variety of domains. People are building start-ups, analyzing large data sets, and doing communications, financial, web, and database work in Clojure. A large and supportive community has grown up around Clojure and, with it, a ton of libraries. These libraries are particularly exciting, not just in the facilities they provide. The best of them embrace the Clojure approach and mechanisms and, in doing so, reach new levels of simplicity and interoperability.
In this second edition, Stuart and Aaron make sure to cover the language enhancements and include a taste of what its like to leverage some of the community libraries, while taking care to convey the concepts that make it all work. The book remains an exhilarating introduction to Clojure, and I hope it inspires you to join the community and, eventually, contribute to the library ecosystem.
Rich Hickey
Creator of Clojure
Copyright 2012, The Pragmatic Bookshelf.
Foreword for the First Edition
We are drowning in complexity. Much of it is incidentalarising from the way we are solving problems, instead of the problems themselves. Object-oriented programming seems easy, but the programs it yields can often be complex webs of interconnected mutable objects. A single method call on a single object can cause a cascade of change throughout the object graph. Understanding what is going to happen when, how things got into the state they did, and how to get them back into that state in order to try to fix a bug are all very complex. Add concurrency to the mix, and it can quickly become unmanageable. We throw mock objects and test suites at our programs but too often failto question our tools and programming models.
Functional programming offers an alternative. By emphasizing pure functions that take and return immutable values, it makes side effects the exception rather than the norm. This is only going to become more important as we face increasing concurrency in multicore architectures. Clojure is designed to make functional programming approachable and practical for commercial software developers. It recognizes the need for running on trusted infrastructure like the JVM and supporting existing customer investments in Java frameworks and libraries, as well as the immense practicality of doing so.
What is so thrilling about Stuarts book is the extent to which he gets Clojure, because the language is targeted to professional developers just like himself. He clearly has enough experience of the pain points Clojure addresses, as well as an appreciation of its pragmatic approach. This book is an enthusiastic tour of the key features of Clojure, well grounded in practical applications, with gentle introductions to what might be new concepts. I hope it inspires you to write software in Clojure that you can look back at and say, Not only does this do the job, but it does so in a robust and simple way, and writing it was fun too!
Rich Hickey
Creator of Clojure
Copyright 2012, The Pragmatic Bookshelf.
Acknowledgments
Many people have contributed to what is good in this book. The problems and errors that remain are ours alone.
Thanks to the awesome team at Relevance and Clojure/core for creating an atmosphere in which good ideas can grow and thrive.
Thanks to the kind folks on the Clojure mailing list for all their help and encouragement.
Thanks to everyone at the Pragmatic Bookshelf. Thanks especially to our editor, Michael Swaine, for good advice delivered on a very aggressive schedule. Thanks to Dave Thomas and Andy Hunt for creating a fun platform for writing technical books and for betting on the passions of their authors.
Thanks to all the people who posted suggestions on the books errata page.
Thanks to our technical reviewers for all your comments and helpful suggestions, including Kevin Beam, Ola Bini, Sean Corfield, Fred Daoud, Steven Huwig, Tibor Simic, David Sletten, Venkat Subramaniam, and Stefan Turalski.