Curry Problem
Description
LeetCode Problem 2632.
Given a function fn, return a curried version of that function.
A curried function is a function that accepts fewer or an equal number of parameters as the original function and returns either another curried function or the same value the original function would have returned.
In practical terms, if you called the original function like sum(1,2,3), you would call the curried version like csum(1)(2)(3), csum(1)(2,3), csum(1,2)(3), or csum(1,2,3). All these methods of calling the curried function should return the same value as the original.
Example 1:
Input:
fn = function sum(a, b, c) { return a + b + c; }
inputs = [[1],[2],[3]]
Output: 6
Explanation:
The code being executed is:
const curriedSum = curry(fn);
curriedSum(1)(2)(3) === 6;
curriedSum(1)(2)(3) should return the same value as sum(1, 2, 3).
Example 2:
Input:
fn = function sum(a, b, c) { return a + b + c; }
inputs = [[1,2],[3]]
Output: 6
Explanation:
curriedSum(1, 2)(3) should return the same value as sum(1, 2, 3).
Example 3:
Input:
fn = function sum(a, b, c) { return a + b + c; }
inputs = [[],[],[1,2,3]]
Output: 6
Explanation:
You should be able to pass the parameters in any way, including all at once or none at all.
curriedSum()()(1, 2, 3) should return the same value as sum(1, 2, 3).
Example 4:
Input:
fn = function life() { return 42; }
inputs = [[]]
Output: 42
Explanation:
currying a function that accepts zero parameters should effectively do nothing.
curriedLife() === 42
Constraints:
1 <= inputs.length <= 10000 <= inputs[i][j] <= 10^50 <= fn.length <= 1000inputs.flat().length == fn.length- function parameters explicitly defined
- If
fn.length > 0then the last array ininputsis not empty - If
fn.length === 0theninputs.length === 1
Solution
Approach: Collect arguments until fn.length is reached
fn.length is the number of parameters the original function declares. The curried function remembers the arguments received so far; each call appends its arguments and checks the count. Once at least fn.length arguments have been collected, call fn with them and return the result. Before that, return another curried function that carries the collected arguments forward.
Passing nothing (csum()()(1, 2, 3)) just returns a new curried function with the same collected list, and a zero-parameter function is satisfied on the first call, so curriedLife() returns 42 right away. Build a fresh array on each step instead of mutating a shared one, so that a partially applied function can be reused.
Complexity: O(total arguments) per full call chain (each step copies the collected list, so up to O(k2) when passing one argument at a time for k parameters).
/**
* @param {Function} fn
* @return {Function}
*/
var curry = function (fn) {
return function curried(...args) {
if (args.length >= fn.length) {
return fn(...args);
}
return (...more) => curried(...args, ...more);
};
};
/**
* function sum(a, b) { return a + b; }
* const csum = curry(sum);
* csum(1)(2) // 3
*/