partialLoading saved progress…

partial

partial pre-fills some of a function's arguments and returns a new function that takes the rest. It's how you turn a general function into a specialized one — greet('Hi', name) becomes sayHi(name) — without writing a wrapper by hand. partialRight does the same from the right end, and a placeholder lets you leave a hole in the middle.

Implement partial(fn, ...preset) and partialRight(fn, ...preset), plus the placeholder _. partial prepends the preset arguments; partialRight appends them; a _ in the preset is filled by the next argument at call time.

Signature

function partial(fn, ...preset) {}      // fn(...preset, ...later)
function partialRight(fn, ...preset) {} // fn(...later, ...preset)
const _ = /* placeholder */;

Examples

const greet = (greeting, name) => `${greeting}, ${name}!`;
const sayHi = partial(greet, 'Hi');
sayHi('Ann');   // 'Hi, Ann!'
partial(greet, _, 'Ann')('Hi');      // 'Hi, Ann!'  — greeting is a hole
partialRight(greet, 'Ann')('Hi');    // 'Hi, Ann!'  — preset on the right

Notes

  • partial prepends — preset arguments come first, then the arguments you pass later.
  • partialRight appends — later arguments come first, then the preset ones.
  • Placeholder _ — marks a position to be filled by the next later argument, so you can skip an early parameter and fix a later one.
  • Reusable — the returned function can be called many times with different remaining arguments.
  • Preserve this — if the partial-applied function is called as a method, this should still bind correctly.
Loading editor…