Created
September 10, 2018 21:14
-
-
Save manicolosi/c1d31402d68ebfa737c500ae568e9109 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; 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