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
1 changed files with 23 additions and 4 deletions

View File

@ -1,5 +1,6 @@
function Async() {
return {
apply: apply,
bind: bind,
parallel: parallel,
run: run,
@ -8,12 +9,30 @@ function Async() {
wrap: wrap
};
function bind(m, f) {
function apply(f, x) {
return function(g) {
m(function(x) {
f(x)(g);
});
g(f(x));
};
}
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() {