Skip to content

Instantly share code, notes, and snippets.

@luqmaan
Created June 19, 2018 15:38
Show Gist options
  • Save luqmaan/1d43a9657a45a7c310bba25ec3b4a106 to your computer and use it in GitHub Desktop.
Save luqmaan/1d43a9657a45a7c310bba25ec3b4a106 to your computer and use it in GitHub Desktop.
function curry(func, ...argv) {
let prevArgs = argv;
function innerCurry(...args) {
prevArgs.push(...args)
if (prevArgs.length === func.length) {
return func(...prevArgs);
}
return innerCurry;
}
return innerCurry;
}
function add1(a) {
return a + 1;
}
function add(a, b, c, d) {
return a + b + c + d;
}
function concat(a, b, c) {
return `${a} ${b} ${c}`;
}
function hi() {
return 'hi';
}
console.log(curry(add)(1)(4, 2)(3))
console.log(curry(concat)('a', null)()(null))
console.log(curry(hi)())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment