Created
January 5, 2020 20:19
-
-
Save dreum/0cb53e6900b97dfd6668637c6602d426 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <initializer_list> | |
#include <range/v3/view/cycle.hpp> | |
#include <optional> | |
#include <range/v3/view/zip_with.hpp> | |
#include <string> | |
#include <iostream> | |
#include <range/v3/algorithm/for_each.hpp> | |
#include <range/v3/view/iota.hpp> | |
#include <range/v3/view/transform.hpp> | |
using std::vector; | |
using std::nullopt; | |
using std::optional; | |
using std::make_optional; | |
using std::string; | |
template<class A> | |
optional<A> operator+(optional<A> a, optional<A> b) | |
{ | |
if(a) | |
{ | |
if(b) | |
return a.value() + b.value(); | |
else | |
return a; | |
} | |
else | |
return b; | |
} | |
int main() | |
{ | |
namespace rv = ranges::views; | |
vector<optional<string>> fizz = {nullopt, nullopt, make_optional("fizz")}; | |
auto fizz_cycle = fizz | rv::cycle; | |
vector<optional<string>> buzz = {nullopt, nullopt, nullopt, nullopt, make_optional("buzz")}; | |
auto buzz_cycle = buzz | rv::cycle; | |
auto fizzbuzz = rv::zip_with([] (auto a, auto b) { return a+b; }, | |
fizz_cycle, buzz_cycle); | |
auto numbers = rv::iota(1,101) | rv::transform([](auto i) { return std::to_string(i); }); | |
auto elements = rv::zip_with([] (auto n, auto fb) { return fb.value_or(n); }, | |
numbers, fizzbuzz); | |
ranges::for_each(elements, [](auto elem){ std::cout << elem << '\n'; }); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment