Skip to content

Instantly share code, notes, and snippets.

@zane
Created October 8, 2014 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zane/00655f136470458e21e1 to your computer and use it in GitHub Desktop.
Save zane/00655f136470458e21e1 to your computer and use it in GitHub Desktop.
(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