-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream-1.cc
47 lines (42 loc) · 1.25 KB
/
stream-1.cc
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
#include <string>
#include "eventuals/loop.h"
#include "eventuals/promisify.h"
#include "eventuals/stream.h"
using namespace eventuals;
int main(int argc, char** argv) {
auto e = []() {
return Stream<int>()
.context(/* count = */ 100)
.next([](auto& count, auto& k) {
if (count-- > 0) {
k.Emit(1);
} else {
k.Ended();
}
})
.done([](auto& count, auto& k) {
k.Ended();
})
>> Loop<int>()
.context(/* sum = */ 0)
.body([](auto& sum, auto& stream, auto&& value) {
if (sum < 42) {
sum += value;
stream.Next();
} else {
stream.Done();
}
})
.ended([](auto& sum, auto& k) {
k.Start(sum);
})
.fail([](auto& sum, auto& k, auto&& e) {
k.Fail(std::forward<decltype(e)>(e)); // Propagate error.
})
.stop([](auto& sum, auto& k) {
k.Stop(); // Propagate stop.
});
};
CHECK_EQ(42, *e());
return 0;
}