Implement Pipe

EasyFunctional Programming10 min4 tests

Implement a pipe function that takes multiple functions and returns a new function that applies them left-to-right (the opposite of compose).

Requirements

  • pipe(f, g, h)(x) should equal h(g(f(x)))
  • Should work with any number of functions

Example

const add1 = x => x + 1;
const double = x => x * 2;
pipe(add1, double)(5); // 12 (add1 first: 6, then double: 12)
Hints (2)
  1. Very similar to compose but in the opposite direction.
  2. Use reduce instead of reduceRight.

Topics

  • Functional Programming

Asked at

Stripe · Airbnb

Join Us
blur