0

$USD

$USD
TUTORIALS ESP32

ESP32 cpplinq Tutorials: Concatenating arrays

DFRobot May 09 2019 941

In this esp32 arduino tutorial we will check how to concatenate arrays using cpplinq, running on the ESP32. The tests shown here were performed using an ESP32 board from DFRobot.

Introduction

In this esp32 tutorial we will check how to concatenate arrays using cpplinq, running on the ESP32.

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

The code

We will start the code by including the cpplinq library and by declaring the use of the cpplinq namespace.

#include "cpplinq.hpp"
  
using namespace cpplinq;

Then we will move on to the Arduino setup function, where we will start by opening a serial connection, to later output the results of our program.

Serial.begin(115200);

After this we will declare two arrays, so we can append one to the other. These will contain some arbitrary integer values.

int array1[] = {3,4,5};
int array2[] = {1,9,10,22};

Then we will start writing our cpplinq expression tree. The first thing we need to do is converting the first array to a range object, so we can apply the cpplinq operators over it.

from_array(array1)

Then, to concatenate the second array to the first one, we simply need to call the concat operator.

This operator receives as input a range object, which will be concatenated to the range over which the operator is being applied. So, we need to convert array2 to a range object and pass is as input of the concat function.

concat(from_array(array2))

Finally, we should convert the resulting range to a type we can iterate. We will convert it to a vector. This is done by calling the to_vector operator.

to_vector()

The full expression tree can be seen below. Note that we are leveraging the auto keyword, so the compiler infers the type of the returned value.

auto result = from_array(array1)
               >> concat(from_array(array2))
               >> to_vector();

After this, we will iterate the vector and print each value that composes it, to ensure we obtained a result that is the concatenation of both array1 and array2.

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

The final source code can be seen below.

#include "cpplinq.hpp"
  
using namespace cpplinq;
  
void setup() {
  Serial.begin(115200);
    
  int array1[] = {3,4,5};
  int array2[] = {1,9,10,22};
  
  auto result = from_array(array1)
               >> concat(from_array(array2))
               >> 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. Then, open the serial monitor.

You should get an output similar to figure 1. As can be seen, the second array was concatenated to the first, as we expected.


Figure 1 – Output of the program, showing both arrays concatenated.