Skip to content

Instantly share code, notes, and snippets.

@cpetzold
Created March 14, 2014 23:23
Show Gist options
  • Save cpetzold/9559137 to your computer and use it in GitHub Desktop.
Save cpetzold/9559137 to your computer and use it in GitHub Desktop.
(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