streams.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <vector>
  2. #include <iostream>
  3. #include <iterator>
  4. #include "streams.hpp"
  5. namespace stream {
  6. template <typename T, typename F>
  7. auto make_map_t(F&& func) -> map_t<T, decltype(func(std::declval<T>()))> {
  8. return map_t<T, decltype(func(std::declval<T>()))>{func};
  9. }
  10. template <typename T, typename F>
  11. auto make_flatmap_t(F&& func) -> flatmap_t<T, decltype(func(std::declval<T>()))> {
  12. return flatmap_t<T, decltype(func(std::declval<T>()))>{func};
  13. }
  14. template <typename T, typename F>
  15. auto make_filter_t(F&& func) -> filter_t<T> {
  16. return filter_t<T>{func};
  17. }
  18. }
  19. #define map(t, x, expr) stream::make_map_t<t>([](t x) { return expr; })
  20. #define filter(t, x, expr) stream::make_filter_t<t>([](t x) { return expr; })
  21. #define flatmap(t, x, expr) stream::make_flatmap_t<t>([](t x) { return expr; })
  22. #define fold(t, x, y, expr, init) stream::make_fold_t<t>([](t x, t y) { return expr; })
  23. template <typename T>
  24. std::ostream& operator<<(std::ostream& os, std::vector<T> const& tmp) {
  25. os << "{ ";
  26. for (auto it = tmp.begin(); it != tmp.end(); ++it) {
  27. os << *it << ", ";
  28. }
  29. return os << "\b\b }";
  30. }
  31. std::vector<int> iota(int max) {
  32. std::vector<int> r(3);
  33. std::iota(r.begin(), r.end(), max);
  34. return r;
  35. }
  36. int main(int argc, const char**argv) {
  37. std::vector<int> vec{1,2,3,4,5};
  38. // auto s = stream::make_stream(vec) | stream::map_t<int, int>([](int x) { return x*2; });
  39. auto s = stream::make_stream(vec) | filter(int, x, x%2==0) | map(int, x, x*2);
  40. std::cout << "The result of the expression 'stream(vec) | filter(x,x%2==0) | map(x,x*2)' is:\n\t";
  41. std::cout << "{ ";
  42. for (auto it = s.begin(); it != s.end(); ++it) {
  43. std::cout << *it << ", ";
  44. }
  45. std::cout << "\b\b }" << std::endl;
  46. 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";
  47. std::cout << s.accumulate([](int i, int j){return i*j;},1);
  48. std::cout << std::endl;
  49. auto t = s | flatmap(int, x, std::vector<double>{1/(double)x});
  50. 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";
  51. std::cout << "{ ";
  52. for (auto it = t.begin(); it != t.end(); ++it) {
  53. std::cout << *it << ", ";
  54. }
  55. std::cout << "\b\b }" << std::endl;
  56. auto q = stream::make_stream(vec) | filter(int, x, x%2==1) | map(int, x, iota(x));
  57. std::cout << "The result of the expression 'stream(vec) | filter(x,x%2==1) | map(x,iota(x))' is:\n\t";
  58. std::cout << "{ ";
  59. for (auto it = q.begin(); it != q.end(); ++it) {
  60. std::cout << *it << ", ";
  61. }
  62. std::cout << "\b\b }" << std::endl;
  63. return 0;
  64. }