May 2017
Intermediate to advanced
590 pages
17h 18m
English
These functions can be pipelined, that is, they can call one function with the result of another. The following example maps a range of integers into a range of positive integers by applying the std::abs() function to its elements. The result is then mapped into another range of squares. These are then summed together by applying a left fold on the range:
auto vnums = std::vector<int>{ 0, 2, -3, 5, -1, 6, 8, -4, 9 }; auto s = funclib::foldl( std::plus<>(), funclib::mapf( [](int const i) {return i*i; }, funclib::mapf( [](int const i) {return std::abs(i); }, vnums)), 0); // s = 236
As an exercise, we could implement the fold function as a variadic function template, in the manner seen in a previous recipe. The function ...
Read now
Unlock full access