Skip to content

Instantly share code, notes, and snippets.

Yassine Elouafi yelouafi

Block or report user

Report or block yelouafi

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View stream-mapError.js
// 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))
View stream-filter.hs
filter :: (a -> Bool) -> [a] -> [a]
filter _pred [] = []
filter pred (x:xs)
| pred x = x : filter pred xs
| otherwise = filter pred xs
View stream-filter.js
// 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(
View stream-span.hs
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)
View stream-span.js
// 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),
View stream-groupBy.js
// 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)))
View stream-group.js
// group : Stream a => Stream (Stream a)
Stream.prototype.group = function() {
return this.groupBy( (x1, x2) => x1 === x2 );
}
View stream-reduce.js
// 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)
View stream-length.js
// length : Stream a -> Promise Number
Stream.prototype.length = function() {
return this.reduce( (n, _) => n + 1, 0 )
}
View stream-all.js
// 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
You can’t perform that action at this time.