Last active
September 20, 2016 10:10
-
-
Save zehnpaard/a50fc859ede4d9399dc726bcc26bf6cb to your computer and use it in GitHub Desktop.
Transducing Reductions?
This file contains hidden or 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 treductions [f] | |
(fn [xf] | |
(let [last-val (volatile! ::none)] | |
(fn | |
([] (xf)) | |
([result] (xf result)) | |
([result input] | |
(if (= ::none @last-val) | |
(vreset! last-val input) | |
(vreset! last-val (f @last-val input))) | |
(xf result @last-val)))))) | |
;;usage | |
(sequence (treductions +) (range 10)) | |
;; output : (0 1 3 6 ...) | |
(sequence (treductions conj) (concat [[]] (range 10))) | |
;; output : ([] [0] [0 1] [0 1 2]...) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment