-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheither_example.cpp
More file actions
77 lines (57 loc) · 1.88 KB
/
Copy patheither_example.cpp
File metadata and controls
77 lines (57 loc) · 1.88 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <functional>
#include <iostream>
#include <optional>
#include "include/EitherMonad.h"
#define WATCH(x) std::cout << (#x) << ": " << x << '\n'
APPLICATION_ERROR(AttemptToDivideByZero, "Attempt to divide by zero", 1, GET_EXIT::True)
APPLICATION_ERROR(BadIndexScenario,"Access not existing index",2, GET_EXIT::True)
APPLICATION_ERROR(TestError,"TEST",3, GET_EXIT::False)
struct Scenario
{
std::string txt;
std::string toStr() { return txt; }
};
template <typename Stream>
Stream& operator<<(Stream& ss, Scenario obj) { return ss << obj.toStr(); }
static Either<Error, Scenario> get_scenario(size_t sc) {
std::vector<Scenario> x {
{ "jedna" },
{ "dva" },
{ "tri" }
};
if (sc < x.size()) { return x[sc]; }
return BadIndexScenario()
.updateDDate(
"Bad index: " + std::to_string(sc) + " is not in range [0, "
+ std::to_string(x.size() - 1) + "]\n"
+ "\tfrom: " + __FILE__ + '(' + std::to_string(__LINE__) + ") " + __FUNCTION__ + "\n"
);
}
static Either<Error, std::string> fn(Scenario s)
{
return "!!!" + s.toStr() + '\n';
}
static Either<Error, int> get_test_error()
{
return TestError();
}
int main(void)
{
std::cout << ((Do ( 0 ) >>= get_scenario ) >>= fn );
std::cout << ((Do ( 1 ) >>= get_scenario ) >>= fn );
std::cout << ((Do ( 4 ) >>= get_scenario ) >>= fn );
std::cout << ( DoubleDo( 2, get_scenario ) ) << "\n\n";
std::cout << DoubleDo( 5, get_scenario ) << "\n\n";
//std::cout << _Do(5, get_scenario, fn) << '\n';
error_handle(std::cout, *fromLeft(get_test_error()));
error_handle(std::cout, *fromLeft((Do ( 3 ) >>= get_scenario ) >>= fn ));
std::vector<Either<Error, std::string>> es = {
((Do ( 4 ) >>= get_scenario ) >>= fn ),
((Do ( 2 ) >>= get_scenario ) >>= fn ),
((Do ( 3 ) >>= get_scenario ) >>= fn ),
((Do ( 2 ) >>= get_scenario ) >>= fn )
};
auto lfs = lefts(es);
if (lfs.size()) { for (auto er : lfs) { } return 0; }
return 0;
}