2

I'd like to return a hash-map like so:

(fn [foo bar] {:foo foo :bar bar})

Is it possible to do that without repeating the names? Something like how let allows this:

(let [{:keys [foo bar]} args]
   (...))
aaronstacy
  • 6,189
  • 13
  • 59
  • 72

2 Answers2

3

The macro:

(defmacro some-hash-thing [& vals]
  (zipmap (map keyword vals) vals))

And in use:

(let [a 4, b 5]
  (some-hash-thing b a))
;; => {:a 4, :b 5}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • I'd suggest stealing the names from @noisesmith answer: `as-keymap` and `names` are way better. – Shepmaster Jan 25 '14 at 22:58
  • 2
    The syntax-quote unquote is extraneous - they cancel each other out. – A. Webb Jan 26 '14 at 02:04
  • Thanks for pointing that out, @A.Webb. Obviously, I didn't clean it up as much as I could from all my REPL experimenting. I've edited to reflect that. – Shepmaster Jan 26 '14 at 03:48
3
(defmacro as-keymap [& names] `(conj {} ~@(map (juxt keyword symbol) names)))
noisesmith
  • 20,076
  • 2
  • 41
  • 49