Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing a destructuring map for later use

I've been trying to get this to work with quote, quote-splicing, eval, and whatever else I can think of, but no luck so far. I understand why it doesn't work - it's being seen as a map, and it's trying to eval a, b, and c - just not how to get around it.

(def destructor {a :a b :b c :c})
; CompilerException java.lang.RuntimeException: Unable to resolve symbol: a in this context, compiling:(:1:15)

(let [destructor my-map]
  'etc)

I have a rather involved destructuring map that I'm considering using several times, so it seemed a good idea to tuck it away somewhere. Maybe there are better ways to go about it?

like image 682
Gary Fixler Avatar asked May 14 '14 02:05

Gary Fixler


1 Answers

Good idea, but you can't do it in quite this way, because the thing you want to store is not really a value, so you can't store it in a var.

Instead, you can define a macro that includes this in its expansion:

(defmacro with-abc [abc & body]
  `(let [~'{:keys [a b c]} ~abc]
     ~@body))

(with-abc foo
  (...use a, b, and c...))
like image 74
amalloy Avatar answered Sep 28 '22 06:09

amalloy