Search code examples
javascriptpipeline

The pipeline operator in JavaScript


Will the pipeline operator enable the composition of functions?

const sum = (nos)=> nos.reduce((p,c)=> p + (+c), 0);
const avg = (nos)=> sum(nos) / nos.length;
const tail = ([_, ...tail])=> tail; 
const tailAndAverage = tail |> avg; // valid?

Is tailAndAverage a function in the above code?


Solution

  • No - to quote the proposal:

    The pipeline operator is essentially a useful syntactic sugar on a function call with a single argument. In other words, sqrt(64) is equivalent to 64 |> sqrt.

    So your example would effectively just end up desugaring to avg(tail), which isn't what you want.

    That said, there are also two separate proposals to add a composition operator to the language: