Function Composition (Pipe)
Function Composition (Pipe) is a reusable JavaScript snippet. Creates a pipeline of functions where the output of each function becomes the input of the next. The full implementation: // Left-to-right: pipe const pipe = (...fns) => (x) => fns.reduce((v, fn) => fn(v), x) // Right-to-left: compose const compose = (...fns) => (x) => fns.reduceRight((v, fn) => fn(v), x) // Usage const addOne = x => x + 1 const double = x => x * 2 const toString = x => String(x) const process = pipe(addOne, double, toString) process(5) // "12" (5+1=6, 6*2=12, "12") This snippet uses only standard JavaScript with no dependencies, so it runs in modern browsers and Node.js alike. Browse more patterns in the snippet library, or paste the code straight into the playground to experiment with it.