Last active
August 29, 2015 14:03
-
-
Save ericnormand/3340346ecefea234ff6a to your computer and use it in GitHub Desktop.
Clojure Lens Implementation
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
(defn lens [getter setter] | |
(fn [fmap vf in] | |
(fmap (partial setter in) | |
(vf (getter in))))) | |
(defn lupdate [lens f in] | |
(lens (fn [f a] (f a)) | |
f | |
in)) | |
(defn lset [lens in v] | |
(lupdate lens in (constantly v))) | |
(defn lget [lens in] | |
(lens (fn [_ a] a) | |
identity | |
in)) | |
(deftype Person [name salary address]) | |
(deftype Address [street zip]) | |
(def lname (lens #(.name %) (fn [p n] (Person. n (.salary p) (.address p))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Because you explicitely pass fmap I prefer your impl to @ctford's one.
I think line 12 should be (lupdate lens (constantly v) in))