Skip to content

Instantly share code, notes, and snippets.

@yelouafi
Last active August 29, 2015 14:25
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/361691057c71f06d7744 to your computer and use it in GitHub Desktop.
Save yelouafi/361691057c71f06d7744 to your computer and use it in GitHub Desktop.
// span : (Stream a, a -> aBoolean) -> [Stream a, Stream a]
Stream.prototype.span = function(p) {
let s1, s2, futs;
return this.isEmpty || this.isAbort ? [this, this]
: this.isCons ?
(p(this.head) ?
([s1, s2] = this.tail.span(p), [Stream.Cons(this.head, s1), s2])
: [Stream.Empty, this])
: (futs = this.promise.then(s => s.span(p), Stream.Abort),
[
Stream.Future( futs.then( sp => sp[0] ) ),
Stream.Future( futs.then( sp => sp[1] ) )
])
}
// example : span a 1s separated sequence form 1 to 10
var [s1, s2] = Stream.range(1, 10, 0, 1000).span( x => x < 3)
s1.log('< 3')
s2.log('>= 3')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment