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?
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With