0

$USD

$USD
TUTORIALS ESP32

ESP32 Arduino cpplinq: The skip operator

DFRobot Jun 06 2019 1002

In this esp32 tutorial we will learn how to use the cpplinq skip operator. The tests shown on this tutorial were performed using an ESP32 board from DFRobot.

Introduction

In this esp32 tutorial we will learn how to use the cpplinq skip operator. This operator will allow us to create a new sequence from an original one, by skipping a predefined number of elements and taking all the others.

In other words, if we have an array with 10 elements and we pass the value 2 to the skip operator, the final array will only have the last 8 elements.

The tests shown on this tutorial were performed using an ESP32 board from DFRobot, running the Arduino core.

The code

To get started, as usual, we include the cpplinq library and we declare the using of the cpplinq namespace.

#include "cpplinq.hpp"
   
using namespace cpplinq;

Then, we move on to the Arduino setup, where we will write the rest of the code for our testing use case.

The first thing we will do is opening a serial connection, to later output the final results.

Serial.begin(115200);

After that, we will declare an array with 9 elements. This will be the original array on which we will apply the skip operator.

int ints[] = {1,2,3,4,5,6,7,8,9};

Then we will convert our array to a range object, allowing us to apply the cpplinq operators. This is done with a call to the from_array function, as we have done in other previous tutorials.

from_array(ints)

After this we will apply the skip operator. As already mentioned, this operator receives as input the number of elements we want to skip from the original sequence, and it will return a new one with the rest of the non-skipped elements.

For testing purposes, we will use the value 4, which means the first 4 elements should not be present in the final result. Looking into the original array, this means the values 1, 2, 3 and 4 should be the ones skipped.

skip(4)

After applying the skip operator, we will convert the resulting range to a C++ vector, so we can iterate it and print all its elements. Below we can see the full chain of operator invocations.

auto result = from_array(ints)
               >> skip(4)
               >> to_vector();

To finalize, we will iterate through all the elements of the array and print them to the serial port.

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

The final code can be seen below.

#include "cpplinq.hpp"
   
using namespace cpplinq;
   
void setup() {
  Serial.begin(115200);
     
  int ints[] = {1,2,3,4,5,6,7,8,9};
     
  auto result = from_array(ints)
               >> skip(4)
               >> 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 is finished, open the serial monitor tool.