Created
May 12, 2013 21:19
-
-
Save galdolber/5564961 to your computer and use it in GitHub Desktop.
Creates a clojure bimap from an map atom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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