function curry(fn) {
function curried(...args) {
if (args.length >= fn.length) {
return fn.apply(this, args);
}
return function collect(...nextArgs) {
return curried.apply(this, [...args, ...nextArgs]);
};
}
return curried;
}
function sum(a, b, c) {
return a + b + c;
}
const curriedSum = curry(sum);
curriedSum(1)(2)(3); // 6
curriedSum(1, 2)(3); // 6
curriedSum(1)(2, 3); // 6