Skip to content

Commit 0164a96

Browse files
committed
Refactor and update examples: remove deprecated operators, enhance usage of Select and Reduce, and add new example files for better clarity and organization
1 parent be33eab commit 0164a96

File tree

14 files changed

+7
-148
lines changed

14 files changed

+7
-148
lines changed

EXTENSIONS.md

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,6 @@ This document provides comprehensive documentation for all the new reactive exte
44

55
## 🔧 Advanced Operators
66

7-
### OperatorSelect
8-
Transforms each emitted value using a provided function.
9-
10-
**Signature:**
11-
```c++
12-
template<typename Tdest> auto Select(Tdest (*selector)(T)) -> OperatorSelect<T, Tdest>&
13-
template<typename Tdest> auto SelectTo() -> OperatorSelect<T, Tdest>&
14-
```
15-
16-
**Example:**
17-
```c++
18-
FromArray(values, size)
19-
.Select<float>([](int x) { return x * 2.5; }) // Convert int to float with scaling
20-
.Do([](float x) { Serial.println(x); });
21-
```
22-
237
### OperatorDistinct
248
Emits only values that haven't been seen before in the stream.
259

@@ -89,25 +73,6 @@ FromArray(values, size)
8973
- State machines
9074
- Progressive calculations
9175

92-
### OperatorReduce
93-
Accumulates values and emits only the final result.
94-
95-
**Signature:**
96-
```c++
97-
template<typename Tdest> auto ReduceToFinal(Tdest (*accumulator)(Tdest, T), Tdest seed) -> OperatorReduce<T, Tdest>&
98-
```
99-
100-
**Example:**
101-
```c++
102-
float multiplyValues(float acc, int current) {
103-
return acc * current;
104-
}
105-
106-
FromArray(values, size)
107-
.ReduceToFinal<float>(multiplyValues, 1.0) // Only final product
108-
.Do([](float x) { Serial.print("Final product: "); Serial.println(x); });
109-
```
110-
11176
### OperatorStartWith
11277
Emits specified values before beginning to emit values from the source observable.
11378

examples/Accelerometer/Accelerometer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void setup()
5050

5151
// Step counter using scan operator
5252
accelerometer
53-
.Select(getMagnitude)
53+
.Select<float>(getMagnitude)
5454
.Where(magnitudeThreshold)
5555
.DistinctUntilChanged() // Only count distinct movements
5656
.Do(printStepCount);

examples/AccelerometerExample/AccelerometerExample.cpp

Whitespace-only changes.

examples/DistinctExample/DistinctExample.cpp

Whitespace-only changes.

examples/Reduce/Reduce.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void loop()
4646

4747
// Using ReduceToFinal - only emits the final accumulated result
4848
FromArray(values, valuesLength)
49-
.ReduceToFinal<float>(accumulateValues, 10.0f)
49+
.Reduce(accumulateValues, 10.0f)
5050
.DoAndFinally(printFinalResult, printComplete);
5151

5252
Serial.println();

examples/RotaryEncoderExample/RotaryEncoderExample.cpp

Whitespace-only changes.

examples/ScanExample/ScanExample.cpp

Whitespace-only changes.

examples/ThrottleExample/ThrottleExample.cpp

Whitespace-only changes.

examples/UltrasonicExample/UltrasonicExample.cpp

Whitespace-only changes.

src/Operators/OperatorReduce.h

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +0,0 @@
1-
/***************************************************
2-
Copyright (c) 2019 Luis Llamas
3-
(www.luisllamas.es)
4-
5-
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6-
7-
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License
8-
****************************************************/
9-
10-
#ifndef _REACTIVEOPERATORREDUCE_h
11-
#define _REACTIVEOPERATORREDUCE_h
12-
13-
template <typename Torig, typename Tdest>
14-
class OperatorReduce : public Operator<Torig, Tdest>
15-
{
16-
public:
17-
typedef Tdest(*ReactiveReduceFunction)(Tdest, Torig);
18-
19-
OperatorReduce(ReactiveReduceFunction function, Tdest initialValue);
20-
21-
void OnNext(Torig value) override;
22-
void OnComplete() override;
23-
void Reset() override;
24-
25-
private:
26-
ReactiveReduceFunction _function;
27-
Tdest _accumulator;
28-
Tdest _initialValue;
29-
bool _hasValue;
30-
};
31-
32-
template <typename Torig, typename Tdest>
33-
OperatorReduce<Torig, Tdest>::OperatorReduce(ReactiveReduceFunction function, Tdest initialValue)
34-
{
35-
_function = function;
36-
_initialValue = initialValue;
37-
_accumulator = initialValue;
38-
_hasValue = false;
39-
}
40-
41-
template <typename Torig, typename Tdest>
42-
void OperatorReduce<Torig, Tdest>::OnNext(Torig value)
43-
{
44-
// Accumulate the value but don't emit yet
45-
_accumulator = _function(_accumulator, value);
46-
_hasValue = true;
47-
}
48-
49-
template <typename Torig, typename Tdest>
50-
void OperatorReduce<Torig, Tdest>::OnComplete()
51-
{
52-
// Only emit the final accumulated result when the observable completes
53-
if (_hasValue || _accumulator != _initialValue)
54-
{
55-
this->_childObservers.OnNext(_accumulator);
56-
}
57-
58-
// Forward the completion signal
59-
Operator<Torig, Tdest>::OnComplete();
60-
}
61-
62-
template <typename Torig, typename Tdest>
63-
void OperatorReduce<Torig, Tdest>::Reset()
64-
{
65-
_accumulator = _initialValue;
66-
_hasValue = false;
67-
}
68-
69-
#endif

0 commit comments

Comments
 (0)