How to implement the pipe / compose function?

Viewed 128

I'm trying to write a 'pipe' function, which is pretty straightforward in Javascript:

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

Example usage:

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 

How to do same thing in Rust? Thanks!

0 Answers
Related