Skip to content

Instantly share code, notes, and snippets.

@galdolber
Created May 12, 2013 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save galdolber/5564961 to your computer and use it in GitHub Desktop.
Save galdolber/5564961 to your computer and use it in GitHub Desktop.
Creates a clojure bimap from an map atom
(defn bimap [map-atom]
(let [inv-atom (atom {})]
(add-watch map-atom :bimap
(fn [k r old cur]
(let [o (set (keys old))
n (set (keys cur))]
(doseq [removed (difference o n)]
(swap! inv-atom dissoc (old removed)))
(doseq [added (difference n o)]
(swap! inv-atom assoc (cur added) added)))))
inv-atom))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment