Hint
Transform f(a,b,c) into f(a)(b)(c) β each call returns a new function waiting for more args
Currying transforms a multi-argument function into a chain of unary functions, each waiting for one argument at a time.
// Manual curried function
const add = a => b => c => a + b + c;
add(1)(2)(3); // 6
const add1 = add(1); // partially applied β waits for b and c
const add1and2 = add1(2); // waits for c
add1and2(3); // 6
// Generic curry utility
function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) { // enough args collected?
return fn(...args);
}
return (...moreArgs) => curried(...args, ...moreArgs);
};
}
const sum = (a, b, c) => a + b + c;
const curriedSum = curry(sum);
curriedSum(1)(2)(3); // 6
curriedSum(1, 2)(3); // 6 β also works (partial application hybrid)
Currying enables: partial application, point-free style, composable specialized functions.