Skip to content

Instantly share code, notes, and snippets.

@manicolosi
Created September 10, 2018 21:14
Show Gist options
  • Select an option

  • Save manicolosi/c1d31402d68ebfa737c500ae568e9109 to your computer and use it in GitHub Desktop.

Select an option

Save manicolosi/c1d31402d68ebfa737c500ae568e9109 to your computer and use it in GitHub Desktop.
;; Produces maps from let-like bindings. And yes: we are not proud of this.
(defmacro bindmap [bindings]
`(let [~@bindings]
~(reduce
(fn [m# [binding-form# _#]]
(let [binding-symbol# (cond
(map? binding-form#)
(:as binding-form#)
(symbol? binding-form#)
binding-form#
(vector? binding-form#)
(let [[maybe-as sym] (take-last 2 binding-form#)]
(when (= maybe-as :as)
sym)))]
(if binding-symbol#
(assoc m#
(keyword binding-symbol#)
binding-symbol#)
m#)))
{}
(partition-all 2 bindings))))
(bindmap [{:keys [foo] :as bar} {:foo :bvar}
[_ crap :as b] [foo 10]
c (+ 2 3 crap)])
; => {:bar {:foo :bvar}, :b [:bvar 10], :c 15}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment