Curry - Problem

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.

Input & Output

Example 1 — Basic Sum Function
$ Input: fn = function sum(a, b, c) { return a + b + c; }
Output: Curried version that can be called as curry(1)(2)(3) or curry(1, 2)(3), etc.
💡 Note: The curried function accumulates arguments until it has 3 parameters, then calls sum(1, 2, 3) = 6
Example 2 — Partial Application
$ Input: fn = function multiply(x, y) { return x * y; }
Output: curry(2)(5) or curry(2, 5) both return 10
💡 Note: Can be called with all arguments at once or one at a time, always returns 2 * 5 = 10
Example 3 — Single Parameter
$ Input: fn = function identity(x) { return x; }
Output: curry(42) returns 42 immediately
💡 Note: Since function only needs 1 parameter, curried version executes immediately when given 1 argument

Constraints

  • 1 ≤ fn.length ≤ 10
  • 0 ≤ arguments.length ≤ 1000
  • Function will be called with valid arguments

Visualization

Tap to expand
Function Currying: Transform Multi-Parameter to IncrementalOriginal Functionsum(1, 2, 3) → 6Currying TransformCreates nested functionsAccumulates argumentsCalls when readyCurried Functioncurry(1)(2)(3) → 6curry(1)curry(1)(2)curry(1)(2)(3)Returns functionReturns functionReturns result: 6Multiple calling patterns all produce the same result
Understanding the Visualization
1
Original Function
Function that takes multiple parameters: sum(a, b, c)
2
Currying Process
Transform into function that can accept arguments incrementally
3
Curried Function
Can be called as curry(1)(2)(3) or curry(1,2)(3) etc.
Key Takeaway
🎯 Key Insight: Use closure to maintain state and check argument count for efficient currying
Asked in
Google 15 Facebook 12
23.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen