This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// anError : Object | |
// mapError : (Stream a, anError -> Stream a) => Stream a | |
Stream.prototype.mapError = function(f) { | |
return this.isEmpty ? this | |
: this.isAbort ? f(this.error) | |
: this.isCons ? Stream.Cons(this.head, this.tail.mapError(f)) | |
: Stream.Future( | |
this.promise.then( | |
s => s.mapError(f), | |
Stream.Abort)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
filter :: (a -> Bool) -> [a] -> [a] | |
filter _pred [] = [] | |
filter pred (x:xs) | |
| pred x = x : filter pred xs | |
| otherwise = filter pred xs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
span :: (a -> Bool) -> [a] -> ([a],[a]) | |
span _ this@[] = (this, this) | |
span p this@(x:xs) | |
| p x = let (ys,zs) = span p xs in (x:ys,zs) | |
| otherwise = ([],this) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// groupBy : (Stream a, (a, a) -> aBoolean) => Stream (Stream a) | |
Stream.prototype.groupBy = function(p) { | |
var s1, s2; | |
return this.isEmpty || this.isAbort ? this | |
: this.isCons ? | |
( [s1, s2] = this.tail.span( x => p(this.head, x) ), | |
Stream.Cons( | |
Stream.Cons(this.head, s1), | |
s2.groupBy(p))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// group : Stream a => Stream (Stream a) | |
Stream.prototype.group = function() { | |
return this.groupBy( (x1, x2) => x1 === x2 ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// length : Stream a -> Promise Number | |
Stream.prototype.length = function() { | |
return this.reduce( (n, _) => n + 1, 0 ) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// all : ( Stream a, a -> aBoolean ) -> Promise Boolean | |
Stream.prototype.all = function(pred) { | |
return this.reduce( (prev, cur) => prev && !!pred(cur), true ); | |
} | |
Stream.seq([1,2,3,4,5], 0, 1000).all( x => x % 2) // => false |