Do you want to know when it is that macros are used extensively? Think about defn
; what's more, do this:
(macroexpand-1 '(defn sample [a] (println a))) ;; (def sample (clojure.core/fn ([a] (println a))))
Did you know that defn
is a macro in clojure.core
that creates a function and binds it to a var
in the current namespace?
Clojure is filled with macros; if you want some samples, you can look at Clojure core, but what else can you do with macros?
Let's have a look at some interesting libraries:
yesql
: Theyesql
library is a very interesting sample of code generation. It reads SQL code from a SQL file and generates the Clojure functions accordingly. Look for thedefquery
anddefqueries
macros in theyesql
project on GitHub; it can be very enlightening.core.async
: If you are familiar with thego
language andgoroutines
, you would probably like to have that same functionality in the Clojure language. This isn't necessary since you could have provided them yourself! The...