Skip to content

Instantly share code, notes, and snippets.

@yelouafi
Created July 17, 2015 02:38
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/b3bb24d354d8d850d6d6 to your computer and use it in GitHub Desktop.
Save yelouafi/b3bb24d354d8d850d6d6 to your computer and use it in GitHub Desktop.
// merge : (Stream a, Stream a) => Stream a
Stream.prototype.merge = function(s2) {
return this.isEmpty ? s2
: this.isAbort ? this
: this.isCons ? Stream.Cons( this.head, this.tail.merge(s2) )
: ( !s2.isFuture ?
s2.merge(this)
: Stream.Future(
Promise.race([
this.promise.then(s => () => s.merge(s2), Stream.Abort),
s2.promise.then(s => () => s.merge(this), Stream.Abort)
]).then(lazy => lazy())))
}
Stream.seq([1,2,3,4,5], 0, 1000)
.merge( Stream.seq([6,7,8], 500, 1000) )
.log()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment