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
/*************************************************/ | |
/************* EventStream ***********************/ | |
/*************************************************/ | |
function EventStream(binder) { | |
this.binder = binder; | |
this.reset(); | |
} | |
EventStream.prototype.reset = function() { | |
this.lastTime = 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
function eachKey(obj, f) { | |
for(var key in obj) { | |
if( obj.hasOwnProperty(key) ) | |
f(key, obj[key]); | |
} | |
} | |
function adtcase (base, proto, key) { | |
return (...args) => { | |
var inst = new base(); |
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
function toArray(args) { | |
return Array.prototype.slice.call(args); | |
} | |
function doLater(act) { | |
setTimeout(act, 0); | |
} | |
function eachKey(obj, f) { | |
for(var key in obj) { |
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
"use strict"; | |
class Emitter { | |
constructor() { | |
this.slots = [], | |
this.ends = []; | |
} | |
onValue(h) { |
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
function Stream() {} | |
adt( Stream, { | |
Empty : new Stream(), | |
Abort : function (error) { return {error} }, | |
Promise : function (promise) { return {promise} }, | |
Cons : function (head, tail) { return {head, tail} } | |
}) |
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
const noop = () => {} | |
Stream.prototype.forEach = function(onNext, onError=noop, onComplete=noop) { | |
return this.isEmpty ? onComplete() | |
: this.isAbort ? onError(this.error) : | |
: this.isCons ? ( | |
onNext(this.head), | |
this.tail.forEach(onNext, onError, onComplete)) | |
: this.promise.then( | |
stream => stream.forEach(onNext, onError, onComplete), |
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
Stream.prototype.log = function(prefix) { | |
this.forEach( | |
v => console.log(prefix, v), | |
err => console.log(prefix, ' error!!', err), | |
() => console.log(prefix, ' end.') | |
); | |
} | |
Stream.seq([1,2,3], 0, 1000).log('seq') |
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
// map : (Stream a, a -> b) -> Stream b | |
Stream.prototype.map = function(f) { | |
return this.isEmpty || this.isAbort ? this | |
: this.isCons ? Stream.Cons(f(this.head), this.tail.map(f)) | |
: Stream.Future( | |
this.promise.then( | |
s => s.map(f), | |
err => Stream.Abort(err))) |
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 |
OlderNewer