This file contains hidden or 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
| /* | |
| The MIT License (MIT) | |
| Copyright (c) 2014 | |
| Permission is hereby granted, free of charge, to any person obtaining a copy | |
| of this software and associated documentation files (the "Software"), to deal | |
| in the Software without restriction, including without limitation the rights | |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| copies of the Software, and to permit persons to whom the Software is |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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))) |
OlderNewer