-
Notifications
You must be signed in to change notification settings - Fork 2
/
designpush.h
93 lines (79 loc) · 1.95 KB
/
designpush.h
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
#pragma once
namespace designpushdef
{
/*
template<class V>
struct receiver
{
void operator()(V v);
};
template<class SenderV>
struct algorithm
{
SenderU operator()(SenderV s);
};
template<class Receiver>
struct sender
{
void operator()(Receiver r);
};
*/
template<class Next>
struct receiver
{
Next i;
template<class V>
void operator()(V&& v) const {
return i(std::forward<V>(v));
}
};
template<class Next>
auto make_receiver(Next i) {
return receiver<std::decay_t<Next>>{i};
}
template<class T>
struct receiver_check : public false_type {};
template<class Next>
struct receiver_check<receiver<Next>> : public true_type {};
template<class T>
using for_receiver = typename enable_if<receiver_check<T>::value>::type;
template<class T>
using not_receiver = typename enable_if<!receiver_check<T>::value>::type;
const auto ints = [](auto first, auto last){
return [=](auto r){
for(auto i=first;i <= last; ++i){
r(i);
}
};
};
const auto copy_if = [](auto pred){
return [=](auto dest){
return [=](auto v){
if (pred(v)) dest(v);
};
};
};
const auto printto = [](auto& output){
return make_receiver([&](auto v) {
output << v << endl;
});
};
template<class SenderV, class SenderU, class CheckS = not_receiver<SenderU>>
auto operator|(SenderV sv, SenderU su){
return [=](auto dest){
return sv(su(dest));
};
}
template<class SenderV, class ReceiverV, class CheckS = not_receiver<SenderV>, class CheckR = for_receiver<ReceiverV>>
auto operator|(SenderV sv, ReceiverV rv) {
return sv(rv);
}
}
extern"C" void EMSCRIPTEN_KEEPALIVE designpush(int first, int last){
using namespace designpushdef;
ints(first, last)(designpushdef::copy_if(even)(printto(cout)));
}
extern"C" void EMSCRIPTEN_KEEPALIVE designoperator(int first, int last){
using namespace designpushdef;
ints(first, last) | designpushdef::copy_if(even) | printto(cout);
}