Book Reviews

Mastering Clojure Macros: Write Cleaner, Faster, Smarter Code by Colin Jones

ISBN-13: 978-1941222225
Publisher: Pragmatic Bookshelf
Pages: 100

In Lisp, the code you write is expressed using the language's own data structures. Macros are a metaprogramming system that allows you to hook into the compile phase and manipulate the source code before it's compiled. It's a simple idea, yet it gives rise to amazing capabilities for the programmer. Suddenly you, as an application programmer, has the same power as the language designer. Lack a language feature to make your program more expressive or succinct? Want a special construct to raise the level of your program to that of the problem domain? No problem - with Lisp that power is just a macro away.

Clojure breaks with several Lisp traditions, but it obviously preserves its code as data philosophy that enables macros (of course, the Lisp language family has always been innovative so I'm not sure if Clojure's break from traditions is really that or just another way of being a Lisp). When I started out with Clojure I worried that the language's additional syntax, like special syntax for different data structures and argument lists, would make macros harder. That was never the case. But I soon realized that I don't write as many macros in Clojure as I do in Common Lisp. As a consequence, I never built the macro experience I should; Clojure's macro system differs from the one I know in Common Lisp. That's why I was eagerly awaiting Mastering Clojure Macros once I saw it announced.

Mastering Clojure Macros is a great book. If you program in Clojure you want to read it. That's basically all you need to know, but of course I'm going to elaborate a little so that you know what you get.

The book is part of the Pragmatic exPress series. I just love the concept: short, maximum 100 pages, and focused. That format fits perfectly for Clojure macros. The book assumes a fluent level of Clojure experience from its reader so no time is wasted on basics of the language itself. Instead we jump right into macro land. In the first two chapters we learn all the mechanics we need: syntax quotes, gensyms to prevent symbol captures, namespace rules, and more. It's a solid introduction. I already knew about some Clojure specific constructs like auto-gensyms, but somehow I had missed the special values &form and &env. And right there lies another strength of the book as Colin makes sure to not only explain the semantics but also why and when you want to use these more esoteric features.

The rest of the book puts macros to use in several domains. We get to learn about macros that encapsulate performance optimizations, build domain specific languages and implement new language features like pattern matching. I particularly like that most of the examples are from well-known Clojure libraries like Compojure and often Clojure itself. It's a good pedagogical move since most readers are already familiar with those constructs from the perspective of the user.

To summarize, Mastering Clojure Macros is a great book written by an author that knows the ins and outs of the subject and explains it without much ceremony. You get exactly what the title promises in terms of macro mastery. Sure, the code isn't as groundbreaking as the one you'll find in the novel Let Over Lambda, but again that's not the point. Instead the focus is on communicating practical techniques that you need in your everyday coding tasks. Highly recommended!

Reviewed May 2015