Skip to content

CodeNot attempted

Curry

Difficulty: easy

curry(fn) keeps collecting arguments until it has fn.length of them, then calls fn.

const add3 = curry((a, b, c) => a + b + c);
add3(1)(2)(3) === add3(1, 2)(3) === add3(1)(2, 3) === add3(1, 2, 3) === 6
  • each partial is independent and reusable
  • preserve this from the final call
Show reference solution
export function curry(fn: (...args: any[]) => any): any {
return function innerCurry(this: unknown, ...args: any[]): any {
if (args.length >= fn.length) {
return fn.apply(this, args);
}
// Not enough arguments yet: wait for more.
return function (this: unknown, ...moreArgs: any[]) {
return innerCurry.apply(this, args.concat(moreArgs));
};
};
}
Loading editor…