5

I have a vector that looks like this:

["Config" "{}" "Auth" "{}" "Other" "{}"] 

I'd like to take each key value pair and turn it into the following map:

{"Config" "{}", "Auth" "{}", "Other" "{}"} 

How can I do this with Clojure? Is there a built in function that does this?

1
  • Just got an answer already, (apply assoc {} ["Config" "{}" "Auth" "{}" "Other" "{}"]). Is there one better? Commented Aug 29, 2013 at 14:33

1 Answer 1

6

Use apply to apply the map constructor of desired type to the vector, ie :

(apply hash-map ["Config" "{}" "Auth" "{}" "Other" "{}"]) 

edit

According to this answer you can get different map types depending on the way you evaluate {}, so use the map constructor suitable to your needs.

edit

Looking at this the different object types returned by literal {} appears to be a bug.

Sign up to request clarification or add additional context in comments.

12 Comments

Thank you, well done! Leaving question open a bit longer, I don't like accepting answers too early in case someone missed something.
"If you want to emulate exactly what {} does, use array-map instead of hash-map." As currently implemented, a literal map {} expression with constant keys and values would also be parsed as hash-map (although confusingly evaluated as an array-map). Note that the key order of the literal {:a 1, :b 2, :c 3} is not respected (in version 1.5).
@A.Webb I was suggested by (type x) result. Why does it give different values then? Due to that confusing evaluation?
@A.Webb you're right, and it is indeed evaluated as array-map. Edited the answer. It there any reason for this behavior?
Try (def m {:a 1 :b 2 :c 3}). Then, (type m) ;=> clojure.lang.PersistentHashMap, but (type (eval m)) ;=> clojure.lang.PersistentArrayMap. And (second m) => [:c 3]. I am not convinced this is intentional.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.