map-invert

added
1.0

ns
clojure.set

type
function

(map-invert m)

Returns the map with the vals mapped to the keys.

                ;; 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.
            
                ;; The inverted map of an empty map is also an empty map.
user=> (map-invert {})
{}

;; Using complex values (which serve as keys in the inverted map) is possible.
user=> ((map-invert {:a {:c 5}}) {:c 5})
:a