Last active
February 1, 2018 12:39
-
-
Save sundarj/60538deab9a348c7a05043f355aa66c4 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
. |
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 at [k xform] | |
(fn [rf] | |
(fn | |
([] (rf)) | |
([result] (rf result)) | |
([result input] | |
(rf result | |
(let [f (partial sequence xform) | |
map-entry? (map-entry? input) | |
associative? (associative? input)] | |
(if-not (or map-entry? associative?) | |
input | |
(update input (if map-entry? 1 k) f)))))))) | |
(defn at-in [[k & ks] xform] | |
(if ks | |
(at k (at-in ks xform)) | |
(at k xform))) | |
(transduce (at :a (map inc)) conj {:a [1 2 3]}) | |
(transduce (at-in [:a] (map inc)) conj {:a [1 2 3]}) | |
(transduce (at-in [:a :b] (map inc)) conj {:a {:b [1 2 3]}}) | |
(transduce (comp (filter map?) | |
(at :a (filter identity))) | |
conj | |
[1 2 {:a [nil nil 1]}]) |
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-xf | |
([k] | |
(map (fn [m] (get m k)))) | |
([k not-found] | |
(map (fn [m] (get m k not-found))))) | |
(defn assoc-xf [k v & kvs] | |
(map (fn [m] (apply assoc m k v kvs)))) | |
(defn update-xf [k f & last-args] | |
(map (fn [m] (apply update m k f last-args)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment