Skip to content

Instantly share code, notes, and snippets.

@zehnpaard
Last active September 20, 2016 10:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zehnpaard/a50fc859ede4d9399dc726bcc26bf6cb to your computer and use it in GitHub Desktop.
Save zehnpaard/a50fc859ede4d9399dc726bcc26bf6cb to your computer and use it in GitHub Desktop.
Transducing Reductions?
(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