-
Notifications
You must be signed in to change notification settings - Fork 14
Open
Description
const curry = (fn, ...args) => {
const initAllArgs = () => [...args];
let allArgs = initAllArgs();
const innerFn = function(...args) {
if (args.length == 0) {
const result = fn.apply(this, allArgs);
allArgs = initAllArgs();
return result;
} else {
allArgs.push(...args);
return innerFn;
}
};
return innerFn;
};
const add = (...args) => args.reduce((pre, cur) => pre + cur);
const newAdd = curry(add, 5, 6);
console.log(newAdd(1)(2)(3, 4)());
console.log(newAdd(1, 2)(3, 4)());
console.log(newAdd(1, 2, 3, 4)());
Metadata
Metadata
Assignees
Labels
No labels