JavaScript › Functional Programming
What is function composition and how do you implement it?
IntermediateAnswer
Composition combines multiple small functions into one, where the output of each becomes the input to the next — typically implemented with a compose() or pipe() helper that reduces over an array of functions.
const pipe = (...fns) => (x) => fns.reduce((acc, fn) => fn(acc), x);
const addOne = n => n + 1;
const double = n => n * 2;
const transform = pipe(addOne, double);
transform(3); // (3+1)*2 = 8Real-world example Building a data-processing pipeline that trims, validates, then formats user input in clearly separated steps.