JavaScript 中的管道运算符
The pipeline operator in JavaScript
管道运算符会启用函数组合吗?
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?
上面代码中tailAndAverage
是一个函数吗?
否 - 引用提案:
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.
因此您的示例实际上最终会脱糖为 avg(tail)
,这不是您想要的。
也就是说,还有两个单独的提议向语言中添加组合运算符:
管道运算符会启用函数组合吗?
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?
上面代码中tailAndAverage
是一个函数吗?
否 - 引用提案:
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.
因此您的示例实际上最终会脱糖为 avg(tail)
,这不是您想要的。
也就是说,还有两个单独的提议向语言中添加组合运算符: