Clojure's maps: are keys and vals in same order?

我的未来我决定 提交于 2019-12-18 18:47:15

问题


Is it ok to rely on (= m (zipmap (keys m) (vals m))) in Clojure 1.3+?

Having this behavior makes for slightly more readable code in some situations, eg

(defn replace-keys [smap m]
  (zipmap (replace smap (keys m)) (vals m)))

vs.

(defn replace-keys [smap m]
  (into {} (for [[k v] m] [(smap k k) v]))

回答1:


I can confirm (officially) that the answer to this is yes. The docstrings for keys and vals were updated in Clojure 1.6 to mention this (see http://dev.clojure.org/jira/browse/CLJ-1302).




回答2:


Yes, lots of clojure would break if that changed.

Maps are stored as trees and both functions walk the same tree in the same order.



来源:https://stackoverflow.com/questions/10772384/clojures-maps-are-keys-and-vals-in-same-order

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!