Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <functional>
- #include <numeric>
- #include <type_traits>
- template <typename IterType, typename Func>
- class MapperIterator {
- public:
- using ValueType = decltype(std::declval<Func>()(*std::declval<IterType>()));
- MapperIterator(IterType iter, Func f) : iter_(iter), func_(f) {};
- ValueType operator*() const { return func_(*iter_); }
- MapperIterator& operator++() {
- ++iter_;
- return *this;
- }
- ValueType* operator->() const {
- return new ValueType(func_(*iter_));;
- }
- bool operator!=(const MapperIterator& other) const { return iter_ != other.iter_; }
- bool operator==(const MapperIterator& other) const { return iter_ == other.iter_; }
- private:
- IterType iter_;
- Func func_;
- };
- template<typename IterType, typename Func>
- MapperIterator<IterType, Func> MakeMapperIterator(IterType iter, Func f) {
- return MapperIterator<IterType, Func>(iter, f);
- }
- template<typename IterType, typename Func>
- class MapClass {
- public:
- MapClass(IterType begin, IterType end, Func f) : begin_(MakeMapperIterator(begin, f)), end_(MakeMapperIterator(end, f)) {}
- auto begin() const { return begin_; }
- auto end() const { return end_; }
- private:
- MapperIterator<IterType, Func> begin_;
- MapperIterator<IterType, Func> end_;
- };
- template<typename IterType, typename Func>
- MapClass<IterType, Func> Map(IterType begin, IterType end, Func f) {
- return MapClass<IterType, Func>(begin, end, f);
- }
- template<typename Container, typename Func>
- auto Map(const MapClass<Container, Func>& mapResult, Func f) {
- return Map(mapResult.begin(), mapResult.end(), f);
- }
- class Foo {
- public:
- Foo(const int& number) : number_(number) {}
- void foo() const {
- std::cout << number_ << " from Foo. ";
- }
- private:
- const int& number_;
- };
- int main() {
- const std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8};
- std::cout << "Numbers:\n";
- for (const int& number : numbers) {
- std::cout << number << " ";
- }
- std::cout << std::endl;
- {
- std::cout << "Doubled:\n";
- std::function<int(const int &)> f = [](const int &x) { return x * 2; };
- for (const int &number : Map(numbers.begin(), numbers.end(), f)) {
- std::cout << number << " ";
- }
- std::cout << std::endl;
- }
- {
- std::cout << "Powers of 2:\n";
- std::function<int(const int &)> f = [](const int &x) { return std::pow(2, x); };
- for (auto it = MakeMapperIterator(numbers.begin(), f); it != MakeMapperIterator(numbers.end(), f); ++it) {
- std::cout << *it << " ";
- }
- std::cout << std::endl;
- }
- {
- std::cout << "Foofied:\n";
- std::function<Foo(const int &)> f = [](const int &x) { return Foo(x); };
- for (auto it = MakeMapperIterator(numbers.begin(), f); it != MakeMapperIterator(numbers.end(), f); ++it) {
- it->foo();
- }
- std::cout << std::endl;
- }
- {
- std::cout << "Double mapped:\n";
- std::function<int(const int &)> f = [](const int &x) { return x * 2; };
- std::function<int(const int &)> g = [](const int &x) { return x + 15; };
- for (const int &number : Map(Map(numbers.begin(), numbers.end(), f), g)) {
- std::cout << number << " ";
- }
- std::cout << std::endl;
- }
- {
- std::cout << "Sum of doubled:\n";
- std::function<int(const int &)> f = [](const int &x) { return x * 2; };
- std::cout << std::accumulate(MakeMapperIterator(numbers.begin(), f), MakeMapperIterator(numbers.end(), f), 0) << std::endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment