Curry with Placeholders
HardP3Coding
Implement curry(fn), which turns a function into a curried one that accepts its arguments in several calls, with placeholder support.
- The curried function can be called with any number of arguments at a time. As soon as enough arguments have been collected (at least
fn.length, and none of the firstfn.lengthis a placeholder), it callsfnwith all collected arguments and returns the result. Until then it returns a new function that keeps collecting. curry.placeholderis a unique value that marks a skipped argument. A placeholder in a call reserves that position; later arguments fill the earliest reserved positions first, and only then are appended.- Partial applications are reusable: calling a returned function twice with different arguments must not interfere.
thisof the final call is passed tofn.
Example 1
- Input
- const _ = curry.placeholder; const add3 = curry((a, b, c) => a + b + c); add3(1)(2)(3) add3(_, 2)(1)(3) add3(1, _, 3)(2)
- Output
- 6 6 6
Constraints
- 0 <= fn.length <= 10
- Arguments can be any values except the placeholder itself.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 11. ⌘Enter runs, ⌘⇧Enter submits.