function sum(...args) {
if (!args.length) {
return 0;
}
const f = (...rest) => {
if (!rest.length) {
return args.reduce((a, b) => a + b);
} else {
return sum(...args, ...rest);
}
};
f.valueOf = () => args.reduce((a, b) => a + b);
return f;
}
console.log(sum()); // 0
console.log(sum(1)(2)(3)(4)(5)()); // 15
console.log(sum(1, 2, 3)(2)(3).valueOf()); // 11
console.log(sum(1, 2, 3)(2)(3) + sum(1, 2, 3)(2)); // 19