问题
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