-
Notifications
You must be signed in to change notification settings - Fork 3
/
hello-world.cpp
48 lines (45 loc) · 1.43 KB
/
hello-world.cpp
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
#include <iostream>
#include "experimental/async.hpp"
using awacorn::async;
using awacorn::asyncfn;
using awacorn::context;
using awacorn::promise;
template <typename Ctx>
typename asyncfn<Ctx>::template expr<promise<void>> print(
const typename asyncfn<Ctx>::template expr<std::string>& v) {
return typename asyncfn<Ctx>::template expr<promise<void>>(
[v](context<Ctx>& ctx) {
promise<promise<void>> x;
v.apply(ctx).then([x](const std::string& v) {
std::cout << v;
x.resolve(awacorn::resolve());
});
return x;
});
}
template <typename Ctx>
typename asyncfn<Ctx>::template expr<promise<std::string>> input() {
return typename asyncfn<Ctx>::template expr<promise<std::string>>(
[](context<Ctx>&) {
promise<promise<std::string>> x;
std::string str;
std::getline(std::cin, str);
x.resolve(awacorn::resolve(std::move(str)));
return x;
});
}
template <typename T>
using expr = asyncfn<void>::expr<T>;
int main() {
async([](asyncfn<void>& v) {
auto a = v.var<std::string>("a");
v << (a = std::string());
v << (print<void>("Hello World!\n"));
v << (print<void>("Please input a string: "));
v << (a = v.await(input<void>()));
v << v.await(print<void>("Your input is: " + a + "\n"));
v << v.cond(a == "awa", v.await(print<void>("OK\n")),
v.await(print<void>("Fail\n")));
v << v.ret();
});
}