Overview
Currying transforms a function that takes multiple arguments into a chain of single-argument (or partial-argument) functions. Your curry(fn) should return a new function that:
- Collects arguments across successive calls.
- Invokes the original
fnonce enough arguments have been accumulated (based onfn.length). - Supports passing multiple arguments in a single call (
curried(1, 2)(3)).
This is a common utility in functional programming and a frequently tested concept in senior-level interviews.
Constraints
- Use
fn.lengthto determine the target arity. - Preserve argument order across all calls.
- Once arity is satisfied, invoke
fnimmediately and return the result. - Extra arguments beyond arity should be ignored.
- If
fn.lengthis 0, invokefnimmediately on the first call. - Do not use
Function.prototype.bindinternally.
Examples
// Basic currying.
function add(a, b, c) {
return a + b + c;
}
const curriedAdd = curry(add);
curriedAdd(1)(2)(3); // 6
curriedAdd(1, 2)(3); // 6
curriedAdd(1)(2, 3); // 6
curriedAdd(1, 2, 3); // 6// Partial application reuse.
function multiply(a, b) {
return a * b;
}
const double = curry(multiply)(2);
double(5); // 10
double(10); // 20Notes
- The key insight is recursion: each call returns a new function if not enough arguments have been collected yet.
- Think about how to track accumulated arguments across calls — closures are your friend.
fn.lengthreturns the number of declared parameters (not counting rest params or default values).
Solution
Reveal solution
function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) {
return fn(...args.slice(0, fn.length));
}
return function (...next) {
return curried(...args, ...next);
};
};
}Resources
curry-with-arity.js
Curry With Arity
hardcodingJavaScriptFunctions
Overview
Currying transforms a function that takes multiple arguments into a chain of single-argument (or partial-argument) functions. Your curry(fn) should return a new function that:
- Collects arguments across successive calls.
- Invokes the original
fnonce enough arguments have been accumulated (based onfn.length). - Supports passing multiple arguments in a single call (
curried(1, 2)(3)).
This is a common utility in functional programming and a frequently tested concept in senior-level interviews.
Constraints
- Use
fn.lengthto determine the target arity. - Preserve argument order across all calls.
- Once arity is satisfied, invoke
fnimmediately and return the result. - Extra arguments beyond arity should be ignored.
- If
fn.lengthis 0, invokefnimmediately on the first call. - Do not use
Function.prototype.bindinternally.
Examples
// Basic currying.
function add(a, b, c) {
return a + b + c;
}
const curriedAdd = curry(add);
curriedAdd(1)(2)(3); // 6
curriedAdd(1, 2)(3); // 6
curriedAdd(1)(2, 3); // 6
curriedAdd(1, 2, 3); // 6// Partial application reuse.
function multiply(a, b) {
return a * b;
}
const double = curry(multiply)(2);
double(5); // 10
double(10); // 20Notes
- The key insight is recursion: each call returns a new function if not enough arguments have been collected yet.
- Think about how to track accumulated arguments across calls — closures are your friend.
fn.lengthreturns the number of declared parameters (not counting rest params or default values).
Solution
Reveal solution
function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) {
return fn(...args.slice(0, fn.length));
}
return function (...next) {
return curried(...args, ...next);
};
};
}Resources
NameTopicDifficulty
103 of 103 problems