-
-
Save zane/00655f136470458e21e1 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 transduce-p | |
[kf xfs] | |
(fn [yield] | |
(let [xfs (p/map-vals (fn [xf] (xf yield)) xfs)] | |
(fn | |
([] (yield)) | |
([result] | |
((reduce comp yield (vals xfs)) | |
result)) | |
([result input] | |
(let [key (kf input) | |
trans (get xfs key)] | |
(when trans | |
(let [result (trans result input)] | |
(if (reduced? result) | |
(deref result) | |
result))))))))) | |
;; (sequence (transduce-p | |
;; even? | |
;; {true (take 3) | |
;; false (partition-all 2)}) | |
;; (range 10)) | |
;; => (0 2 [1 3] 4 [5 7] [9]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment