ESP32 Arduino: Filtering arrays with cpplinq

Introduction

In this tutorial we will check how to filter arrays using cpplinq. We are going to perform our tests on a ESP32 microcontroller, using the Arduino core.

Note however that cpplinq is a generic C++ library, which means that what we are going to learn here can be used outside the scope of developing programs for the ESP32.

For an introduction on cpplinq and a guide on how to install it as an Arduino library, please check this previous tutorial. You can check the project page here. You can read a very interesting article about cpplinq here.

The tests shown on this tutorial were performed using an ESP32 board from DFRobot.

The code

We will start out code by including the cpplinq library.

#include "cpplinq.hpp"

Then we will declare the use of the cpplinq namespace, for a shorter syntax when calling its functions.

using namespace cpplinq;

Then we will move on to the Arduino setup function, where we will write the rest of our code. We will start by opening a serial connection, so we can output the results of our program.

Serial.begin(115200);

Then we will declare the array of integers that we want to filter. We will declare some arbitrary values, making sure the array contains both odd and even numbers, as can be seen below.

Note that, from the array below, we expect to obtain a result with the even values: 4, 8, 14 and 30.

int ints[] = {5,7,4,7,8,15,9,25,15,14,30,9};

Then we will start applying our cpplinq expressions. As we did in the previous tutorial, we will start by creating a range object from our array of integers. We do this by calling the from_array function, passing as input our ints array.

from_array(ints)

Then, we can start chaining cpplinq operators by using the >> operator. So, the next thing we want to do is applying an operator that will select all the values of the array that fill a specific condition.

This operator is called where. It receives as input a function that specifies the condition we want to apply to the elements of the array. So, this function will be applied to all the elements of the array, one by one, and it should return true when the given element fills the condition.

Taking this in consideration, we can already know the signature that our function needs to follow. It needs to return a Boolean value and receive as input an integer. The function implementation will simply consist on checking if the number is even, which is true if the number is divisible by 2.

Note that we are going to be using the C++ lambda function syntax for a more compact code.

[](int i) {return i%2 ==0;}

As mentioned before, this lambda function will be the argument we will pass to the where operator.

where([](int i) {return i%2 ==0;})

To finalize, we will convert the range object back to a type that we can iterate. We are going to convert it to a C++ vector by calling the to_vector operator.

to_vector()

Note that we have been looking into each operator call separately. In our final code, we will chain the previous calls by using the >> operator.

Note that we are going to be using the auto keyword, which guarantees that the compiler will infer the type of the variable that will be returned by the cpplinq expression tree.

auto result = from_array(ints)
                >> where([](int i) {return i%2 ==0;})
                >> to_vector();

As mentioned, we will obtain a vector of integers, which we can iterate to print each element. Note that we can use the size member function to obtain the number of elements of the vector.

Then, in each iteration of the loop, we can access each element of the vector using the [] operator and the current index, pretty much like we would do if we were iterating a regular array of integers.

for(int i=0; i<result.size(); ++i){
    Serial.print(result[i]);
    Serial.print("|");
}

The final complete 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};
  
  auto result = from_array(ints)
               >> where([](int i) {return i%2 ==0;})
               >> to_vector();
               
  for(int i=0; i<result.size(); ++i){
    Serial.print(result[i]);
    Serial.print("|");
  }
}

void loop() {}

Testing the code

To test the code, simply compile it and upload it to your ESP32, using the Arduino IDE. Once the procedure completes, open the IDE serial monitor. You should get an output similar to figure 1.

As can be seen below, the array printed to the serial monitor only contains the even elements, as expected.

Output of the ESP32 Arduino program that filters the even elements of an integer array.
Figure 1 – Output of the program, with the filtered array containing only even numbers.

Related Posts

Leave a Reply

Discover more from techtutorialsx

Subscribe now to keep reading and get access to the full archive.

Continue reading