-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathyield.cpp
More file actions
31 lines (24 loc) · 715 Bytes
/
Copy pathyield.cpp
File metadata and controls
31 lines (24 loc) · 715 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
#include "corofx/task.hpp"
#include <iostream>
#include <vector>
using namespace corofx;
// Example adapted from https://koka-lang.github.io/koka/doc/book.html#why-handlers.
struct yield {
using return_type = bool;
int i{};
};
auto traverse(std::vector<int> xs) -> task<void, yield> {
for (auto x : xs) {
if (not co_await yield{x}) break;
}
co_return {};
}
auto print_elems() -> task<void> {
co_await traverse(std::vector{1, 2, 3, 4})
.with(handler_of<yield>([](auto&& e, auto&& resume) -> task<void> {
std::cout << "yielded " << e.i << "\n";
co_return resume(e.i <= 2);
}));
co_return {};
}
auto main() -> int { print_elems()(); }