Writing Code That Writes Code for You

Because Clojure is a dialect of Lisp, it provides a powerful macro system. Macros allow for templating of repetitive blocks of code and for 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 the result of macro execution. 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, macros have legitimate uses, and it’s worth understanding how they work. In this book we use very few macros, so we’ll only touch on ...

Get Web Development with Clojure, 2nd Edition 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.