Last active
August 29, 2015 14:23
-
-
Save th507/626664612a1ae9893589 to your computer and use it in GitHub Desktop.
Currying with generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function *step(fn) { | |
var arr = Array(fn.length); | |
for (var i = 0; i < arr.length; i++) { | |
arr[i] = (yield); | |
} | |
var gen = fn.apply(null, arr); | |
return gen; | |
} | |
// Demo function | |
function timeout(a, b) { | |
setTimeout(a, b); | |
return "The end"; | |
} | |
// Usage: | |
var res = step(timeout); | |
var out = res.next(); | |
// arguments for timeout | |
var args = [console.log.bind(console, 1), 100]; | |
while (!out.done) { | |
// passing a and b to timeout | |
out = res.next( args.shift() ); | |
} | |
console.log(out.value); // -> "The end" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment