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
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
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
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
โ Linear Growth
Space Complexity
O(n)
Space used to store accumulated arguments in closure
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code