You're viewing version 1.2.0 of map-invert. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16



  • (map-invert m)
Returns the map with the vals mapped to the keys.

1 Example top

  • ;; Despite being in clojure.set, this has nothing to do with sets. 
    user=> (map-invert {:a 1, :b 2})
    {2 :b, 1 :a}
    ;; If there are duplicate keys, one is chosen:
    user=> (map-invert {:a 1, :b 1})
    {1 :b}
    ;; I suspect it'd be unwise to depend on which key survives the clash.
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/set.clj:106 top

(defn map-invert
  "Returns the map with the vals mapped to the keys."
  {:added "1.0"}
  [m] (reduce (fn [m [k v]] (assoc m v k)) {} m))
Vars in clojure.set/map-invert: assoc defn fn reduce
Used in 0 other vars

Comments top

No comments for map-invert. Log in to add a comment.