Generalize bind to sequences of arbitrary length and add an apply method to get the result of an application into the async monad

This commit is contained in:
Tissevert 2018-12-09 23:01:11 +01:00
parent 6038d3bdbc
commit 3ab4762d2f

View file

@ -1,5 +1,6 @@
function Async() { function Async() {
return { return {
apply: apply,
bind: bind, bind: bind,
parallel: parallel, parallel: parallel,
run: run, run: run,
@ -8,12 +9,30 @@ function Async() {
wrap: wrap wrap: wrap
}; };
function bind(m, f) { function apply(f, x) {
return function(g) { return function(g) {
m(function(x) { g(f(x));
f(x)(g); };
}); }
function bind() {
var m, steps, i;
if(arguments.length < 1) {
return wrap();
} }
m = arguments[0];
steps = arguments;
i = 1;
return function(f) {
var step = function(x) {
if(i < steps.length) {
steps[i++](x)(step);
} else {
return f(x);
}
}
m(step);
};
} }
function parallel() { function parallel() {