3 ms·
If you're not familiar with lisps in general, it might be hard to grok the differences between lisp-macros (as used in Clojure) and "normal" macros you see in o
by capableweb 4y ago
If you're not familiar with lisps in general, it might be hard to grok the differences between lisp-macros (as used in Clojure) and "normal" macros you see in other (non-lisp [sans Elixir I think]) languages.
But, if you are familiar already, and just wanna see examples of neat macros that makes the API nicer than what a function could provide, here are a few:
- https://github.com/clojure/core.async/blob/master/examples/walkthrough.clj https://github.com/clojure/core.async/blob/master/examples/w...
- https://github.com/weavejester/compojure https://github.com/weavejester/compojure
- https://github.com/ptaoussanis/timbre https://github.com/ptaoussanis/timbre
- https://github.com/krisajenkins/yesql https://github.com/krisajenkins/yesql
Furthermore, macros enables APIs like this, that would be impossible to have in JavaScript for example:
(spy :info (* 5 4 3 2 1)) => 120
%> 15-Jun-13 19:19:13 localhost INFO [my-app.core] - (* 5 4 3 2 1) => 120
`spy` here doesn't just print what the `` form is returning, but the form itself too. You wouldn't be able to achieve this without macros, as the evaluation of the `` form would happen before it gets passed to `spy`, so the initial form is already gone. Instead, a macro received the very code you pass into it, so you can print it, inspect it, rewrite it or whatever.