Skip to content
Onsite

Compose and Pipe

EasyP2Coding

Implement two function combinators.

  • pipe(...fns) returns a function that runs the functions left to right: the result of each is passed to the next.
  • compose(...fns) does the same right to left, like mathematical composition: compose(f, g)(x) is f(g(x)).

The first function to run may receive any number of arguments; every later function receives the single result of the previous one. With no functions, the returned function returns its first argument unchanged.

Example 1

Input
const add1 = x => x + 1, dbl = x => x * 2; pipe(add1, dbl)(3); compose(add1, dbl)(3);
Output
8 7
Why
pipe: (3 + 1) * 2. compose: (3 * 2) + 1.

Constraints

  • 0 <= fns.length <= 10^3
  • Every item is a synchronous function.

Hints

Run checks the 3 visible tests, Submit all 8.