map-invert

clojure.set

  • (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:
Used in 0 other vars

Comments top

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