我知道 Clojure 列表和向量大多是可互换的,并且使用其中一种或另一种与插入顺序conj
或在向量的情况下不需要引用有关。
那为什么
(into {} '( (1 2) (3 4)))
失败,而
(into {} '( [1 2] [3 4]))
成功了吗?
我知道 Clojure 列表和向量大多是可互换的,并且使用其中一种或另一种与插入顺序conj
或在向量的情况下不需要引用有关。
那为什么
(into {} '( (1 2) (3 4)))
失败,而
(into {} '( [1 2] [3 4]))
成功了吗?
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}