I am slowly building up the ability to work with Clojure macros. This was my main motivation for learning the language, since I want to learn a new way of thinking.
Here's a simple macro I wrote as I was learning. It doesn't do much. It uses the
built-in &env
form to optionally return the value of an existing name.
(defmacro current-city [] (if (contains? &env (symbol "city")) 'city nil))
=> #'playground.amt/current-city
And when used:
(let [city "london"] (current-city))
=> "london"
(current-city)
=> nil
I may try to use this to access some implicit state set by some earlier macro. At this point I have no idea if this is a 'correct' way of programming. I'm just goofin'.
This blog post by Jay Field helped me tremendously.