ESP32 cpplinq: Reversing an array

In this tutorial we will check how to reverse an array of integers using the cpplinq library, running on the ESP32. The tests shown on this tutorial were performed using an ESP32 board from DFRobot.

Introduction

In this tutorial we will check how to reverse an array of integers using the cpplinq library, running on the ESP32. We will be using the Arduino core to program the ESP32.

As we will see below, we can achieve this using cpplinq by calling a simple operator, which makes our code very compact.

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

The code

As usual, we start our code by including the cpplinq library and declaring the use of the cpplinq namespace.

#include "cpplinq.hpp"
 
using namespace cpplinq;

Then we will move to the Arduino setup function, where we will write the rest of our code to reverse the array. But the first thing we will do is opening a serial connection, to output the results of our program.

Serial.begin(115200);

Then we will declare an array of integers which we will later reverse.

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

After this we need to convert our array to a range object, so we can apply the cpplinq operators. We can do this simply by calling the from_array function and passing as input the array of integers we just declared.

from_array(ints)

Then, to reverse all the elements of the range, we just need to call the reverse operator. This operator takes no arguments and will take care of performing the reverse operation.

reverse()

To finalize, we will convert the range to a C++ vector, so we can iterate over it and print all the elements.

to_vector()

Chaining all the cpplinq operators together by using the >> operator, we obtain the following expression:

auto result = from_array(ints)
               >> reverse()
               >> to_vector();

To finalize, we are going to iterate over all the elements of the vector and print them to the serial port, so we can confirm the result matches what we expect.

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};
   
  auto result = from_array(ints)
               >> reverse()
               >> 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. When the procedure finishes, open the IDE serial monitor. You should get an output similar to figure 1, which shows the reversed array getting printed to the serial port.

Output of reversing an array of integers with the cpplinq library, running on the ESP32.
Figure 1 – Output of the program, showing the reversed array of integers.

Related Posts

Leave a Reply

Discover more from techtutorialsx

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

Continue reading