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/4a787fb35ce1f0981866 to your computer and use it in GitHub Desktop.
Save yelouafi/4a787fb35ce1f0981866 to your computer and use it in GitHub Desktop.
// aBoolean : Object (JavaScript view of truth)
// filter : (Stream a, a -> aBoolean) => Stream a
Stream.prototype.filter = function(pred) {
return this.isEmpty || this.isAbort ? this
: this.isCons ?
(pred(this.head) ?
Stream.Cons(this.head, this.tail.filter(pred))
: this.tail.filter(pred))
: Stream.Future(
this.promise.then(
s => s.filter(pred),
Stream.Abort))
}
//filters a stream of keypress events to yield only Ctrl keys
Stream.fromDomEvent(document, 'keydown').filter( e => e.keyCode === 17 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment