In this esp32 tutorial we will check how to chain multiple cpplinq operators to apply more complex expression trees to filter arrays. The tests shown on this tutorial were performed using an ESP32 board from DFRobot.
Introduction
In this esp32 tutorial we will check how to chain multiple cpplinq operators to apply more complex expression trees to filter arrays. We will be using the Arduino core and the ESP32 as target device.
For this tutorial we will use cpplinq to process an array of integers. We want to obtain the maximum number of the array that is even and lesser than 100. As we will see below, we will be able to express this criteria in 3 lines of code and obtain the result.
The tests shown on this tutorial were performed using an ESP32 board from DFRobot.
The code
As usual, we will start by including the cpplinq library and declaring the use of the cpplinq namespace.
#include "cpplinq.hpp"
using namespace cpplinq;
Then we will move on to the Arduino setup function, where we will write the rest of our code. We start by opening a serial connection, to output the results of the program.
Serial.begin(115200);
Then we will declare an array of integers, with some arbitrary values. We should have some even and odd values, and also some values greater and lesser than 100.
int ints[] = {5,7,4,7,8,15,9,25,15,14,30,9,24,5,78,912,37,48,980,200,201};
Then we will convert our array to a range object, so we can apply cpplinq operators over it. To do this conversion, we call the from_array function, passing as input our previously declared array of integers.
from_array(ints)
Then we will start applying our conditions. The first thing we want is to filter the array to obtain all the even numbers. As we have already seen on this previous article, we can do it by using the where operator.
The where operator receives as input a function that will be applied to all the values of the array, one by one, and should return true in case our conditions applies to that value, and false otherwise.
To keep the code short and compact, we will use the C++ lambda functions syntax. The implementation of the function will be as simple as checking if the number if divisible by 2, which means it is even.
where([](int i) {return i%2 == 0;})
The second filtering condition we want to apply is getting only the numbers that are lesser than 100. We can achieve this using again the where operator, passing as input a lambda function that reflects this condition.
Note that since we have the freedom to implement the body of the lambda function as we want, we could have reflected these two conditions (being even and lesser than 100) in a single function, thus only having to call the where operator once.
This would also be a valid approach but when we start having multiple conditions, it becomes harder to read if we place all the conditions in a single function. Thus, in terms of readability, it typically becomes simpler if we have multiple where operators chained.
where([](int i) {return i < 100;})
Then, to obtain the maximum value from the array with even numbers lesser than 100, we simply need to call the max operator, without passing anything as input. You can check more about the max operator on this previous tutorial.
max()
Now that we have individually specified all the operators we need to use, we simply need to start chaining them. To do this, we need to use the >> operator between the calls.
Note that, for readability, it becomes much simpler if we have each cpplinq operator in a new line, as shown below.
Since we are applying the max operator at the end, over an array of integers, the expected result is an integer.
int result = from_array(ints)
>> where([](int i) {return i%2 == 0;})
>> where([](int i) {return i < 100;})
>> max();
After this, we will print the result to the serial port, to confirm we obtained the expected value.
Serial.println(result);
The final code can be seen below.
#include "cpplinq.hpp"
using namespace cpplinq;
void setup() {
Serial.begin(115200);
int ints[] = {5,7,4,7,8,15,9,25,15,14,30,9,24,5,78,912,37,48,980,200,201};
int result = from_array(ints)
>> where([](int i) {return i%2 == 0;})
>> where([](int i) {return i < 100;})
>> max();
Serial.println(result);
}
void loop() {}
Testing the code
To test the code, simply compile it and upload it to your ESP32 device, using the Arduino IDE, with support for the Arduino core.
When the procedure finishes, open the IDE serial monitor. You should get an output similar to figure 1, which shows the value 78 getting printed. If we look into the array of integers used, this is exactly the value that fills the criteria we applied.
Figure 1 – Output of the program, showing the maximum even number lesser than 100 contained in the array.
https://techtutorialsx.com/2019/04/13/esp32-arduino-chaining-cpplinq-operators/