Clojure Cookbook
Luke VanderHart
Ryan Neufeld
Beijing Cambridge Farnham Kln Sebastopol Tokyo
Special Upgrade Offer
If you purchased this ebook directly from oreilly.com, you have the following benefits:
DRM-free ebooksuse your ebooks across devices without restrictions or limitations
Multiple formatsuse on your laptop, tablet, or phone
Lifetime access, with free updates
Dropbox syncingyour files, anywhere
If you purchased this ebook from another retailer, you can upgrade your ebook to take advantage of all these benefits for just $4.99. to access your ebook upgrade.
Please note that upgrade offers are not available from sample content.
Preface
The primary goal of this book is to provide mid-length examples ofClojure code that go beyond the basics, with a focus on real-world,everyday applications (as opposed to more conceptual or academicissues).
Unlike many of the other books on Clojure written to date, theorganizing theme of this book is not the language itself, or itsfeatures and capabilities. Instead, it focuses on specific tasks that developers face (regardless of what language theyre using) andshows an example of how to use Clojure to solve each of those specificproblems.
As such, this book is not and cannot be truly comprehensive; there areinfinite possible example problems. However, we do hope wevedocumented some of the more common ones that most programmersencounter frequently, and that by induction readers will be able tolearn some common patterns, approaches, and techniques that will servethem well as they design solutions for their own unique problems.
How This Book Was Written
An important thing you should understand about this book isthat it is, first and foremost, a group effort. It is not authored byone or two people. It isnt even the work of a single, well-definedgroup. Instead, it is the collaborative product of more than 60 of thebest Clojurists from all over the world, from all backgrounds. Theseauthors use Clojure every day on real applications, ranging fromaerospace to social media, banking to robotics, AI research toe-commerce.
As such, you will see a lot of diversity in the recipespresented. Some are quick and to the point. Others are more deliberate, presenting digestible yetpenetrating insights into the philosophy and implementation of certainaspects of Clojure.
We hope that there is something in this book for readers of diverseinterests. We believe that it will be useful not only as a referencefor looking up solutions to specific problems, but also as a worthwhile survey of thevariety and expressivity that Clojure is capable of. As we editedsubmissions, we were astonished by the number of concepts andtechniques that were new to us, and will hopefully be new to ourreaders as well.
Something else that we discovered while writing and editing was howdifficult it was to draw a circumference around what we wanted tocover. Every single recipe is a beautiful, endless fractal, touchingmultiple topics, each of which deserves a recipe, a chapter, or a bookof its own. But each recipe also needs to stand on its own. Each oneshould provide some useful nugget of information that readers canunderstand and take away with them.
We sincerely hope that we have balanced these goals appropriately, andthat you find this book useful without being tedious, and insightfulwithout being pedantic.
Audience
Anyone who uses Clojure will, we hope, be able to get something out ofthis book. There are a lot of recipes on truly basic things thatbeginners will find useful, but there are also many recipes onmore specialized topics that advanced users should find useful forgetting a head start on implementation.
That said, if youre completely new to Clojure, this probably isntthe book to start withat least, not by itself. It covers a greatmany useful topics, but not as methodically or as thoroughly as a goodintroductory text. See the following section for a listof general Clojure books you may find useful as prior or supplementaltexts.
Other Resources
One thing that this book is not, and could never be, iscomplete. There is too much to cover, and by presenting information ina task-oriented recipe format we have inherently precluded ourselvesfrom methodical, narrative explanation of the features andcapabilities of the whole language.
For a more linear, thorough explanation of Clojure and its features,we recommend one of the following books:
- Clojure Programming (OReilly, 2012), by Chas Emerick, Brian Carper, and Christophe Grand. A good, comprehensive, general-purpose Clojure book focusing on the language and common tasks, oriented toward beginner Clojure programmers.
- Programming Clojure , 2nd ed. (Pragmatic Bookshelf, 2012), by Stuart Halloway and Aaron Bedra. The first book on Clojure, this is a clear, comprehensive introductory tutorial on the Clojure language.
- Practical Clojure (Apress, 2010), by Luke VanderHart and Stuart Sierra. This is a terse, no-nonsense explanation of what Clojure is and what its features do.
- The Joy of Clojure (Manning, 2011), by Michael Fogus and Chris Houser. This is a slightly more advanced text that really digs into the themes and philosophies of Clojure.
- ClojureScript: Up and Running (OReilly, 2012), by Stuart Sierra and Luke VanderHart. While Clojure Cookbook and the other Clojure books listed here focus mainly or entirely on Clojure itself, ClojureScript (a dialect of Clojure that compiles to JavaScript) has gained considerable uptake. This book introduces ClojureScript and how to get started with it, and covers the similarities and differences between ClojureScript and Clojure.
Finally, you should look at the source code for this book itself,which is freely available on GitHub. The selectionof recipes available online is larger than that in the print version,and we are still accepting pull requests for new recipes that mightsomeday make it into a future edition of this book.
Structure
The chapters in this book are for the most part groupings of recipesby theme, rather than strictly categorical. It is entirely possible fora recipe to be applicable to more than one chapterin these cases,we have simply tried to place it where we think the majority ofreaders will likely look first.
A recipe consists of three primary parts and one secondary: problem,solution, discussion, and see also. A recipes problem statement lays out a task or obstacle to be overcome. Its solution tackles the problemhead-on, illustrating a particular technique or library thateffectively accomplishes the task. The discussion rounds everythingout, exploring the solution and any caveats that may come with it.Finally, we tie off each recipe with a see also section, pointingyou, the reader, to any additional resources or recipes that willassist you in enacting the described solution.
Chapter Listing
The book is composed of the following chapters:
- , cover Clojuresbuilt-in primitive and composite data structures, and explain manycommon (and less common) ways one might want to use them.
- , is a grab bag of useful topics that aregenerally applicable in many different application areas and projectdomains, from Clojure features such as Protocols to alternateprogramming paradigms such as logic programming with