TUTORIALS ESP32

# ESP32 Arduino cpplinq tutorial: the average operator

DFRobot May 13 2019 666

### The code

#### The first thing we will do is importing the cpplinq library. After that, we will declare that we will be using the cpplinq namespace.

```#include "cpplinq.hpp"

using namespace cpplinq;```

#### Moving on to the setup function, we will start by opening a serial connection. We will use it to output the result of our program.

`Serial.begin(115200);`

#### Then we will declare an array of integers, which we will first filter and then obtain the average value.

`int ints[] = {5,5,2,100,200};`

#### After this, we will need to convert the array to a range object, in order for us to be able to apply the cpplinq operators. We perform this conversion by calling the from_array function, passing as input our array of integers.

`from_array(ints)`

#### Note that we are going to use the C++ lambda syntax.

`where([](int i) {return i < 100;})`

`avg()`

#### To chain all the previous function calls, we will use the >> operator. Note that the result of the expression tree should be an integer containing the average value of all the elements lesser than 100.

```int result = from_array(ints)
>> where([](int i) {return i < 100;})
>> avg();
```

#### To finalize, we will print to the serial port the result obtained.

```Serial.print("Average: ");
Serial.println(result);```

#### The final source code can be seen below.

```#include "cpplinq.hpp"

using namespace cpplinq;

void setup() {
Serial.begin(115200);

int ints[] = {5,5,2,100,200};

int result = from_array(ints)
>> where([](int i) {return i < 100;})
>> avg();

Serial.print("Average: ");
Serial.println(result);

}

void loop() {}```