ESP32 / ESP8266 cpplinq: Getting last element of array that fills a criteria

In this tutorial we will learn how to obtain the last element of an array that fills a given criteria, using cpplinq. The tests on the ESP32 were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The tests on the ESP8266 were performed using a DFRobot’s ESP8266 FireBeetle board.

Introduction

In this tutorial we will learn how to obtain the last element of an array that fills a given criteria, using cpplinq. This tutorial was tested both on the ESP32 and on the ESP8266.

For this example, we will obtain the last element of an integer array that is even. The cpplinq operator that we will use is called last_or_default.

Note that, as the name implies, when we apply this operator and no element of the array that fills the criteria is found, then a default value is returned.

The tests on the ESP32 were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The tests on the ESP8266 were performed using a DFRobot’s ESP8266 FireBeetle board.

The code

We will start by including the cpplinq library. After that we will declare the use of the cpplinq namespace.

#include "cpplinq.hpp"
 
using namespace cpplinq;

Moving on to the Arduino setup, we will start by opening a serial connection, so we are able to output the results of our program.

Serial.begin(115200);

Then we will define an array of integers over which we will apply the cpplinq operator.

int ints[] = {1,2,3,5,10,17};

After this we will take care of applying the cpplinq operator to our array. But before that we need to convert the array to a range object, with a call to the from_array operator.

from_array(ints)

After we have our range, we will apply the last_or_default operator. As input, we need to pass a function that implements the criteria that will be applied to all the elements of the range. Then, the last_or_default operator will return the last element that fills our criteria.

So, the function will receive as input an integer and return true in case that value meets our criteria, and false otherwise. We will specify this function using the C++ lambda function syntax.

As already mentioned, we want to obtain the last even element of the sequence. So, our function should return true if the element is even and false otherwise.

The full expression tree can be seen below.

int result1 = from_array(ints)
              >> last_or_default([](int i){return i%2 == 0;});

Additionally, we will check the result of applying a criteria that is not filled by any of the elements of the array.

So, we will try to find the last element in our array that is greater than 20. Since no element of the array is greater than 20, then a default value is returned as result. Since the expected result is an integer, its default value will be 0.

int result2 = from_array(ints)
              >> last_or_default([](int i){return i > 20;});  

The final code can be seen below. It already contains the prints of the results from both expression trees.

#include "cpplinq.hpp"
 
using namespace cpplinq;
 
void setup() {
  Serial.begin(115200);

  int ints[] = {1,2,3,5,10,17};
   
  int result1 = from_array(ints)
               >> last_or_default([](int i){return i%2 == 0;});

  int result2 = from_array(ints)
               >> last_or_default([](int i){return i > 20;});
                
  Serial.println(result1);                  
  Serial.println(result2);  
 
}
 
void loop() {}

Testing the code

To test the code, simply compile it and upload it to your device using the Arduino IDE. When the procedure finishes, open the IDE serial monitor.

You should get an output similar to figure 1. As can be seen, for the first expression tree, we have obtained the value 10, which is the last even element of the array.

On the other hand, for the second expression tree, we have obtained the value 0. As mentioned before, our array did not have an element greater than 20. So the default value was returned for the result type (0, since the result type is integer).

Output of the program on the Arduino IDE serial monitor.
Figure 1 – Output of the program on the Arduino IDE serial monitor. Taken from the tests on the ESP32.

Leave a Reply