JS Built-ins
FunctionalProgrammingadvanced

Compose vs pipe equivalence

πŸ“– Usage Example
const compose = (...fns) => x => fns.reduceRight((v, f) => f(v), x);
const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x);
// compose(g, h)(x) === pipe(h, g)(x)

compose(g, h) and pipe(h, g) produce the same result β€” they differ only in order.

Comparing Compose and Pipe

Compose and pipe do the same thing but in opposite orders. Their composed results should be equivalent when used correctly.

Challenge: Prove that compose(f, g)(x) equals pipe(g, f)(x) by implementing both and testing their equivalence.

πŸ’‘ Hints (3)
  • β€’ compose(f, g) reads right-to-left: apply g first, then f
  • β€’ pipe(g, f) reads left-to-right: apply g first, then f
  • β€’ They are mirrors of each other