Skip to content

Commit 32724f9

Browse files
committed
Fixed #2.
1 parent 3185689 commit 32724f9

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

examples/src/BasicAlgorithms.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ namespace example2
9393
}
9494

9595
///////////////////////////////////////////////////////////////////////////////////////////////////
96-
/// Example 3 - Creating stateful signals (1)
96+
/// Example 3 - Folding event streams into signals (1)
9797
///////////////////////////////////////////////////////////////////////////////////////////////////
9898
namespace example3
9999
{
@@ -119,7 +119,7 @@ namespace example3
119119

120120
void Run()
121121
{
122-
cout << "Example 3 - Creating stateful signals (1)" << endl;
122+
cout << "Example 3 - Folding event streams into signals (1)" << endl;
123123

124124
Counter myCounter;
125125

@@ -135,7 +135,7 @@ namespace example3
135135
}
136136

137137
///////////////////////////////////////////////////////////////////////////////////////////////////
138-
/// Example 4 - Creating stateful signals (2)
138+
/// Example 4 - Folding event streams into signals (2)
139139
///////////////////////////////////////////////////////////////////////////////////////////////////
140140
namespace example4
141141
{
@@ -149,23 +149,36 @@ namespace example4
149149
public:
150150
USING_REACTIVE_DOMAIN(D)
151151

152-
EventSourceT<int> Input = MakeEventSource<D,int>();
152+
EventSourceT<float> Input = MakeEventSource<D,float>();
153153

154-
SignalT<float> Average = Iterate(
154+
SignalT<int> Count = Iterate(
155+
Tokenize(Input),
156+
0,
157+
[] (Token, int oldCount) {
158+
return oldCount + 1;
159+
});
160+
161+
SignalT<float> Sum = Iterate(
155162
Input,
156163
0.0f,
157-
[] (int sample, float oldAvg) {
158-
return (oldAvg + sample) / 2.0f;
164+
[] (float v, float sum) {
165+
return v + sum;
166+
});
167+
168+
SignalT<float> Average = MakeSignal(
169+
With(Sum,Count),
170+
[] (float sum, int count) {
171+
return count != 0 ? sum / count : 0.0f;
159172
});
160173
};
161174

162175
void Run()
163176
{
164-
cout << "Example 4 - Creating stateful signals (2)" << endl;
177+
cout << "Example 4 - Folding event streams into signals (2)" << endl;
165178

166179
Sensor mySensor;
167180

168-
mySensor.Input << 10 << 5 << 10 << 8;
181+
mySensor.Input << 10.0f << 5.0f << 10.0f << 8.0f;
169182

170183
cout << "Average: " << mySensor.Average() << endl; // output: 3
171184

@@ -174,7 +187,7 @@ namespace example4
174187
}
175188

176189
///////////////////////////////////////////////////////////////////////////////////////////////////
177-
/// Example 5 - Creating stateful signals (3)
190+
/// Example 5 - Folding event streams into signals (3)
178191
///////////////////////////////////////////////////////////////////////////////////////////////////
179192
namespace example5
180193
{
@@ -210,7 +223,7 @@ namespace example5
210223

211224
void Run()
212225
{
213-
cout << "Example 5 - Creating stateful signals (3)" << endl;
226+
cout << "Example 5 - Folding event streams into signals (3)" << endl;
214227

215228
Counter myCounter;
216229

0 commit comments

Comments
 (0)