Skip to content

Commit

Permalink
[new] Add invert-map!
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Sep 23, 2024
1 parent 86c840a commit 920bd07
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/taoensso/encore.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2509,6 +2509,17 @@
(persistent! (reduce-kv (fn [m k v] (assoc! m v k)) (transient {}) m))
(do (reduce-kv (fn [m k v] (assoc m v k)) {} m)))))

(defn invert-map!
"Like `invert-map` but throws on non-unique vals."
{:added "Encore vX.Y.Z (YYYY-MM-DD)"}
[m]
(when-let [im (invert-map m)]
(if (= (count im) (count m)) ; 1-to-1
im
(throw
(ex-info "[encore/invert-map!] Non-unique map vals"
{:given (typed-val m)})))))

(defn map-keys
"Returns given ?map with (key-fn <key>) keys."
[key-fn m]
Expand Down

0 comments on commit 920bd07

Please sign in to comment.