Writing Code That Writes Code for You

Clojure, being a Lisp, also provides a powerful macro system. Macros allow templating repetitive blocks of code and deferring evaluation, among numerous other uses. A macro works by treating code as data instead of evaluating it. This allows us to manipulate the code tree just like any other data structure.

Macros execute before compile time and the compiler sees whatever the result of executing the macro will be. Because of this level of indirection, macros can be difficult to reason about, and thus it’s best not to use them when a function will do the job.

However, there are legitimate uses for macros, and it’s worth understanding how they work. In this book we use very few macros, so we’ll only touch ...

Get Web Development with Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.