2632. Curry π
Description
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] <= 1050 <= fn.length <= 1000inputs.flat().length == fn.length- function parameters explicitly defined
- If
fn.length > 0Β then the last array ininputsis not empty - IfΒ
fn.length === 0theninputs.length === 1Β
Solutions
Solution 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |