Pipe: Left-to-Right Composition
You'll learn how pipe applies functions in the natural reading order — left to right — making transformation chains more intuitive.
The Natural Reading Problem
You've learned compose, which applies functions right-to-left (last function first). While mathematically elegant, it can feel backwards when reading code. The pipe pattern solves this by reversing the order: functions execute left-to-right, matching how we naturally read English.
How Pipe Works
Instead of writing:
const result = compose(third, second, first)(value);
// Executes: third(second(first(value)))
With pipe, you write:
const result = pipe(first, second, third)(value);
// Executes: third(second(first(value))) — but reads left-to-right!
Same execution order, but the visual order matches the data flow.
Building a Pipe Helper
const pipe = (...fns) => (initialValue) =>
fns.reduce((value, fn) => fn(value), initialValue);
// Example transformation chain
const processUser = pipe(
user => user.name, // Step 1: extract name
name => name.toUpperCase(), // Step 2: uppercase
name => `Hello, ${name}!` // Step 3: format greeting
);
console.log(processUser({ name: 'alice' }));
// "Hello, ALICE!" — read the transformations top-to-bottom!
Why Choose Pipe?
Use pipe when describing step-by-step transformations: "Take this data, do X, then Y, then Z." It's especially clear for data processing pipelines where each step logically follows the previous one.
Key Takeaway: pipe applies functions left-to-right, making transformation sequences read naturally from first step to last — it's compose with reversed argument order for better readability.