Last active
August 29, 2015 13:56
-
-
Save ckirkendall/8793712 to your computer and use it in GitHub Desktop.
distilled version of enliven segments as an external lib
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 segments) | |
(defrecord Lens [fetch putback]) | |
(defprotocol Segment | |
(-fetch [seg value]) | |
(-putback [seg value subvalue])) | |
(defn fetch [value seg] | |
(-fetch seg value)) | |
(defn putback [value seg subvalue] | |
(-putback seg value subvalue)) | |
(def base-extend-list [clojure.lang.Keyword | |
#+clj clojure.lang.Symbol | |
#+cljs string | |
#+clj String]) | |
(doseq [t base-extend-list] | |
(extend t | |
Segment {:-fetch #(get %2 %1) | |
:-putback #(assoc %2 %1 %3)})) | |
(extend Lens | |
Segment {:-fetch #((:fetch %1) %1 %2) | |
:-putback #((:putback %1) %1 %2)) | |
(defn fetch-in [obj path] | |
(reduce fetch obj path)) | |
(defn putback-in [obj value [seg & path]] | |
(when (and obj seg) | |
(if (empty? path) | |
(putback obj seg value) | |
(putback obj seg (putback-in (fetch obj seg) value path))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment