fedor-resh

Untitled

Dec 23rd, 2023
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <functional>
  5. #include <numeric>
  6. #include <type_traits>
  7.  
  8. template <typename IterType, typename Func>
  9. class MapperIterator {
  10. public:
  11.     using ValueType = decltype(std::declval<Func>()(*std::declval<IterType>()));
  12.  
  13.     MapperIterator(IterType iter, Func f) : iter_(iter), func_(f) {};
  14.  
  15.     ValueType operator*() const { return func_(*iter_); }
  16.  
  17.     MapperIterator& operator++() {
  18.         ++iter_;
  19.         return *this;
  20.     }
  21.  
  22.     ValueType* operator->() const {
  23.         return new ValueType(func_(*iter_));;
  24.     }
  25.  
  26.     bool operator!=(const MapperIterator& other) const { return iter_ != other.iter_; }
  27.     bool operator==(const MapperIterator& other) const { return iter_ == other.iter_; }
  28.  
  29. private:
  30.     IterType iter_;
  31.     Func func_;
  32. };
  33.  
  34. template<typename IterType, typename Func>
  35. MapperIterator<IterType, Func> MakeMapperIterator(IterType iter, Func f) {
  36.     return MapperIterator<IterType, Func>(iter, f);
  37. }
  38.  
  39. template<typename IterType, typename Func>
  40. class MapClass {
  41. public:
  42.     MapClass(IterType begin, IterType end, Func f) : begin_(MakeMapperIterator(begin, f)), end_(MakeMapperIterator(end, f)) {}
  43.  
  44.     auto begin() const { return begin_; }
  45.     auto end() const { return end_; }
  46.  
  47. private:
  48.     MapperIterator<IterType, Func> begin_;
  49.     MapperIterator<IterType, Func> end_;
  50. };
  51.  
  52. template<typename IterType, typename Func>
  53. MapClass<IterType, Func> Map(IterType begin, IterType end, Func f) {
  54.     return MapClass<IterType, Func>(begin, end, f);
  55. }
  56.  
  57. template<typename Container, typename Func>
  58. auto Map(const MapClass<Container, Func>& mapResult, Func f) {
  59.     return Map(mapResult.begin(), mapResult.end(), f);
  60. }
  61.  
  62. class Foo {
  63. public:
  64.     Foo(const int& number) : number_(number) {}
  65.     void foo() const {
  66.         std::cout << number_ << " from Foo. ";
  67.     }
  68. private:
  69.     const int& number_;
  70. };
  71.  
  72. int main() {
  73.     const std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8};
  74.  
  75.     std::cout << "Numbers:\n";
  76.     for (const int& number : numbers) {
  77.         std::cout << number << " ";
  78.     }
  79.     std::cout << std::endl;
  80.  
  81.     {
  82.         std::cout << "Doubled:\n";
  83.         std::function<int(const int &)> f = [](const int &x) { return x * 2; };
  84.         for (const int &number : Map(numbers.begin(), numbers.end(), f)) {
  85.             std::cout << number << " ";
  86.         }
  87.         std::cout << std::endl;
  88.     }
  89.  
  90.     {
  91.         std::cout << "Powers of 2:\n";
  92.         std::function<int(const int &)> f = [](const int &x) { return std::pow(2, x); };
  93.         for (auto it = MakeMapperIterator(numbers.begin(), f); it != MakeMapperIterator(numbers.end(), f); ++it) {
  94.             std::cout << *it << " ";
  95.         }
  96.         std::cout << std::endl;
  97.     }
  98.  
  99.     {
  100.         std::cout << "Foofied:\n";
  101.         std::function<Foo(const int &)> f = [](const int &x) { return Foo(x); };
  102.         for (auto it = MakeMapperIterator(numbers.begin(), f); it != MakeMapperIterator(numbers.end(), f); ++it) {
  103.             it->foo();
  104.         }
  105.         std::cout << std::endl;
  106.     }
  107.  
  108.     {
  109.         std::cout << "Double mapped:\n";
  110.         std::function<int(const int &)> f = [](const int &x) { return x * 2; };
  111.         std::function<int(const int &)> g = [](const int &x) { return x + 15; };
  112.         for (const int &number : Map(Map(numbers.begin(), numbers.end(), f), g)) {
  113.             std::cout << number << " ";
  114.         }
  115.         std::cout << std::endl;
  116.     }
  117.  
  118.     {
  119.         std::cout << "Sum of doubled:\n";
  120.         std::function<int(const int &)> f = [](const int &x) { return x * 2; };
  121.         std::cout << std::accumulate(MakeMapperIterator(numbers.begin(), f), MakeMapperIterator(numbers.end(), f), 0) << std::endl;
  122.     }
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment