|
| 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 | +#include "ReactiveArduinoLib.h" |
| 11 | +using namespace Reactive; |
| 12 | + |
| 13 | +int values[] = { 1, 2, 3, 4, 5 }; |
| 14 | +int valuesLength = sizeof(values) / sizeof(values[0]); |
| 15 | + |
| 16 | +// Accumulator function for sum calculation |
| 17 | +int addValues(int acc, int value) { |
| 18 | + return acc + value; |
| 19 | +} |
| 20 | + |
| 21 | +// Accumulator function for product calculation |
| 22 | +int multiplyValues(int acc, int value) { |
| 23 | + return acc * value; |
| 24 | +} |
| 25 | + |
| 26 | +// Action to print intermediate scan results |
| 27 | +void printScanResult(int x) { |
| 28 | + Serial.print("Scan result: "); |
| 29 | + Serial.println(x); |
| 30 | +} |
| 31 | + |
| 32 | +// Action when scan is complete |
| 33 | +void printScanComplete() { |
| 34 | + Serial.println("Scan complete!"); |
| 35 | + Serial.println(); |
| 36 | +} |
| 37 | + |
| 38 | +// Example showing running average calculation |
| 39 | +float runningAverageValues[] = { 10.0, 20.0, 30.0, 40.0, 50.0 }; |
| 40 | +int avgValuesLength = sizeof(runningAverageValues) / sizeof(runningAverageValues[0]); |
| 41 | +int valueCount = 0; |
| 42 | + |
| 43 | +// Running average accumulator |
| 44 | +float calculateRunningAverage(float acc, float value) { |
| 45 | + valueCount++; |
| 46 | + return ((acc * (valueCount - 1)) + value) / valueCount; |
| 47 | +} |
| 48 | + |
| 49 | +// Action to print running average |
| 50 | +void printRunningAverage(float x) { |
| 51 | + Serial.print("Running average: "); |
| 52 | + Serial.println(x, 2); |
| 53 | +} |
| 54 | + |
| 55 | +// Action when running average is complete |
| 56 | +void printAverageComplete() { |
| 57 | + Serial.println("Running average complete!"); |
| 58 | + Serial.println(); |
| 59 | + valueCount = 0; // Reset for next iteration |
| 60 | +} |
| 61 | + |
| 62 | +void setup() |
| 63 | +{ |
| 64 | + Serial.begin(115200); |
| 65 | + while (!Serial) delay(1); |
| 66 | + |
| 67 | + Serial.println("=== Scan Operator Example ==="); |
| 68 | + Serial.println("Scan emits intermediate accumulated results"); |
| 69 | + Serial.println("Unlike Reduce which only emits the final result"); |
| 70 | + Serial.println(); |
| 71 | +} |
| 72 | + |
| 73 | +void loop() |
| 74 | +{ |
| 75 | + Serial.println("Example 1: Running Sum with Scan"); |
| 76 | + Serial.println("Values: 1, 2, 3, 4, 5 (starting with 0)"); |
| 77 | + |
| 78 | + // Scan emits: 1, 3, 6, 10, 15 |
| 79 | + FromArray(values, valuesLength) |
| 80 | + .Scan<int>(0, addValues) // Start with 0, add each value |
| 81 | + .DoAndFinally(printScanResult, printScanComplete); |
| 82 | + |
| 83 | + delay(1000); |
| 84 | + |
| 85 | + Serial.println("Example 2: Running Product with Scan"); |
| 86 | + Serial.println("Values: 1, 2, 3, 4, 5 (starting with 1)"); |
| 87 | + |
| 88 | + // Scan emits: 1, 2, 6, 24, 120 |
| 89 | + FromArray(values, valuesLength) |
| 90 | + .Scan<int>(1, multiplyValues) // Start with 1, multiply each value |
| 91 | + .DoAndFinally(printScanResult, printScanComplete); |
| 92 | + |
| 93 | + delay(1000); |
| 94 | + |
| 95 | + Serial.println("Example 3: Running Average with Scan"); |
| 96 | + Serial.println("Values: 10.0, 20.0, 30.0, 40.0, 50.0"); |
| 97 | + |
| 98 | + // Reset counter for this example |
| 99 | + valueCount = 0; |
| 100 | + |
| 101 | + // Scan emits running averages: 10.0, 15.0, 20.0, 25.0, 30.0 |
| 102 | + FromArray(runningAverageValues, avgValuesLength) |
| 103 | + .Scan<float>(0.0f, calculateRunningAverage) |
| 104 | + .DoAndFinally(printRunningAverage, printAverageComplete); |
| 105 | + |
| 106 | + delay(3000); |
| 107 | + Serial.println("========================================"); |
| 108 | +} |
0 commit comments