Skip to content

Instantly share code, notes, and snippets.

@asolove
Created January 8, 2014 18:53
Show Gist options
  • Save asolove/8322218 to your computer and use it in GitHub Desktop.
Save asolove/8322218 to your computer and use it in GitHub Desktop.
Om cursor protocol idea
(defprotocol ICursor
(value [self] "Get the current value of the cursor")
(transact [self f] "Update the current value of the cursor"))
(extend Atom
ICursor
(value [self]
(deref self))
(transact [self f]
(swap! self f)))
(defrecord PathCursor [parent path]
ICursor
(value [self]
(get-in (value parent) path))
(transact [self f]
(transact parent #(update-in % path f))))
(defn join [parent korks]
(let [path (if (seq? korks) korks [korks])]
(PathCursor. parent path)))
(defrecord RenameCursor [parent renames]
ICursor
(value [self]
(rename (value parent) renames))
(transact [self f]
(transact parent (fn [state]
(rename
(f (rename state renames))
(map-invert renames))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment