Learn how to extend Clojure using macros
Have you ever wanted a language that adapts to your software? Have you ever wanted to eliminate boilerplate? What about extend the syntax without changing the compiler? Those are what macros let you do.
If you've ever wanted to learn Clojure macros, this course is the place to start. This course jumps right into macros with gusto. It starts with the key to understanding macros, takes you through the implementation of 6 progressively more complex macros, and finishes with the three reasons you need macros.
Beginning with Clojure Macros
Want this course?
Love it or leave it guarantee
If you don't learn as much from this course as you thought, just ask for a refund within 30 days and I'll give you your money back.
File downloads for this course are below. Jump down
Beginning with Clojure Macros
The key to understanding macros
Do you know the key to really understanding macros? This lesson explains it. It's simpler than you think.
Writing our first macro
We jump right in and start coding a simple macro, discovering the sigils we need along the way.
How to tackle difficult macros
Before we dive into our more complex macros, we equip ourselves with a powerful technique that focuses our mental powers on the data transformation at the heart of macros.
We need one last tool before we begin our exploration. In order to debug our macros, we need to see what code they are outputting. There are three levels of macroexpansion to learn.
Macro example: when
Clojure's built-in when is a macro that expands into if. Let's write a version ourselves.
Macro example: regex
Let's build an interesting macro to simulate regular expression features of Perl and Ruby: automatically binding locals to the match and groups.
Macro example: with-open
One of the more interesting uses of macros is to make with- macros. These bind locals but also do something with them at the end. Let's build with-open.
Macro example: mf
This is an experimental macro that I came up with. It makes a function that "wraps" a macro so that it can be used at runtime.
How to know when you need a macro
People say you shouldn't write a macro unless you need one. I don't know if that's true (it's controversial in the Lisp world). But how do you know you need one?
A deeper explanation of with-open
Do you understand the pattern of using helper functions within a macro and how it can make your macro more useful and easier to understand?
We tend to code using patterns that we repeat a lot. Learning these patterns can help you level up your macro skills very quickly. This reference sheet contains six annotated patterns that Clojure programmers use all the time.
Have you ever wondered what the symbols in Clojure macros do? I’m talking about `, ~, ~@, etc. This handy reference sheet tells you what they all do, when to use it, and shows examples. Never get lost in a macro again!
When we’re developing macros, it really helps to be able to see what code it will output. Fortunately, Clojure comes with three built-in functions for doing just that. They are so useful for debugging. This reference sheet shows what each one does. It also includes how to access macroexpansion in the three most popular Clojure IDEs.