Created
October 7, 2016 06:05
-
-
Save rkrupinski/30e5b98258918bf82d026a75bd1c66cb to your computer and use it in GitHub Desktop.
Currying with generators
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* curryGen(fn) { | |
const l = fn.length; | |
const args = []; | |
while (true) { | |
if (args.length < l) { | |
args.push(...yield); | |
} else { | |
return fn(...args); | |
} | |
} | |
} | |
function curry(fn) { | |
const iter = curryGen(fn); | |
iter.next(); | |
return function curryOnce(...args) { | |
const { done, value } = iter.next(args); | |
return done ? value : curryOnce; | |
}; | |
} | |
console.log(curry((a, b, c) => a + b + c)()(1)()(2,3)); // 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment