Curry - Problem
Currying is a fundamental concept in functional programming that transforms a function that takes multiple arguments into a series of functions that each take a single argument. Your task is to implement a curry function that takes any function fn and returns its curried version.

The curried function should be flexible - it can accept any number of arguments up to the original function's arity. Once it receives enough arguments, it executes the original function. If it receives fewer arguments, it returns another function waiting for the remaining arguments.

Example: If you have sum(a, b, c), the curried version allows: curriedSum(1)(2)(3), curriedSum(1, 2)(3), curriedSum(1)(2, 3), or curriedSum(1, 2, 3) - all producing the same result.

Input & Output

example_1.js โ€” Basic Sum Function
$ Input: fn = (a, b, c) => a + b + c curriedFn = curry(fn) curriedFn(1)(2)(3)
โ€บ Output: 6
๐Ÿ’ก Note: The curried function accepts one argument at a time. Each call returns a new function until all 3 arguments are provided, then executes the original sum.
example_2.js โ€” Mixed Argument Grouping
$ Input: fn = (a, b, c) => a + b + c curriedFn = curry(fn) curriedFn(1, 2)(3)
โ€บ Output: 6
๐Ÿ’ก Note: The curried function can accept multiple arguments in a single call. Here it takes 2 arguments first, then 1 more to complete the required 3.
example_3.js โ€” All Arguments at Once
$ Input: fn = (a, b, c) => a + b + c curriedFn = curry(fn) curriedFn(1, 2, 3)
โ€บ Output: 6
๐Ÿ’ก Note: When all arguments are provided in the first call, the curried function behaves exactly like the original function.

Visualization

Tap to expand
Currying Function TransformationOriginal Functionsum(a, b, c)Needs 3 args at onceCurried Functioncurry(sum)(1)(2)(3)Flexible argument inputTransformCurrying Processfn(1)fn(2)fn(3)ResultArgs: [1]Args: [1,2]Args: [1,2,3]Execute!Each call either returns a new function or the final result๐Ÿ’ก Key: Use closure to accumulate arguments until arity is reached
Understanding the Visualization
1
Capture Function
Take the original function and determine how many arguments it needs
2
Create Accumulator
Build a function that collects arguments from multiple calls
3
Check Threshold
When enough arguments are collected, execute the original function
4
Return Result
Either return the final result or another function waiting for more arguments
Key Takeaway
๐ŸŽฏ Key Insight: Currying transforms any multi-argument function into a chain of single-argument functions using closure to maintain state across calls.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Each function call is constant time, just argument accumulation

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Space used to store accumulated arguments in closure

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค fn.length โ‰ค 10 (function arity between 1 and 10)
  • fn is a valid JavaScript function
  • Arguments can be of any type (number, string, object, etc.)
  • The curried function should work with any argument distribution
Asked in
Google 35 Facebook 28 Amazon 22 Microsoft 18
23.4K Views
Medium Frequency
~15 min Avg. Time
892 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