#include #include #include #include "streams.hpp" namespace stream { template auto make_map_t(F&& func) -> map_t()))> { return map_t()))>{func}; } template auto make_flatmap_t(F&& func) -> flatmap_t()))> { return flatmap_t()))>{func}; } template auto make_filter_t(F&& func) -> filter_t { return filter_t{func}; } } #define map(t, x, expr) stream::make_map_t([](t x) { return expr; }) #define filter(t, x, expr) stream::make_filter_t([](t x) { return expr; }) #define flatmap(t, x, expr) stream::make_flatmap_t([](t x) { return expr; }) #define fold(t, x, y, expr, init) stream::make_fold_t([](t x, t y) { return expr; }) template std::ostream& operator<<(std::ostream& os, std::vector const& tmp) { os << "{ "; for (auto it = tmp.begin(); it != tmp.end(); ++it) { os << *it << ", "; } return os << "\b\b }"; } std::vector iota(int max) { std::vector r(3); std::iota(r.begin(), r.end(), max); return r; } int main(int argc, const char**argv) { std::vector vec{1,2,3,4,5}; // auto s = stream::make_stream(vec) | stream::map_t([](int x) { return x*2; }); auto s = stream::make_stream(vec) | filter(int, x, x%2==0) | map(int, x, x*2); std::cout << "The result of the expression 'stream(vec) | filter(x,x%2==0) | map(x,x*2)' is:\n\t"; std::cout << "{ "; for (auto it = s.begin(); it != s.end(); ++it) { std::cout << *it << ", "; } std::cout << "\b\b }" << std::endl; std::cout << "The result of the expression 'stream(vec) | filter(x,x%2==0) | map(x,x*2) | fold(i,j,i*j,1)' is:\n\t"; std::cout << s.accumulate([](int i, int j){return i*j;},1); std::cout << std::endl; auto t = s | flatmap(int, x, std::vector{1/(double)x}); std::cout << "The result of the expression 'stream(vec) | filter(x,x%2==0) | map(x,x*2) | map(x, [1/x])' is:\n\t"; std::cout << "{ "; for (auto it = t.begin(); it != t.end(); ++it) { std::cout << *it << ", "; } std::cout << "\b\b }" << std::endl; auto q = stream::make_stream(vec) | filter(int, x, x%2==1) | map(int, x, iota(x)); std::cout << "The result of the expression 'stream(vec) | filter(x,x%2==1) | map(x,iota(x))' is:\n\t"; std::cout << "{ "; for (auto it = q.begin(); it != q.end(); ++it) { std::cout << *it << ", "; } std::cout << "\b\b }" << std::endl; return 0; }