Created
March 25, 2020 08:42
-
-
Save bsless/c3c7e3a2293738634b3ec67bbc1ad162 to your computer and use it in GitHub Desktop.
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 get-lens [k] (fn [m] (get m k))) | |
(defn get-in-lens | |
[ks] | |
(reduce | |
(fn [f k] | |
(fn [m] | |
(let [lens (get-lens k)] | |
(lens (f m))))) | |
identity | |
ks)) | |
(comment | |
((get-in-lens [:a :b]) {:a {:b 1}})) | |
(defn assoc-lens [k v] (fn [m] (assoc m k v))) | |
(defn update-lens [k f] (fn [m] (update m k f))) | |
(defn assoc-in-lens | |
[[k & ks] v] | |
(if ks | |
(update-lens k (assoc-in-lens ks v)) | |
(assoc-lens k v))) | |
((assoc-in-lens [:a :b :c] 1) {}) | |
(defn update-in-lens | |
[[k & ks] f] | |
(if ks | |
(update-lens k (update-in-lens ks f)) | |
(update-lens k f))) | |
(comment | |
((update-in-lens [:a :b :c] inc) | |
((assoc-in-lens [:a :b :c] 1) {}))) | |
(defn dissoc-lens [k] (fn [m] (dissoc m k))) | |
(defn dissoc-in-lens | |
[ks] | |
(let [k (last ks) | |
ks (butlast ks)] | |
(update-in-lens ks (dissoc-lens k)))) | |
(comment | |
((dissoc-in-lens [:a :b :c]) | |
((assoc-in-lens [:a :b :c] 1) {}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment