Skip to content

Instantly share code, notes, and snippets.

@aeriksson
Created March 3, 2016 15:24
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 aeriksson/2ca17d3586e6131a484c to your computer and use it in GitHub Desktop.
Save aeriksson/2ca17d3586e6131a484c to your computer and use it in GitHub Desktop.
Basic Clojure implementation of cursors (pointers into atoms)
(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