Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. /**
  6. Example showing the function programming ideas in
  7. C++ 11. Features used here are part of standard language.
  8. **/
  9.  
  10. template <typename Collection,typename unop>
  11. void for_each(Collection col, unop op){
  12. std::for_each(col.begin(),col.end(),op);
  13. }
  14.  
  15. template <typename Collection,typename unop>
  16. Collection map(Collection col,unop op) {
  17. std::transform(col.begin(),col.end(),col.begin(),op);
  18. return col;
  19. }
  20.  
  21. template <typename Collection,typename binop>
  22. Collection zip(Collection fc,Collection sc,binop op) {
  23. std::transform(fc.begin(),fc.end(),sc.begin(),fc.begin(),op);
  24. return fc;
  25. }
  26.  
  27. template <typename Collection,typename Condition>
  28. bool exists(Collection col,Condition con) {
  29. auto exist = std::find_if(col.begin(),col.end(),con);
  30. return exist != col.end();
  31. }
  32.  
  33.  
  34. template <typename Collection,typename Predicate>
  35. Collection filterNot(Collection col,Predicate predicate ) {
  36. auto returnIterator = std::remove_if(col.begin(),col.end(),predicate);
  37. col.erase(returnIterator,std::end(col));
  38. return col;
  39. }
  40.  
  41. template <typename Collection,typename Predicate>
  42. Collection filter(Collection col,Predicate predicate) {
  43. auto fnCol = filterNot(col,[predicate](typename Collection::value_type i) { return !predicate(i);});
  44. return fnCol;
  45. }
  46.  
  47. int main()
  48. {
  49. //helper println
  50. auto println = [](const char *message){ std::cout << message << std::endl;};
  51.  
  52. auto lambda_echo = [](int i ) { std::cout << i << std::endl; };
  53. std::vector<int> col{20,24,37,42,23,45,37};
  54.  
  55. //call foreach to print the vector
  56. println("running foreach");
  57. for_each(col,lambda_echo);
  58.  
  59. // add one to each element
  60. println("running map operation");
  61. auto addOne = [] (int i) { return i+1;};
  62. auto returnCol = map(col,addOne);
  63. for_each(returnCol,lambda_echo);
  64.  
  65. //zip operation
  66. println("running zip operation");
  67. auto zipAdd = [] (int a, int b){ return a+b;};
  68. std::vector<int> secondCol{40,22,22,24,23,45,34};
  69. auto zipCol = zip(col,secondCol,zipAdd);
  70. for_each(zipCol,lambda_echo);
  71.  
  72. println("runnig exists");
  73. //prints 1 if true 0 if false
  74. std::cout << "value 20 exist= " << exists(col, [] (int value){ return value==20;}) << std::endl;
  75. std::cout << "value 43 exist= " << exists(col, [] (int value){ return value==43;}) << std::endl;
  76.  
  77. println("running filterNot");
  78. auto filterCol = filterNot(col,[](int value){ return value==23;});
  79. for_each(filterCol,lambda_echo);
  80.  
  81. println("running filter");
  82. auto filteredCol = filter(col,[](int value){ return value > 30;});
  83. for_each(filteredCol,lambda_echo);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement