@@ -93,7 +93,7 @@ namespace example2
93
93
}
94
94
95
95
// /////////////////////////////////////////////////////////////////////////////////////////////////
96
- // / Example 3 - Creating stateful signals (1)
96
+ // / Example 3 - Folding event streams into signals (1)
97
97
// /////////////////////////////////////////////////////////////////////////////////////////////////
98
98
namespace example3
99
99
{
@@ -119,7 +119,7 @@ namespace example3
119
119
120
120
void Run ()
121
121
{
122
- cout << " Example 3 - Creating stateful signals (1)" << endl;
122
+ cout << " Example 3 - Folding event streams into signals (1)" << endl;
123
123
124
124
Counter myCounter;
125
125
@@ -135,7 +135,7 @@ namespace example3
135
135
}
136
136
137
137
// /////////////////////////////////////////////////////////////////////////////////////////////////
138
- // / Example 4 - Creating stateful signals (2)
138
+ // / Example 4 - Folding event streams into signals (2)
139
139
// /////////////////////////////////////////////////////////////////////////////////////////////////
140
140
namespace example4
141
141
{
@@ -149,23 +149,36 @@ namespace example4
149
149
public:
150
150
USING_REACTIVE_DOMAIN (D)
151
151
152
- EventSourceT<int > Input = MakeEventSource<D,int >();
152
+ EventSourceT<float > Input = MakeEventSource<D,float >();
153
153
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(
155
162
Input,
156
163
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 ;
159
172
});
160
173
};
161
174
162
175
void Run ()
163
176
{
164
- cout << " Example 4 - Creating stateful signals (2)" << endl;
177
+ cout << " Example 4 - Folding event streams into signals (2)" << endl;
165
178
166
179
Sensor mySensor;
167
180
168
- mySensor.Input << 10 << 5 << 10 << 8 ;
181
+ mySensor.Input << 10 . 0f << 5 . 0f << 10 . 0f << 8 . 0f ;
169
182
170
183
cout << " Average: " << mySensor.Average () << endl; // output: 3
171
184
@@ -174,7 +187,7 @@ namespace example4
174
187
}
175
188
176
189
// /////////////////////////////////////////////////////////////////////////////////////////////////
177
- // / Example 5 - Creating stateful signals (3)
190
+ // / Example 5 - Folding event streams into signals (3)
178
191
// /////////////////////////////////////////////////////////////////////////////////////////////////
179
192
namespace example5
180
193
{
@@ -210,7 +223,7 @@ namespace example5
210
223
211
224
void Run ()
212
225
{
213
- cout << " Example 5 - Creating stateful signals (3)" << endl;
226
+ cout << " Example 5 - Folding event streams into signals (3)" << endl;
214
227
215
228
Counter myCounter;
216
229
0 commit comments