In clojure, I can destructure a map like this:
(let [{:keys [key1 key2]} {:key1 1 :key2 2}]
...)
which is similar to CoffeeScript’s method:
{key1, key2} = {key1: 1, key2: 2}
CoffeeScript can also do this:
a = 1
b = 2
obj = {a, b} // just like writing {a: a, b: b}
Is there a shortcut like this in Clojure?
It’s not provided, but can be implemented with a fairly simple macro:
(defmacro rmap [& ks] `(let [keys# (quote ~ks) keys# (map keyword keys#) vals# (list ~@ks)] (zipmap keys# vals#)))user=> (def x 1) #'user/x user=> (def y 2) #'user/y user=> (def z 3) #'user/z user=> (rmap x y z) {:z 3, :y 2, :x 1}