The Joy of Clojure: Thinking the Clojure Way
Michael Fogus & Chris Houser
Copyright
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. 180 Broad St. Suite 1323 Stamford, CT 06901 Email:
orders@manning.com2011 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 Mannings 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 |
180 Broad St. | Copyeditor: | Benjamin Berg |
Suite 1323 | Typesetter: | Dottie Marsico |
Stamford, CT 06901 | Cover designer: | Marija Tudor |
ISBN 978-1-935182-64-1
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 MAL 16 15 14 13 12 11
Dedication
The Joy of Clojure ... wherein we teach you the joys of Clojure programming.
We have written this book for you, the adventurous programmer with prior experience in Java or functional programming languagesespecially Lisp. Our aim is to enhance your programming knowledge in general, and your understanding of Clojure in particular, by exploring the philosophy, motivations, and semantics of the Clojure programming language.
Brief Table of Contents
Table of Contents
Foreword
The authors of this book have taken an ambitious and aggressive approach to teaching Clojure. You know how everyone loves to say they teach using the drinking from a fire hydrant method? Well, at times it feels like these guys are trying to shove that fire hydrant right up... lets just say its a place where you dont normally put a fire hydrant. This isnt intended as a first book on programming, and it may not be an ideal first book on Clojure either. The authors assume youre fearless and, importantly, equipped with a search engine. Youll want to have Google handy as you go through the examples. The authors blaze through many of the classics of both functional programming and industry programming in a whirlwind tour of Clojure that feels at times more like a class-five tropical storm. Youll learn fast!
Our industry, the global programming community, is fashion-driven to a degree that would embarrass haute couture designers from New York to Paris. Were slaves to fashion. Fashion dictates the programming languages people study in school, the languages employers hire for, the languages that get to be in books on shelves. A naive outsider might wonder if the quality of a language matters a little, just a teeny bit at least, but in the real world fashion trumps all.
So nobody could be more surprised than I that a Lisp dialect has suddenly become fashionable again. Clojure has only been out for three years, but its gaining momentum at a rate that we havent seen in a new language in decades. And it doesnt even have a killer app yet, in the way that browsers pushed JavaScript into the spotlight, or Rails propelled Ruby. Or maybe the killer app for Clojure is the JVM itself. Everyones fed up with the Java language, but understandably we dont want to abandon our investment in the Java Virtual Machine and its capabilities: the libraries, the configuration, the monitoring, and all the other entirely valid reasons we still use it.
For those of us using the JVM or .NET, Clojure feels like a minor miracle. Its an astoundingly high-quality language, surein fact, Im beginning to think its the best Ive ever seenyet somehow it has still managed to be fashionable. Thats quite a trick. It gives me renewed hope for the overall future of productivity in our industry. We might just dig ourselves out of this hole were in and get back to where every project feels like a legacy-free startup, just like it was in the early days of Java.
There are still open questions about Clojures suitability for production shops, especially around the toolchain. Thats normal and expected for a new language. But Clojure shows so much promise, such beautiful and practical design principles, that everyone seems to be jumping in with both feet anyway. I certainly am. I havent had this much fun with a new language since Java arrived on the scene 15 years ago. There have been plenty of pretenders to the JVM throne, languages that promised to take the Java platform to unprecedented new levels. But until now, none of them had the right mix of expressiveness, industrial strength, performance, and just plain fun.
I think maybe its the fun part thats helped make Clojure fashionable.
In some sense, all this was inevitable, I think. Lispthe notion of writing your code directly in tree formis an idea thats discovered time and again. People have tried all sorts of crazy alternatives, writing code in XML or in opaque binary formats or using cumbersome code generators. But their artificial Byzantine empires always fall into disrepair or crush themselves into collapse while Lisp, the road that wanders through time, remains simple, elegant, and pure. All we needed to get back on that road was a modern approach, and Rich Hickey has given it to us in Clojure.
The Joy of Clojure just might help make Clojure as fun for you as it is for us.
STEVE YEGGE
GOOGLE
steve-yegge.blogspot.com
Preface
To fully appreciate Clojure, we hearken back to Paul Grahams essay Beating the Averages, an interesting look at the inner workings of his company Viaweb during the years prior to being bought by Yahoo! Inc. in 1998. Though interesting as survey of startup culture, the truly memorable part of the essay was the description of how Viaweb used the programming language Lisp as an advantage over its competition. How could a programming language more than 50 years old provide any market advantage over Viawebs competitors, who were surely using modern enterprise technologies? Without repeating the exact terms of the essay, Graham makes a compelling case for the capability of Lisp to facilitate a more agile programming environment.
Clojure is a dialect of Lisp directly supporting concurrent software development using functional programming techniques, and like the Lisp described in Beating the Averages, provides an environment conducive to agility. Clojure fosters agility in ways that many popular programming languages cant. Many programming languages are bewitched with most or all of the following:
- Verbosity
- Unavoidable boilerplate