-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
175 lines (142 loc) · 4.43 KB
/
main.cpp
File metadata and controls
175 lines (142 loc) · 4.43 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
#define INFINITY_NO_EXCEPTIONS
// #define INFINITY_LANGUAGE_STD_CPP14
#define INFINITY_LANGUAGE_STD_CPP23
#ifdef INFINITY_LANGUAGE_STD_CPP14
#include "include/infinity/option.hpp"
#include "include/infinity/result.hpp"
Infinity::Option<int> maybe_add_one(Infinity::Option<int> x) {
return x.map([](int v) { return v + 1; });
}
Infinity::Result<int, std::string> parse_int(const std::string& s) {
try {
int v = std::stoi(s);
return Infinity::Result<int, std::string>::ok(v);
} catch (...) {
return Infinity::Result<int, std::string>::err("parse error");
}
}
int main() {
using namespace Infinity;
Option<int> a = Some(5);
Option<int> b = None;
std::cout << a.unwrap() << "\n";
std::cout << "B: " << b.unwrap() << "\n";
auto o1 = maybe_add_one(a);
auto o2 = maybe_add_one(b);
std::cout << o1.unwrap() << "\n";
std::cout << o2.unwrap() << "\n";
std::cout << o2.unwrap_or(23) << "\n";
auto r1 = parse_int("42");
auto r2 = parse_int("not a number");
if (r1.is_ok()) {
std::cout << "r1: " << r1.unwrap() << "\n";
}
if (r2.is_err()) {
std::cout << "r2 error: " << r2.unwrap_err() << "\n";
}
}
#elifdef INFINITY_LANGUAGE_STD_CPP23
#include "include/infinity/match.hpp"
#include "include/infinity/option.hpp"
#include "include/infinity/result.hpp"
#include "include/infinity/vec.hpp"
using namespace Infinity;
Option<int> get_number(bool give) { return give ? Some(123) : None; }
Result<int, std::string> compute(bool ok) {
return ok ? Result<int, std::string>(999) : Result<int, std::string>("Bad!");
}
int main() {
// Basic MATCH tests
Option<int> value = Some(10);
MATCH(value, {
SomeCase(val) {
std::cout << "Got Some with value: " << val << "\n";
break;
}
NoneCase {
std::cout << "Got None\n";
break;
}
});
Result<int, std::string> result = Result<int, std::string>(42);
MATCH(result,
{
OkCase(v) {
std::cout << "Ok: " << v << "\n";
break;
}
ErrCase(e) {
std::cout << "Err: " << e << "\n";
break;
}
});
MATCH(get_number(true),
{
SomeCase(v) { std::cout << v << "\n"; break;
}
NoneCase {
std::cout << "none\n";
break;
}
});
MATCH(compute(false),
{
OkCase(v) { std::cout << v << "\n"; break;
}
ErrCase(e) {
std::cout << e << "\n";
break;
}
});
// Option combinators: map, and_then, unwrap_or
Option<int> o_some = Some(5);
Option<int> o_none = None;
auto o_mapped = o_some.map([](int x) { return x * 2; });
std::cout << "Option map(Some 5 -> 10): " << o_mapped.unwrap() << "\n";
std::cout << "Option unwrap_or(None -> 123): " << o_none.unwrap_or(123) << "\n";
// Result combinators: map
Result<int, std::string> r_ok(7);
Result<int, std::string> r_err(std::string("oops"));
auto r_mapped = r_ok.map([](int x) { return x + 1; });
std::cout << "Result map(Ok 7 -> Ok 8): " << r_mapped.unwrap() << "\n";
std::cout << "Result unwrap_err(Err oops): " << r_err.unwrap_err() << "\n";
// collect_results: turn vector<Result<int, E>> into Result<vector<int>, E>
std::vector<Result<int, std::string> > many_ok =
vec_of(Result<int, std::string>(1), Result<int, std::string>(2), Result<int, std::string>(3));
auto collected_ok = collect_results<int, std::string>(many_ok);
std::cout << "collect_results all Ok: ";
for (auto v: collected_ok.unwrap()) std::cout << v << ' ';
std::cout << "\n";
std::vector<Result<int, std::string> > some_err =
vec_of(Result<int, std::string>(1), Result<int, std::string>(std::string("bad")), Result<int, std::string>(3));
auto collected_err = collect_results<int, std::string>(some_err);
std::cout << "collect_results hits Err: " << collected_err.unwrap_err() << "\n";
// vec helpers
auto v = vec_of(std::string("a"), std::string("b"), std::string("c"));
std::cout << "vec_of strings size: " << v.size() << "\n";
auto rep = vec_repeat(42, 5);
std::cout << "vec_repeat(42,5) size: " << rep.size() << ", last: " << rep.back() << "\n";
// MATCH over Option None
Option<int> a_none = None;
MATCH(a_none, {
SomeCase(v) { std::cout << "Unexpected Some: " << v << "\n"; break;
}
NoneCase {
std::cout << "Match on None works\n";
break;
}
});
// MATCH over Result Err
Result<int, std::string> a_err(std::string("E"));
MATCH(a_err, {
OkCase(v) { std::cout << "Unexpected Ok: " << v << "\n"; break;
}
ErrCase(e) {
std::cout << "Match on Err: " << e << "\n";
break;
}
});
return 0;
}
#endif