19
votes

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]))
2
Generally speaking I don't think its a good idea to consider "position" in case of a map data structure - Ankur
In 2013, is this considered an implementation detail or a promise? - ToBeReplaced

2 Answers

20
votes

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.

15
votes

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).