ComposeLoading saved progress…

Compose

You've chained data through a few transformations — trim a string, lowercase it, then split it. Writing split(lower(trim(input))) reads inside-out, and it gets worse with every step. Function composition gives you a single reusable function built from the pieces, applied right-to-left so the picture matches the math: compose(f, g, h)(x) === f(g(h(x))).

Implement a compose function that takes any number of unary functions and returns a new function. Calling that new function with an input runs the rightmost function first, feeds its result into the next, and so on, returning the final value. See MDN on function composition for background.

Signature

function compose(...fns) {
  // returns a new function that, when called with `x`,
  // applies the rightmost fn to `x` and pipes results leftward
}

Examples

const addOne = (n) => n + 1;
const double = (n) => n * 2;

const f = compose(addOne, double);
f(3); // 7   — double(3) = 6, then addOne(6) = 7
const shout = compose(
  (s) => s + '!',
  (s) => s.toUpperCase(),
  (s) => s.trim(),
);
shout('  hello  '); // 'HELLO!'

Notes

  • Right-to-left. The last function in the argument list runs first.
  • Identity on empty. compose()(x) must return x unchanged.
  • Single function. compose(f)(x) must return f(x).
  • Unary only. Each function takes one argument and returns one value. Don't worry about variadic intermediate functions.
  • No mutation. Don't modify the input fns array.
Loading editor…