Building pipe and compose
Implement left-to-right and right-to-left composition.
From Two Functions to Many
A two-argument compose is limiting. Real pipelines chain many steps. In this lesson you will build general-purpose compose and pipe using reduce and reduceRight.
pipe: Left to Right
pipe reads in execution order: the first function runs first. pipe(a, b, c)(x) equals c(b(a(x))). Most people find this more natural than compose.
const pipe = (...fns) => x => fns.reduce((acc, fn) => fn(acc), x);
const inc = n => n + 1;
const double = n => n * 2;
console.log(pipe(inc, double)(5));All lessons in this course
- Partial Application
- Currying Functions
- Function Composition
- Building pipe and compose