Skip to content

Instantly share code, notes, and snippets.

@nahuel
Created May 9, 2014 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nahuel/a34a9fe967c035a3d069 to your computer and use it in GitHub Desktop.
Save nahuel/a34a9fe967c035a3d069 to your computer and use it in GitHub Desktop.
improving as-> macro by supporting destructuring
;; The as-> macro doesn't work with destructuring. This is invalid code:
(-> [1 2]
(as-> [a & b]
[a (inc b)]
[(inc a) b]))
;; because it is expanded to:
(let [[a & b] [1 2]
[a & b] [a (inc b)]
[a & b] [(inc a) b]]
[a & b]) ;; this last expression will not compile
;; but with a little redefinition is possible to make as-> work with
;; destructuring:
(defmacro as->
"Binds name to expr, evaluates the first form in the lexical context
of that binding, then binds name to that result, repeating for each
successive form, returning the result of the last form."
{:added "1.5"}
[expr name & forms]
`(let [~name ~expr
~@(interleave (repeat name) (butlast forms))]
~(last forms)))
;; now the previous example will expand to:
(let [[a & b] [1 2]
[a & b] [a (inc b)]]
[(inc a) b])
;; The following example shows why an as-> destructuring compatible
;; macro can be useful. This code parses a defmulti like parameter list
;; by reusing a destructuring form:
(defmacro defmulti2 [mm-name & opts]
(-> [{} opts]
(as-> [m [e & r :as o]]
(if (string? e)
[(assoc m :docstring e) r]
[m o])
(if (map? e)
[(assoc m :attr-map e :dispatch-fn (first r)) (next r)]
[(assoc m :dispatch-fn e) r])
...
;; Compare with the original defmulti:
(defmacro defmulti [mm-name & options]
(let [docstring (if (string? (first options))
(first options)
nil)
options (if (string? (first options))
(next options)
options)
m (if (map? (first options))
(first options)
{})
options (if (map? (first options))
(next options)
options)
dispatch-fn (first options)
options (next options)
m (if docstring
(assoc m :doc docstring)
m)
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment