In programming, function composition is the act of combining simple functions together to create more complex functionality. The result of each function is passed as an argument to the next function, and the result of the last function becomes the result of the whole composition. Functions put together this way are often referred to as a pipeline — an analogy to a physical pipeline in the sense that information flows through the pipeline in one direction, much like water would in a physical pipeline.

Chaining functions together in JavaScript is already possible today. Here is a simple example:

const repeat = str => `${str}, ${str}`;
const capitalize = str => str[0].toUpperCase() + str.substring(1);
const exclaim = str => `${str}!`;

const result = exclaim(capitalize(repeat('hello')))

console.log(result) // "Hello, hello!"

This is not bad — we’ve broken our code into three reusable components that are then called in sequence to create the desired result. The result of one function is passed as an argument to the next, with the result of the last function being our end result — essentially, we have our pipeline. This example has its limits in terms of readability, though. The function execution in this sequence is repeat followed by capitalize and finally exclaim— essentially right to left. However, Latin languages (like English) are read left to right, making our pipeline somewhat counterintuitive.

This brings us to a proposed new operator that might make things more readable in the future.

#react #functional-programming #javascript #programming #nodejs

The State of Piping in JavaScript
1.25 GEEK