AniUI Academy
medium+250 XPPractice

Partial application, left and right

Partial application is what you actually reach for in product code: you have fetchJson(baseUrl, path, options) and you want fetchApi with the base URL already filled in. Unlike currying, it does not care about arity — it hands over whatever it has and calls through immediately.

Implement two functions. partial(fn, ...preset) returns a function that calls fn with the preset arguments first, followed by the arguments given at call time. partialRight(fn, ...preset) returns a function that calls fn with the call-time arguments first, followed by the preset arguments. Both return fn's return value, both work with no preset arguments at all, both can be called repeatedly without the preset arguments accumulating, and both preserve this so they can be used as object methods.

What it has to do

  • partial places the preset arguments before the call-time arguments.
  • partialRight places the preset arguments after the call-time arguments.
  • Both work when no preset arguments are supplied.
  • Repeated calls to the same partial do not accumulate arguments.
  • Both preserve this when used as an object method.

Your workspace

Try it yourself
Loading playground...

Ready to check it?

5 tests run against your code, right here in your browser. Sign in to claim the XP when you pass.

AI Crack & Solution Assist

Stuck? Get instant AI hints or break down the optimal solution.

Stuck? The javascript course covers everything this challenge needs.