O'Reilly logo

Living Clojure by Carin Meier

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required