Programming Clojure, Third Edition
by Alex Miller, with Stuart Halloway, and Aaron Bedra
Version: P1.0 (February 2018)
Copyright 2018 The Pragmatic Programmers, LLC. This book is licensed to the individual who purchased it. We don't copy-protect it because that would limit your ability to use it for your own purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copy with other members of your team, with friends, or via file sharing services. Thanks.
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.
About the Pragmatic Bookshelf
The Pragmatic Bookshelf is an agile publishing company. Were here because we want to improve the lives of developers. We do this by creating timely, practical titles, written by programmers for programmers.
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.
Our ebooks do not contain any Digital Restrictions Management, and have always been DRM-free. We pioneered the beta book concept, where you can purchase and read a book while its still being written, and provide feedback to the author to help make a better book for everyone. Free resources for all purchasers include source code downloads (if applicable), errata and discussion forums, all available on the book's home page at pragprog.com. Were here to make your life easier.
New Book Announcements
Want to keep up on our latest titles and announcements, and occasional special offers? Just create an account on pragprog.com (an email address and a password is all it takes) and select the checkbox to receive newsletters. You can also follow us on twitter as @pragprog.
About Ebook Formats
If you buy directly from pragprog.com, you get ebooks in all available formats for one price. You can synch your ebooks amongst all your devices (including iPhone/iPad, Android, laptops, etc.) via Dropbox. You get free updates for the life of the edition. And, of course, you can always come back and re-download your books when needed. Ebooks bought from the Amazon Kindle store are subject to Amazon's polices. Limitations in Amazon's file format may cause ebooks to display differently on different devices. For more information, please see our FAQ at pragprog.com/frequently-asked-questions/ebooks. To learn more about this book and access the free resources, go to https://pragprog.com/book/shcloj3, the book's homepage.
Thanks for your continued support,
Andy Hunt
The Pragmatic Programmers
The team that produced this book includes: Andy Hunt (Publisher) Janet Furlow (VP of Operations) Brian MacDonald (Managing Editor) Jacquelyn Carter (Supervising Editor) Paula Robertson (Copy Editor) Potomac Indexing, LLC (Indexing) Gilson Graphics (Layout)
For customer support, please contact .
For international rights, please contact .
Table of Contents
Copyright 2018, The Pragmatic Bookshelf.
Early praise for Programming Clojure, Third Edition
Programming Clojure is an inspiration of Clojure knowledge and has furthered my understanding of the nuances of Clojure. One of the new sections includes a step-by-step on building an application that made me want to drop everything and code along.
Nola Stowe |
CTO/Founder, Ruby Geek LLC |
If you are interested in learning the ins and outs of the Clojure language, Programming Clojure will provide you with a valuable resource. The book notonly covers the basics of the language, but also builds on the basics to allowreaders to understand and apply more advanced concepts like spec and macros.
Joy Clark |
Consultant, innoQ Deutschland GmbH |
This book is very effective at teaching Clojures unique take on functional programming and data manipulation. It explains concepts clearly and covers the mechanics of nearly every part of the language, with helpful commentary that goes beyond the code.
Ghadi Shayban |
Engineer, healthfinch |
The third edition of Programming Clojure is an excellent resource for new and old Clojure programmers. It provides a thorough account of the languages rationale and features, including approachable explanations of more recent features like transducers and spec.
Michael Fogleman |
Developer |
Acknowledgments
Many people have contributed to what is good in this book. The problems and errors that remain are ours alone.
Thanks to Rich Hickey for creating the excellent Clojure language and fostering a community around it. Thanks to the awesome team at Cognitect (formerly Relevance) for creating an atmosphere in which good ideas can grow and thrive. Thanks to the Clojure community for using Clojure and turning an idea into a working ecosystem.
Thanks to all the readers and technical reviewers who have suggested improvements across all three editions of the book. Jeff Brown suggested the coin toss problem in .
Thanks to everyone at Pragmatic Bookshelf. Thanks especially to our editor, Jacquelyn Carter, and former editors Michael Swaine and Susannah Pfalzer for focus and advice. Thanks also to Dave Thomas and Andy Hunt.
Thanks to my wife and family for their love, support, and the precious gift of time to create. Alex
Thanks to my wife, Joey, and to my children, Hattie, Harper, Mabel Faire, and Truman. You all make the sun rise. Stuart
Thanks to my wife, Erin, for endless love and encouragement. Aaron
Copyright 2018, The Pragmatic Bookshelf.
Introduction
Clojure is a dynamic programming language for the Java Virtual Machine (JVM) with a compelling combination of features:
Clojure is elegant. Clojures clean, carefuldesign lets you write programs that get right to the essenceof a problem, without a lot of clutter and ceremony.
Clojure is Lisp reloaded. Clojure has the powerinherent in Lisp but is not constrained by the history ofLisp.
Clojure is a functional language. Data structuresare immutable, and most functions are free from sideeffects. This makes it easier to write correct programs and tocompose large programs from smaller ones.
Clojure simplifies concurrent programming. Manylanguages build a concurrency model around locking, which isdifficult to use correctly. Clojure provides severalalternatives to locking: software transactional memory,agents, atoms, and dynamic variables.
Clojure embraces Java. Calling from Clojure toJava is direct and fast, with no translation layer.
Unlike many popular dynamic languages, Clojure isfast. Clojure is written to take advantage of theoptimizations possible on modern JVMs.