Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure: why does (into {} '( (1 2) (3 4))) fail?

Tags:

clojure

I've understood that Clojure lists and vectors are mostly interchangeable and using one or the other has to do with things like insert order in conj or the lack of need to quote in case of vectors.

Why does then

(into {} '( (1 2) (3 4))) 

fail, while

(into {} '( [1 2] [3 4]))

succeeds?

like image 951
Marcus Junius Brutus Avatar asked Jun 22 '13 21:06

Marcus Junius Brutus


1 Answers

It's an artifact of how maps are implemented.

Maps are conceptually treated as sequences of java.util.Map.Entry elements by many Clojure functions. It happens that there is special case code in APersistentMap.java to treat length 2 vectors as a map entry (in APersistentMap.cons), but not for lists.

Arguably there is a reasonable case for giving vectors this special treatment, because they are a convenient form for representing map entry literals in code. So you can write stuff like the following:

(conj {} [:a 1])
=> {:a 1}
like image 90
mikera Avatar answered Sep 28 '22 11:09

mikera