Created
January 8, 2014 18:53
-
-
Save asolove/8322218 to your computer and use it in GitHub Desktop.
Om cursor protocol idea
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
(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