Created
March 3, 2016 15:24
-
-
Save aeriksson/2ca17d3586e6131a484c to your computer and use it in GitHub Desktop.
Basic Clojure implementation of cursors (pointers into atoms)
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
(ns cursor | |
(:import [clojure.lang IAtom IDeref])) | |
(defn cursor [the-atom path] | |
(reify | |
IAtom | |
(swap [this f] | |
(swap! the-atom update-in path f)) | |
(swap [this f arg] | |
(swap! the-atom update-in path f arg)) | |
(swap [this f arg1 arg2] | |
(swap! the-atom update-in path f arg1 arg2)) | |
(swap [this f x y args] | |
(apply swap! the-atom update-in path f x y args)) | |
(reset [this newval] | |
(get-in (swap! the-atom assoc-in path newval) path)) | |
(compareAndSet [this oldv newv] | |
(if (= (get-in @the-atom path) oldv) | |
(swap! the-atom assoc-in path newv))) | |
IDeref | |
(deref [this] (get-in @the-atom path)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment