FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cpp.react/examples/src/BasicAlgorithms.cpp at master · zengyuxing007/cpp.react · GitHub
zengyuxing007
/
cpp.react
Public
forked from
snakster/cpp.react
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
cpp.react
/
examples
/
src
/
BasicAlgorithms.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
323 lines (248 loc) · 8.29 KB
Breadcrumbs
cpp.react
/
examples
/
src
/
BasicAlgorithms.cpp
Copy path
File metadata and controls
323 lines (248 loc) · 8.29 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//
Copyright Sebastian Jeckel 2014.
//
Distributed under the Boost Software License, Version 1.0.
//
(See accompanying file LICENSE_1_0.txt or copy at
//
http://www.boost.org/LICENSE_1_0.txt)
#
include
<
iostream
>
#
include
<
string
>
#
include
<
utility
>
#
include
<
vector
>
#
include
"
react/Domain.h
"
#
include
"
react/Signal.h
"
#
include
"
react/Event.h
"
#
include
"
react/Observer.h
"
#
include
"
react/Algorithm.h
"
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ Example 1 - Converting events to signals
//
/////////////////////////////////////////////////////////////////////////////////////////////////
namespace
example1
{
using
namespace
std
;
using
namespace
react
;
REACTIVE_DOMAIN
(D, sequential)
class
Sensor
{
public:
USING_REACTIVE_DOMAIN
(D)
EventSourceT<
int
> Samples = MakeEventSource<D,
int
>();
SignalT<
int
> LastSample = Hold(Samples,
0
);
};
void
Run
()
{
cout <<
"
Example 1 - Converting events to signals
"
<< endl;
Sensor mySensor;
Observe
(mySensor.
LastSample
, [] (
int
v) {
std::cout << v << std::endl;
});
mySensor.
Samples
<<
20
<<
21
<<
21
<<
22
;
//
output: 20, 21, 22
DoTransaction<D>([&] {
mySensor.
Samples
<<
30
<<
31
<<
31
<<
32
;
});
//
output: 32
cout << endl;
}
}
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ Example 2 - Converting signals to events
//
/////////////////////////////////////////////////////////////////////////////////////////////////
namespace
example2
{
using
namespace
std
;
using
namespace
react
;
REACTIVE_DOMAIN
(D, sequential)
class
Employee
{
public:
USING_REACTIVE_DOMAIN
(D)
VarSignalT<string> Name = MakeVar<D>(string(
"
Bob
"
));
VarSignalT<
int
> Salary = MakeVar<D>(
3000
);
EventsT<
int
> SalaryChanged = Monitor(Salary);
};
void
Run
()
{
cout <<
"
Example 2 - Converting signals to events
"
<< endl;
Employee bob;
Observe
(
bob.
SalaryChanged
,
With
(bob.
Name
),
[] (
int
newSalary,
const
string& name) {
cout << name <<
"
now earns
"
<< newSalary << endl;
});
cout << endl;
}
}
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ Example 3 - Folding event streams into signals (1)
//
/////////////////////////////////////////////////////////////////////////////////////////////////
namespace
example3
{
using
namespace
std
;
using
namespace
react
;
REACTIVE_DOMAIN
(D, sequential)
class
Counter
{
public:
USING_REACTIVE_DOMAIN
(D)
EventSourceT<> Increment = MakeEventSource<D>();
SignalT<
int
> Count = Iterate(
Increment,
0
,
[] (Token,
int
oldCount) {
return
oldCount +
1
;
});
};
void
Run
()
{
cout <<
"
Example 3 - Folding event streams into signals (1)
"
<< endl;
Counter myCounter;
//
Note: Using function-style operator() instead of .Emit() and .Value()
myCounter.
Increment
();
myCounter.
Increment
();
myCounter.
Increment
();
cout << myCounter.
Count
() << endl;
//
output: 3
cout << endl;
}
}
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ Example 4 - Folding event streams into signals (2)
//
/////////////////////////////////////////////////////////////////////////////////////////////////
namespace
example4
{
using
namespace
std
;
using
namespace
react
;
REACTIVE_DOMAIN
(D, sequential)
class
Sensor
{
public:
USING_REACTIVE_DOMAIN
(D)
EventSourceT<
float
> Input = MakeEventSource<D,
float
>();
SignalT<
int
> Count = Iterate(
Tokenize
(Input),
0
,
[] (Token,
int
oldCount) {
return
oldCount +
1
;
});
SignalT<
float
> Sum = Iterate(
Input,
0
.
0f
,
[] (
float
v,
float
sum) {
return
v + sum;
});
SignalT<
float
> Average = MakeSignal(
With
(Sum,Count),
[] (
float
sum,
int
count) {
return
count !=
0
? sum / count :
0
.
0f
;
});
};
void
Run
()
{
cout <<
"
Example 4 - Folding event streams into signals (2)
"
<< endl;
Sensor mySensor;
mySensor.
Input
<<
10
.
0f
<<
5
.
0f
<<
10
.
0f
<<
8
.
0f
;
cout <<
"
Average:
"
<< mySensor.
Average
() << endl;
//
output: 8.25
cout << endl;
}
}
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ Example 5 - Folding event streams into signals (3)
//
/////////////////////////////////////////////////////////////////////////////////////////////////
namespace
example5
{
using
namespace
std
;
using
namespace
react
;
REACTIVE_DOMAIN
(D, sequential)
enum
ECmd { increment, decrement, reset };
class
Counter
{
public:
USING_REACTIVE_DOMAIN
(D)
EventSourceT<
int
> Update = MakeEventSource<D,
int
>();
VarSignalT<
int
> Delta = MakeVar<D>(
1
);
VarSignalT<
int
> Start = MakeVar<D>(
0
);
SignalT<
int
> Count = Iterate(
Update,
Start.Value(),
With
(Delta, Start),
[] (
int
cmd,
int
oldCount,
int
delta,
int
start) {
if
(cmd == increment)
return
oldCount + delta;
else
if
(cmd == decrement)
return
oldCount - delta;
else
return
start;
});
};
void
Run
()
{
cout <<
"
Example 5 - Folding event streams into signals (3)
"
<< endl;
Counter myCounter;
cout <<
"
Start:
"
<< myCounter.
Count
() << endl;
//
output: 0
myCounter.
Update
(increment);
myCounter.
Update
(increment);
myCounter.
Update
(increment);
cout <<
"
3x increment by 1:
"
<< myCounter.
Count
() << endl;
//
output: 3
myCounter.
Delta
<<=
5
;
myCounter.
Update
(decrement);
cout <<
"
1x decrement by 5:
"
<< myCounter.
Count
() << endl;
//
output: -2
myCounter.
Start
<<=
100
;
myCounter.
Update
(reset);
cout <<
"
reset to 100:
"
<< myCounter.
Count
() << endl;
//
output: 100
cout << endl;
}
}
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ Example 6 - Avoiding expensive copies
//
/////////////////////////////////////////////////////////////////////////////////////////////////
namespace
example6
{
using
namespace
std
;
using
namespace
react
;
REACTIVE_DOMAIN
(D, sequential)
class
Sensor
{
public:
USING_REACTIVE_DOMAIN
(D)
EventSourceT<
int
> Input = MakeEventSource<D,
int
>();
VarSignalT<
int
> Threshold = MakeVar<D>(
42
);
SignalT<vector<
int
>> AllSamples = Iterate(
Input,
vector<
int
>{},
[] (
int
input, vector<
int
>& all) {
all.
push_back
(input);
});
SignalT<vector<
int
>> CriticalSamples = Iterate(
Input,
vector<
int
>{},
With
(Threshold),
[] (
int
input, vector<
int
>& critical,
int
threshold) {
if
(input > threshold)
critical.
push_back
(input);
});
};
void
Run
()
{
cout <<
"
Example 6 - Avoiding expensive copies
"
<< endl;
Sensor mySensor;
mySensor.
Input
<<
40
<<
29
<<
43
<<
50
;
cout <<
"
All samples:
"
;
for
(
auto
const
& v : mySensor.
AllSamples
())
cout << v <<
"
"
;
cout << endl;
cout <<
"
Critical samples:
"
;
for
(
auto
const
& v : mySensor.
CriticalSamples
())
cout << v <<
"
"
;
cout << endl;
cout << endl;
}
}
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
/ Run examples
//
/////////////////////////////////////////////////////////////////////////////////////////////////
int
main
()
{
example1::Run
();
example2::Run
();
example3::Run
();
example4::Run
();
example5::Run
();
example6::Run
();
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL