Write a `curry(fn)` function that converts a multi-argument function into a sequence of single-argument functions.
Problem Statement
Write a `curry(fn)` function that converts a multi-argument function into a sequence of single-argument functions.
Complexity
Time Complexity: -
Space Complexity: -
Hints
š” Hint 1:
Currying transforms f(a, b, c) into f(a)(b)(c).
š” Hint 2:
Collect arguments until you have enough to call the original function.
š” Hint 3:
Return a new function that checks if enough args are collected; if not, return another function.
ā
Solution:
```javascript
function curry(fn) {
return function curried(...args) {
if (args.length >= fn.length) return fn(...args);
return (...nextArgs) => curried(...args, ...nextArgs);
};
}
```