Deviopath › JavaScript

Curry With Placeholders

Hard · Functions

Write a function curry(fn) that returns a curried version of fn.

- Arguments can come in any grouping: f(1)(2)(3), f(1, 2)(3) and f(1, 2, 3) all call fn(1, 2, 3).

- As soon as the first fn.length positions hold real values, call fn with all collected arguments (extra ones too) and return its result. Until then, return a new curried function. f() changes nothing.

- The placeholder _ (defined in the starter) keeps a position open. A later call fills the open positions from left to right, then appends the rest.

- Intermediate functions are reusable: after const g = f(1), g(2)(3) and g(4)(5) must not affect each other.

testCurry(arity, prefix, branches) is already written: it turns "_" into the placeholder, applies prefix once, then runs each branch from that result ("function" = not called yet).

Next: Retry an Async Task