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]))
Clojure has three built-in map types: array maps, hash maps and sorted maps. Of these, hash maps and sorted maps are unordered, but array maps actually are ordered: this is explained in the data structures section of the official documentation on clojure.org.
Maps are represented as alternating keys and values surrounded by { and } . When Clojure prints a map at the REPL, it will put `,'s between each key/value pair. These are purely used for readability - commas are treated as whitespace in Clojure. Feel free to use them in cases where they help you!
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.
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).
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