I stumbled upon this nice blog post – pipable functions in C++14. This is realy fun idea as its usage plain for anybody who is familiar with unix pipelines. So i tried to use it in C++11 (without boost) from slightly different angle to make it more real-life concept.
First sample (fun, but not so interesting):
1 2 3 |
auto add = piped([](int x, int y){ return x + y; }); auto mul = piped([](int x, int y){ return x * y; }); int y = 5 | add(2) | mul(5) | add(1); // Output: 36 |
Second sample (functional style array processing):
1 2 3 |
vector<int> numbers{4,8,15,16,23,42}; auto result = numbers | where([](int x){ return (x > 10); }) | map([](int x){ return x + 5; }) | log(); // List: 20 21 28 47 |
Simple and short implementation is under the cut…