861. Curry

Given a function fn, return a curried version of that function.

A curried function is one that takes as few or as many parameters as the original function and returns the same value as the original function or another curried function itself.

The curried version of the function would be called like csum(1)(2)(3), csum(1)(2,3), csum(1,2)(3), or csum(1,2,3) if you called the original function like sum(1,2,3). The value returned by each of these calls to the curried function should be identical to the original.

Example 1:

Input :

fn = function product(a, b, c) { return a * b * c; }

inputs = [[4,2],[3]]

Output : 24

Explanation :

The code being executed is:

const curriedProduct = curry(fn);

curriedProduct(4)(2)(3) === 24;

curriedProduct(4)(2)(3) should return the same value as product(4, 2, 3).

Example 2:

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).

Now Your Turn!

Pick the correct output for the given input

Input :

fn = function sum(a, b, c) { return a * b + c; }

inputs = [[4],[2],[3]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= inputs.length <= 1000
  • 0 <= inputs[i][j] <= 105
  • 0 <= fn.length <= 1000
  • inputs.flat().length == fn.length
  • function parameters explicitly defined
  • If fn.length > 0 then the last array in inputs is not empty

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
function curry(fn) {
return function curried(...args) {
//your code goes here
};
}
Test Case

Input:

Fn
Inputs