Skip to content

Instantly share code, notes, and snippets.

Avatar

Yassine Elouafi yelouafi

View GitHub Profile
View frp-toy.js
/*************************************************/
/************* EventStream ***********************/
/*************************************************/
function EventStream(binder) {
this.binder = binder;
this.reset();
}
EventStream.prototype.reset = function() {
this.lastTime = 0;
View List.js
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();
View Stream.js
function toArray(args) {
return Array.prototype.slice.call(args);
}
function doLater(act) {
setTimeout(act, 0);
}
function eachKey(obj, f) {
for(var key in obj) {
View observable.js
"use strict";
class Emitter {
constructor() {
this.slots = [],
this.ends = [];
}
onValue(h) {
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-length.js
// length : Stream a -> Promise Number
Stream.prototype.length = function() {
return this.reduce( (n, _) => n + 1, 0 )
}
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-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
View streqm-flatten.js
// flatten : ( Stream (Stream a), (Stream a, Stream a) -> Stream a) -> Stream a
Stream.prototype.flatten = function(f) {
return this.isEmpty || this.isAbort ? this
: this.isCons ? f( this.head, this.tail.flatten(f) )
: Stream.Future(this.promise.then(s => s.flatten(f), Stream.Abort));
};
View stream-mergeAll.js
// mergeAll : Stream (Stream a) -> Stream a
Stream.prototype.mergeAll = function() {
return this.flatten( (s1, s2) => s1.merge(s2) );
}