Implement curry
Write curry(fn): collect arguments across calls until at least fn.length of them have arrived, then call fn with all of them.
- Any call may pass several arguments, and extra ones are passed through.
- Partial applications are reusable:
const add1 = add(1)can be called many times without the calls affecting each other. - A function with no parameters is called on the first call, and
thisis forwarded.
const add3 = curry((a, b, c) => a + b + c); add3(1)(2)(3); add3(1, 2)(3); add3(1)(2, 3); add3(1, 2, 3); // all 6
Hints
Hint 1
curry returns a function that compares the arguments collected so far against fn.length. With enough, call fn.
Hint 2
With too few, return a new function that closes over the arguments so far. Build a new array on every call — never mutate a shared one, or partials start affecting each other.