0

I am reading data from a yaml file that results in data like this:

{:test1 (1 2 3)}

I can lookup the key :test1 and get a clojure.lang.LazySeq with the elements 1 2 3. But when I want to use this data in a macro it gets expanded to a function call and not to a quoted list.

For example:

(defmacro mmerge
  [map1 map2]
  `(assoc ~(merge map1 map2) :merged true))

(mmerge {:test1 (1 2 3)} {:test2 (4 5 6)})

This gets expanded to:

(clojure.core/assoc {:test2 (4 5 6), :test1 (1 2 3)} :merged true)

Is there a possibility to somehow get this to work?

Thanks in advance

1
  • 2
    why is this a macro? Commented Nov 12, 2014 at 21:20

1 Answer 1

5

You can achieve the same with a function if you write the map arguments' values as quoted lists:

(defn mmerge*
  [map1 map2]
  (assoc (merge map1 map2) :merged true))

(mmerge* {:test1 '(1 2 3)} {:test2 '(4 5 6)})
;= {:merged true, :test2 (4 5 6), :test1 (1 2 3)}

If you still want a macro, you need to quote the result from the merge operation in the form returned by the macro (or as @fl00r mentioned it is absolutely correct if you just quote the lists :P):

(defmacro mmerge
  [map1 map2]
  `(assoc '~(merge map1 map2) :merged true))

Which results in the following macroexpansion:

(clojure.core/assoc '{:test2 (4 5 6), :test1 (1 2 3)} :merged true)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.