-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperhaps.t.cpp
More file actions
37 lines (26 loc) · 923 Bytes
/
perhaps.t.cpp
File metadata and controls
37 lines (26 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "perhaps.hpp"
#include <iostream>
#include <string>
template <class M> auto join(M&& m) {
return m.and_then([](auto&& x) { return x; });
}
perhaps<int> operator+(const perhaps<int>& x, const perhaps<int>& y) {
return x.and_then([&](int x) { return y.and_then([&](int y) { return perhaps<int>(x + y); }); });
}
struct mile {
operator int() const {return x;}
int x = 3;
};
int main() {
using namespace std::string_literals;
perhaps<int> x = 10;
perhaps<int> y = 20;
x.transform([](int x) { return (x == 10) ? "success"s : "fail"s; })
.and_then([](std::string const& r) { std::cout << r << std::endl; });
(x + y).and_then([](int r) { std::cout << r << std::endl; });
std::cout << join(x + x + y) << std::endl;
perhaps<mile> z;
mile q;
z.or_else([](mile r) {return perhaps<mile>{};}).value_or(q);
std::cout << z.value_or(mile{5}) << std::endl;
}