Skip to content

Instantly share code, notes, and snippets.

@phi16
Created October 7, 2016 02:55
Show Gist options
  • Select an option

  • Save phi16/1eb2e3ac4060b076702f70f6155a7bac to your computer and use it in GitHub Desktop.

Select an option

Save phi16/1eb2e3ac4060b076702f70f6155a7bac to your computer and use it in GitHub Desktop.
Reader Monad
function *f(){
var a = yield "Arg1";
var b = yield "Arg2";
return a+b;
}
function *add(g1,g2){
var x = yield* g1;
var y = yield* g2;
return x+y;
}
function *output(g){
var ret = yield* g;
console.log(ret);
}
function exec(g){
var u;
var val;
while(u = g.next(val), !u.done){
if(u.value == "Arg1")val = 3;
if(u.value == "Arg2")val = 5;
}
}
exec(output(add(f(),f()))); // 16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment