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

ESP32 cpplinq Tutorial: combining the skip and take operators

DFRobot Jul 24 2019 499

In this esp32 tutorial we will learn how to combine the cpplinq skip and take operators to be able to obtain a given section of an integer array. The tests shown on this tutorial were performed using an ESP32 board from DFRobot.

Introduction

In this esp32 tutorial we will learn how to combine the cpplinq skip and take operators to be able to obtain a given section of an integer array. We will be using the ESP32 and the Arduino core.

The technique we will use here is the same we can apply for pagination, using these two operators.

This might seem a feature that we don’t need on a microcontroller, but with the increase of the processing capabilities of these devices and the fact that we can setup a HTTP web server in a device such as the ESP32, being able to implement pagination might be useful when developing an API.

When we are doing pagination, we need to specify the offset from which we want to obtain our page, as we are going through the pages. Common approaches consist on allowing to specify either the index of the first element of the page or the page number.

The skip operator allows to specify the starting index of the range from which we want to start obtaining elements, thus serving this purpose.

Besides that, for pagination, we need to specify the number of elements we want per page, starting from the previously mentioned offset.

The take operator allows to obtain a given number of elements from a range and ignoring the remaining ones.

Thus, by using the skip operator followed by the take operator, we can specify the starting index and the number of elements we want to obtain from a sequence.

In this tutorial we are operating the pagination over a simple integer array, for illustration purposes. Naturally, for a real application scenario, we would most likely be applying the operators over an array of objects.

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

The code

As usual, the first thing we will do is 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 setup function, we will first open a serial connection, so we are able to output the results of our program. Then, we will declare an array of integers, over which we will apply the operators.

Serial.begin(115200);
     
int ints[] = {1,2,3,4,5,6,7,8,9};

Then, before we can apply the cpplinq operators, we need to convert our array to a range object. We do this by calling the from_array operator, passing as input our integer array.

from_array(ints)

Then we will apply the skip operator, which allows us to skip a given number of elements from our range. For illustration purposes, we will pass the value 4 to the operator, thus indicating that we want to skip the 4 first elements of the array.

skip(4)

Naturally, just by applying the skip operator, we would get all the remaining elements of the array. If we stopped here, the final range we would get would have the following elements:

[5,6,7,8,9]

Since, in our case, we want to get a fixed amount of elements (our page size), we need to keep processing the resulting range.

So, we then need to call the take operator. This operator allows to specify how many elements we want to take from the sequence, ignoring the remaining ones.

For this tutorial, we will assume a page size of 4 elements, meaning that we will get the first 4 elements of the already skipped sequence.

take(4)

After this, we expect to obtain the following sequence:

[5,6,7,8]

Then we need to convert the range back to a format we can iterate. Thus, we will use the to_vector function to convert it to a C++ vector. The full expression tree can be seen below.

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

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

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

The complete 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)
               >> take(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 device using the Arduino IDE. After the procedure finishes, open the IDE serial monitor.

You should get an output similar to figure 1. As can be seen, the resulting sequence contains the expected elements.


Figure 1 – Output of the program, printed on the Arduino IDE serial monitor.

REVIEW