$USD
  • EUR€
  • £GBP
  • $USD
TUTORIALS ESP32

ESP32 Arduino cpplinq: The all operator

DFRobot May 13 2019 473

In this esp32 tutorial we are going to check how to use the cpplinq all operator, using the ESP32 and the Arduino core. The tests shown here were performed using an ESP32 board from DFRobot.

Introduction

In this esp32 tutorial we are going to check how to use the cpplinq all operator, using the ESP32 and the Arduino core.

The all operator allows to check if all the elements of an array fill a given condition. It will return true if all the elements fill that condition and false otherwise.

The tests shown here were performed using an ESP32 board from DFRobot.

The code

As usual, we will start by importing the cpplinq library and to declare the using of the cpplinq namespace.

#include "cpplinq.hpp"
    
using namespace cpplinq;

We will write the rest of the code in the Arduino setup function. We start by opening a serial connection, to later output the results of our program.

Serial.begin(115200);

Then will declare an array of integers that fills our criteria. So, we should declare an array that has only values lesser than 100.

int array1[] = {5,5,2,89};

Then we will convert the array to a range object, so we can apply the cpplinq operator we need to check if all the elements fill our criteria.

The operator we should use is called all. This operator returns true if all the elements of the array respect our criteria, and false otherwise.

The criteria is specified as a function, that we pass as input of the all operator. This function will be applied to each element of the array and it should return true if that element fills the criteria, and false otherwise.

At the end, the all operator will return true if, for all the elements of the array, the function returned true.

For keeping the code compact, we will use the C++ lambda syntax to write our function.

all([](int i) {return i < 100;})

The full expression tree can be seen below. Note that, for this array, all elements are lesser than 100. Thus, we expect to obtain the value true.

bool resultArray1 = from_array(array1)
               >> all([](int i) {return i < 100;});

Now we will apply the same operator but over an array that has some elements that are greater than 100 (thus not respecting our condition). Taking this in consideration, it is expected that the all operator returns the value false.


bool resultArray2 = from_array(array2)
               >> all([](int i) {return i < 100;});

To finalize the code, we will print the results to the serial port.

Serial.print("Array 1: ");
Serial.println(resultArray1);
 
Serial.print("Array 2: ");
Serial.println(resultArray2);

The final code can be seen below.

#include "cpplinq.hpp"
    
using namespace cpplinq;
    
void setup() {
  Serial.begin(115200);
      
  int array1[] = {5,5,2,89};
      
  bool resultArray1 = from_array(array1)
               >> all([](int i) {return i < 100;});
 
  int array2[] = {1,2,200,477};
      
  bool resultArray2 = from_array(array2)
               >> all([](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, simply compile the previous code and upload it to your ESP32 device, using the Arduino IDE.

After the uploading procedure finishes, open the IDE serial monitor. You should get an output similar to figure 1. For the first array, where all the elements were lesser than 100, the all operator returned the value true. For the second array, since some of the elements did not fill the criteria, the operator returned false.

Figure 1 – Output the program. 


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
  • ESP32 Arduino cpplinq: The Any operator

REVIEW