Curry

Yangshun TayEx-Meta Staff Engineer
Languages

Currying is the technique of converting a function that takes multiple arguments into a sequence of functions that each takes a single argument.

Implement curry(fn) to return a curried version of fn. Collect one argument at a time until at least fn.length arguments have been provided, then call fn with the collected arguments.

Calls with no arguments should be ignored and return another function. Preserve the this value from the first call in the chain when fn is finally invoked.

Arguments

  1. fn (Function): The function to curry.

Returns

(Function): Returns the curried function.

Examples

function add(a, b) {
return a + b;
}
const curriedAdd = curry(add);
curriedAdd(3)(4); // 7
const alreadyAddedThree = curriedAdd(3);
alreadyAddedThree(4); // 7
function multiplyThreeNumbers(a, b, c) {
return a * b * c;
}
const curriedMultiplyThreeNumbers = curry(multiplyThreeNumbers);
curriedMultiplyThreeNumbers(4)(5)(6); // 120
curriedMultiplyThreeNumbers()(4)()(5)(6); // 120
const containsFour = curriedMultiplyThreeNumbers(4);
const containsFourMulFive = containsFour(5);
containsFourMulFive(6); // 120

Asked at these companies

Premium featurePurchase premium to see companies which ask this question.
View plans

Loading editor