Peter Seliger
2 min readJul 25, 2024

--

Nice overview except for the chosen array-method pipelining example, where the author states ... "In this example, the pipeline operator is used to chain map, filter and reduce methods, resulting in a clean and readable transformation sequence."

When comparing …

const numbers = [1, 2, 3, 4, 5];

const result = numbers
|> (nums => nums.map(n => n * 2))
|> (nums => nums.filter(n => n > 5))
|> (nums => nums.reduce((sum, n) => sum + n, 0));

console.log(result);

… to …

const numbers = [1, 2, 3, 4, 5];

const result = numbers
.map(n => n * 2)
.filter(n => n > 5)
.reduce((sum, n) => sum + n, 0);

console.log(result);

… I think, it is not just an opinion to say, that the 2nd example code which uses classic method chaining, (altering the author’s quote) … presents a much cleaner and better readable transformation sequence.

The by far better comparison does provide the author’s lodash based example code; … have a look at …

const data = [1, 2, 3, 4, 5];

// const result =
// _.sum(_.filter(_.map(data, n => n * 2), n => n > 5));

const result =
_.sum(
_.filter(
_.map(
data,
n => n * 2
),
n => n > 5
)
);

console.log(result); // 24

… versus …

const _ = require('lodash');

const data = [1, 2, 3, 4, 5];
const result = data
|> _.map(_, n => n * 2)
|> _.filter(_, n => n > 5)
|> _.sum;

console.log(result); // 24

One instantly and clearly gets the reason/purpose/idea behind the pipeline operator; transforming nested function calls into visually chained operations.

--

--

No responses yet