documentcloud/underscore — pull request #871 by tkareine (2012-11-23 08:42:26+00:00)
Title: Add `_.scan` for returning an array of successive reduced values

I don't know any easy way to do this in FP style with Underscore, and without resorting to recursion: gather the value of each iteration, and pass the last processed value to the next iteration. It's easy to do with an explicit loop, but that looks ugly in a program that is otherwise written in FP style.

It's difficult to use `_.reduce` for this, because you need a way to store the initial value and access the last accumulator value:

_.chain(['a', 'b', 'c'])
.reduce(function(acc, cur) { return acc.concat(_.last(acc) + cur) }, [''])
.tail()
.value()

The name of the function is taken from Haskell Prelude:
http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:scanl