-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzane_like.cpp
More file actions
50 lines (42 loc) · 1.11 KB
/
zane_like.cpp
File metadata and controls
50 lines (42 loc) · 1.11 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
#include <iostream>
#include <string_view>
#include <zane-cpp.hpp>
namespace demo {
struct Engine {
int rpm = 0;
};
class Car {
public:
zane::ref<Engine> engine;
};
struct parse_error {
std::string_view message;
};
auto parse_scale(std::string_view text) -> zane::abortable<int, parse_error> {
if (text == "high") {
return zane::abortable<int, parse_error>::success(3);
}
return zane::abortable<int, parse_error>::abort({"expected \"high\""});
}
auto scaled_rpm(Car car, std::string_view text) -> int {
return parse_scale(text)
| zane::pipe([&](auto result) {
return zane::then(std::move(result), [&](int scale) {
return car.engine->rpm * scale;
});
})
| zane::pipe([](auto result) {
return zane::handle(std::move(result), [](const parse_error&) {
return 0;
});
});
}
} // namespace demo
int main() {
demo::Engine engine{900};
demo::Car car{engine};
auto lanes = zane::array(1, 2, 3);
std::cout << "scaled rpm: " << demo::scaled_rpm(car, "high") << '\n';
std::cout << "fallback rpm: " << demo::scaled_rpm(car, "low") << '\n';
std::cout << "array lanes: " << lanes.size() << '\n';
}