如何实现管道/撰写功能?

时间:2018-09-05 12:06:32

标签: rust function-composition

我正在尝试编写一个'pipe'函数,这在Javascript中非常简单:

const pipe = (...funcs) => val => (
  funcs.reduce((acc, func) => func(acc), val)
)

用法示例:

const sqrt = v => Math.sqrt(v);
const half = v => v / 2;
const plusFive = v => v + 5;

plusFive(half(sqrt(16))); // 7

const doStuff = pipe(sqrt, half, plusFive);
doStuff(16); // 7 

如何在Rust中做同样的事情?谢谢!

0 个答案:

没有答案