Skip to content

Instantly share code, notes, and snippets.

@gerow
Last active December 19, 2015 02:09
Show Gist options
  • Save gerow/5881058 to your computer and use it in GitHub Desktop.
Save gerow/5881058 to your computer and use it in GitHub Desktop.
Looking for what this function might be called. I would call it a function that maps a list to its partial reductions.
(defn map-partial-reductions
"Given a reduce function, an initial value for the reduce, and a list generate a list of partial reductions as it goes"
[f memo l]
(reduce #(if (nil? (last %1))
(conj %1 (f memo %2))
(conj %1 (f (last %1) %2))) [] l))
; So, for the following
(map-partial-reductions + 0 '(1 2 3 4 5))
; => [1 3 6 10 15]
; so the first value is 1, the second is 1 + 2, the third is 1 + 2 + 3, and so on
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment