Skip to content

Instantly share code, notes, and snippets.

@yelouafi
Created July 17, 2015 02:07
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 yelouafi/c65d1db365dea3f3928b to your computer and use it in GitHub Desktop.
Save yelouafi/c65d1db365dea3f3928b to your computer and use it in GitHub Desktop.
// reduce : ( Stream a, ((b, a) -> a), b ) -> Promise b
Stream.prototype.reduce = function(f, seed) {
return this.isEmpty ? Promise.resolve(seed)
: this.isAbort ? Promise.reject(this.error)
: this.isCons ? this.tail.reduce( f, f(seed, this.head) )
: /* isFuture */ this.promise.then( s => s.reduce(f, seed), Promise.reject )
}
Stream.seq([1,2,3,4,5], 0, 1000)
.reduce((x, y) => x + y, 0)
.then (v => console.log('sum =', v) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment