Created
March 14, 2014 23:23
-
-
Save ConnerPetzold/9559137 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(deftype Cursor [a ks meta validator] | |
IEquiv | |
(-equiv [o other] (identical? o other)) | |
IDeref | |
(-deref [this] (get-in @a ks)) | |
IReset | |
(-reset! [this new-value] | |
(when-not (nil? validator) | |
(assert (validator new-value) "Validator rejected reference state")) | |
(-swap! a assoc-in ks new-value)) | |
ISwap | |
(-swap! [this f] | |
(-reset! this (f (get-in @a ks)))) | |
(-swap! [this f x] | |
(-reset! this (f (get-in @a ks) x))) | |
(-swap! [this f x y] | |
(-reset! this (f (get-in @a ks) x y))) | |
(-swap! [this f x y more] | |
(-reset! this (apply f (get-in @a ks) x y more))) | |
IMeta | |
(-meta [_] meta) | |
IPrintWithWriter | |
(-pr-writer [this writer opts] | |
(-write writer "#<Cursor: ") | |
(pr-writer a writer opts) | |
(-write writer ">")) | |
IHash | |
(-hash [this] (goog/getUid this))) | |
(defn cursor | |
"Wraps an atom at a ks, providing an atom interface at that ks." | |
([a ks] (Cursor. a ks nil nil)) | |
([a ks & {:keys [meta validator]}] (Cursor. a ks meta validator))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment