Chapter 8. The Power of Macros

You are growing strong with Clojure. Now that you are comfortable, we are returning to the language itself to take a look at an advanced language feature called macros. The name might be strange, but it is based on simplicity. Harnessing this simplicity gives it great power.

In fact, you already learned the secret back in Chapter 1code is data! It is contained in the simple sentence.

With this knowledge at the forefront of our minds, let’s step forward and explore Clojure macros.

Exploring Macros

Macros are powerful. Macros are awesome. What exactly are they? In practical terms, they are a way to do meta-programming in Clojure.

Note

Meta-programming comes from ability of languages to treat their programs as data. This allows a program to modify another program, or even its own program.

What can you do with this special ability? In practical terms, macros give you several advantages. They enable you to:

  • Create and implement your own language features yourself.

  • Reduce the lines of code needed for a program and make it more concise and readable.

  • Encapsulate patterns and repetitive code and replace it with a simple macro call.

Want to add a new feature to the language to help make your life easier? In many languages, you are unable to do this yourself. To get a new language feature added, you must request or submit the change. Then the change has to be evaluated. After the evaluation, it needs to be tested. Finally, it can be used with a new ...

Get Living 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.