Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. #include <iostream>
  2. #include "immutable.hpp"
  3. #include <vector>
  4. #include <list>
  5. #include <string>
  6. #include <cassert>
  7.  
  8. using namespace std;
  9.  
  10. int main(void){
  11.  
  12. using namespace colls;
  13.  
  14. auto testMap = [&]() {
  15. vector<int> v = {1, 2, 3, 5, 10, 100 };
  16. auto v2 = map(v, [](auto& a){ a *= 2; });
  17. for_each(v2.begin(), v2.end(), [](auto& a){ cout << a << ", "; });
  18. cout << endl;
  19.  
  20. int v3[] = {1, 2, 3, 5, 10, 100 };
  21. auto v4 = map(v3, [](auto& a) {a *= 2; });
  22. for_each(begin(v4), end(v4), [](auto a) {cout << a << ", "; } );
  23. cout << endl;
  24. };
  25. testMap();
  26.  
  27. auto testFilter = [&]() {
  28. vector<int> v = {1, 2, 3, 5, 10, 100 };
  29. auto v2 = filter(v, [](auto& a){ return a % 2 == 0; });
  30.  
  31. for_each(v2.begin(), v2.end(), [](auto& a){ cout << a << ", "; });
  32. cout << endl;
  33. };
  34. testFilter();
  35.  
  36. auto testReduceLeft = [&]() {
  37. vector<int> v = {1, 2, 3, 5, 10, 100 };
  38. auto add = [](const auto& l, const auto& r) { return l + r; };
  39.  
  40. vector<int> v1 = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  41. cout << "reduce v1 = " << reduceLeft(v1, add ) << endl;
  42.  
  43. vector<string> v2 = {"this ", "is ", "a ", "pen."};
  44. cout << "reduce v2 = " << reduceLeft(v2, add ) << endl;
  45.  
  46. vector<double> v3;
  47. cout << "reduce v3 = " << reduceLeft(v3, add) << endl;
  48.  
  49. };
  50. testReduceLeft();
  51.  
  52. auto testForEach = [&]() {
  53. vector<int> v = {1, 2, 3, 5, 10, 100 };
  54. auto l = for_each<std::list<int>>(v, [](auto& a) { ++a; } );
  55. for(auto a : l) { cout << a << ", "; }
  56. cout << endl;
  57.  
  58. auto l2 = for_each(v, [](auto& a) { ++a; } );
  59. for(auto a : l2) { cout << a << ", "; }
  60. cout << endl;
  61. };
  62. testForEach();
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement