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

ESP32 / ESP8266 Arduino: The pairwise operator

DFRobot Sep 23 2019 643

In this tutorial we will learn how to use the cpplinq pairwise operator. This operator allows to group adjacent elements of an array into pairs. The tests on the ESP32 were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The tests on the ESP8266 were performed using a DFRobot’s ESP8266 FireBeetle board.


Introduction

In this tutorial we will learn how to use the cpplinq pairwise operator.

This operator allows to group adjacent elements of an array into pairs. More specifically, each element of the array will be paired with the element immediately after it.

Since the last element of the array doesn’t have any element after to be paired with, we will obtain n-1 pairs for an array with n elements.

The code we will cover on this tutorial can be used both on the ESP32 and on the ESP8266. Since cpplinq is a generic C++ library, it can also be used for general C++ programming.

The tests on the ESP32 were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The tests on the ESP8266 were performed using a DFRobot’s ESP8266 FireBeetle board.


The code

As usual, we start by including the cpplinq library and we declare the using of the cpplinq namespace.

#include<cpplinq.hpp>
 
using namespace cpplinq;

After this we will move on to the Arduino setup code. We start by opening a serial connection, so we can output the results of our program.

Serial.begin(115200);

Then we will define an array of integers, over which we will later apply the pairwise operator.

int array[] = {1,2,3,4};

Once we have our array, we can start working on it. Before applying any cpplinq operator, we need to convert it to a range object.

from_array(array)

Then we will apply the pairwise operator. This operator takes no arguments and returns as output a range with the resulting pairs of adjacent elements.

pairwise()

Finally we will convert our range to a data type that we can iterate. We will convert it to a C++ vector. The full expression tree can be seen below.

auto result = from_array(array)
                >> pairwise()
                >> to_vector();

We will finish our code by printing all the elements of the vector. Note however that this vector does not contain just integers but rather pairs of integers. This means that, when we are performing the iteration, each element will be a pair.

As covered in the previous tutorial, a pair has two member variables named first and second. These allow to access the first and second elements of the pair, respectively.

So, in each iteration of the loop, we will print the first and second elements of the pair. We are going to add some parenthesis and commas for better readability of the final result.

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

The final complete code can be seen below.

#include<cpplinq.hpp>
 
using namespace cpplinq;
 
void setup() {
  Serial.begin(115200);
 
  int array[] = {1,2,3,4};
 
  auto result = from_array(array)
                >> pairwise()
                >> to_vector();
 
  for(int i=0; i<result.size(); i++){
    Serial.print("(");
    Serial.print(result[i].first);
    Serial.print(",");
    Serial.print(result[i].second);
    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 obtain a result like the one shown in figure 1. As can be seen, we have obtained 3 pairs of elements. Each pair groups each element of the original array with the element immediately after it.

Since we had an array with 4 elements, we have obtained 3 pairs, accordingly to what was described in the introductory section. This because the last element of the array doesn’t have any adjacent element after it, which means it won’t be paired.

Figure 1 – Output of the program, showing the paired elements. Taken from the tests on the ESP32.

REVIEW