Skip to content

Instantly share code, notes, and snippets.

@rkrupinski
Created October 7, 2016 06:05
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkrupinski/30e5b98258918bf82d026a75bd1c66cb to your computer and use it in GitHub Desktop.
Save rkrupinski/30e5b98258918bf82d026a75bd1c66cb to your computer and use it in GitHub Desktop.
Currying with generators
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