Some experiments on Arrows using Arrowlets(http://www.cs.umd.edu/projects/PL/arrowlets/index.xhtml) code.
function AsyncA(cps) { | |
this.cps = cps; | |
} | |
AsyncA.prototype.AsyncA = function () { | |
return this; | |
}; | |
AsyncA.prototype.next = function (g) { | |
var f = this; | |
g = g.AsyncA(); | |
return new AsyncA(function (x, k) { | |
f.cps(x, function(y) { | |
g.cps(y, k); | |
}); | |
}); | |
}; | |
AsyncA.prototype.first = function() { | |
var f = this; | |
return new AsyncA(function(x, k) { | |
f.cps(x.fst, function(y) { | |
k({fst: y, snd:x.snd}); | |
}); | |
}); | |
}; | |
AsyncA.prototype.second = function() { | |
var f = this; | |
return new AsyncA(function(x, k) { | |
f.cps(x.snd, function(y) { | |
k({fst: x.fst, snd: y}); | |
}); | |
}); | |
}; | |
AsyncA.prototype.pair = function(g) { | |
var f = this; | |
return f.first().next(g.second()); | |
}; | |
AsyncA.prototype.both = function(g) { | |
var f = this; | |
return new AsyncAF(function(x) { | |
return {fst: x, snd: x}; | |
}).next(f.pair(g)); | |
}; | |
AsyncA.prototype.run = function(x) { | |
this.cps(x, function (y) {}); | |
}; | |
var AsyncAF = function(f) { | |
return new AsyncA(function(x, k) { | |
k(f(x)); | |
}); | |
}; | |
function ConstA(x) { | |
return AsyncAF(function() { | |
return x; | |
}); | |
}; | |
var square = AsyncAF(function(n) { return n * n; }); | |
var addOne = AsyncAF(function(n) { return n + 1; }); | |
var asyncAddOne = new AsyncA(function(x, k) { | |
setTimeout(function() { | |
k(x + 1); | |
}, 1000); | |
}); | |
ConstA(2).next(square).next(asyncAddOne).next(console.log).run(); | |
ConstA(2).next(square.both(addOne)).next(console.log).run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment