ESP32 Arduino cpplinq: the count operator

Introduction

In this tutorial we will check how to apply the cpplinq count operator over an array of integers. We will be using the ESP32 and the Arduino core.

For the installation instructions and an introduction on the cpplinq library, please check this previous tutorial.

As a testing scenario, we will try to obtain all the elements of an array that are lesser than 100. We will see two approaches to achieve this result: chaining the where operator with the count operator and using the count operator only,

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

The code

As usual, we start by including the cpplinq library and declaring the using of the cpplinq namespace, so we can have access to all the operators we need.

#include "cpplinq.hpp"
  
using namespace cpplinq;

Then we will start writing our Arduino setup function. The first thing we will do is opening a serial connection, so later we can output the results of our program.

Serial.begin(115200);

Then we will declare an array of integers, containing some arbitrary values, both lesser and greater than 100.

int ints[] = {5,912,37,48,980,200,201};

Then we need to convert the array to a range object, so we can apply the cpplinq operators over it. We do this by calling the from_array function, passing as input the array of integers.

from_array(ints)

Then we will use the where operator to filter the array, obtaining only the elements that meet our criteria (being lesser than 100). You can read here in detail how to use this operator to filter an array.

Basically, we need to pass as input of this operator a function that implements our criteria. This function will be applied to all elements of the array and it should return true if an element fulfills the criteria, and false otherwise.

For a shorter syntax, we will specify this function using the C++ lambda syntax.

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

To get the count of the total number of elements of the filtered array, we simply need to call the count operator and pass no argument.

count();

The full expression tree can be seen below. Recall that we use the >> operator to chain cpplinq operators.

int result = from_array(ints)
               >> where([](int i) {return i < 100;})
               >> count();

Alternatively, the count operator can also receive as input a function specifying a criteria. If so, only the elements of the array that meet that criteria will be counted.

Thus, as an alternative, we can rewrite the previous expression tree to use only the count operator and achieve the same result.

int result2 = from_array(ints)
               >> count([](int i) {return i < 100;});

The final source code can be seen below. It contains the printing of both results to the serial port, so we can confirm the correct values are obtained. Looking into the array of values used, it is expected that we obtain a count of 3 elements.

#include "cpplinq.hpp"
  
using namespace cpplinq;
  
void setup() {
  Serial.begin(115200);
    
  int ints[] = {5,912,37,48,980,200,201};
    
  int result = from_array(ints)
               >> where([](int i) {return i < 100;})
               >> count();

  int result2 = from_array(ints)
               >> count([](int i) {return i < 100;});

  Serial.print("First result: ");
  Serial.println(result);

  Serial.print("Second result: ");
  Serial.println(result2);
}
  
void loop() {}

Testing the code

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

You should get a result similar to figure 1. As can be seen, both approaches achieve the same expected result.

Result of filtering an array and applying the count operator, using cpplinq.
Figure 1 – Result of applying the count operator over an array, with a filtering criteria.

Related Posts

Leave a Reply

Discover more from techtutorialsx

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

Continue reading