The objective of this tutorial is to show how use the cpplinq any operator, using the ESP32 and the Arduino core. The tests shown here were performed using an ESP32 board from DFRobot.
Introduction
The objective of this tutorial is to show how use the cpplinq any operator, using the ESP32 and the Arduino core.
This operator allows to check if at least one element of an array fills a certain criteria. It will return a Boolean value depending on the result: true if at least an element fills the criteria and false if none does.
The tests shown here were performed using an ESP32 board from DFRobot.
The code
We will start the code by importing the cpplinq library, so we have access to the functions we need.
#include "cpplinq.hpp"
Then we will declare the using of the cpplinq namespace, so we avoid having to use the C++ scope resolution operator.
using namespace cpplinq;
Moving on to the Arduino setup function, we will open a serial connection to later output the result of our program.
Serial.begin(115200);
Then we will declare an array containing some arbitrary integer values. We will include some values that are lesser than 100, so there are some elements that fill our criteria.
int array1[] = {5,5,2,100,200};
In order for us to be able to apply the cpplinq operator we need, we first need to convert the previous array to a range object. We do this by calling the from_array function, passing as argument the previously declared array of integers.
from_array(array1)
Then we will use the cpplinq any operator, which will return true if at least one of the elements of the array fills our condition (thus the name any).
Our condition is specified as a function that will be passed as input of the any operator. This function will be applied to each element of the array and it should return true if the element fills the criteria, and false otherwise.
As mentioned, the any operator will return true if this function returns true for at least one of the elements of the array.
any([](int i) {return i < 100;})
The full expression tree can be seen below. Note that the result of the any operator is a Boolean, which we will store in a variable. Note that since there are some elements of the array that fill the criteria, we expect that the any operator returns true.
bool resultArray1 = from_array(array1)
>> any([](int i) {return i < 100;});
Now we will apply the same procedure to a second array which doesn’t contain any element that fills our criteria. In this case, the any operator should return false.
int array2[] = {345,100,200,477};
bool resultArray2 = from_array(array2)
>> any([](int i) {return i < 100;});
To finalize, we will print both results to the serial port.
Serial.print("Array 1: ");
Serial.println(resultArray1);
Serial.print("Array 2: ");
Serial.println(resultArray2);
The final source code can be seen below.
#include "cpplinq.hpp"
using namespace cpplinq;
void setup() {
Serial.begin(115200);
int array1[] = {5,5,2,100,200};
bool resultArray1 = from_array(array1)
>> any([](int i) {return i < 100;});
int array2[] = {345,100,200,477};
bool resultArray2 = from_array(array2)
>> any([](int i) {return i < 100;});
Serial.print("Array 1: ");
Serial.println(resultArray1);
Serial.print("Array 2: ");
Serial.println(resultArray2);
}
void loop() {}
Testing the code
To test the code, compile it and upload it to your device, using the Arduino IDE. When the procedure finishes, open the serial monitor to obtain the outputs from the program.
You should see a result similar to figure 1. For the first array it returns the value true, since there’s at least one element lesser than 100 in that array. On the second array, since there is no element lesser than 100, the operator returns the value false.

Related Posts
- ESP32 Arduino cpplinq: Getting started
- ESP32 Arduino cpplinq: Filtering an array
- ESP32 Arduino cpplinq: min and max operators
- ESP32 Arduino cpplinq: The count operator
- ESP32 Arduino cpplinq: Concatenating arrays
- ESP32 Arduino cpplinq: The Average operator
I would like to thank your for your efforts in producing clear and well thought out tutorials, its much appreciated. When you have time could you produce a tutorial on cpplinq with two dimensional arrays?