coveo::linq
Implementation of .NET-like LINQ operators in C++
Functions

Computes average of a sequence of values. More...

Functions

template<typename F >
auto coveo::linq::average (const F &num_f) -> detail::average_impl< F >
 Computes average using numerical function. More...
 

Detailed Description

The average operator computes the average of all values in a sequence. To achieve this, it needs a numerical function to extract a numeric value from each sequence element.

This is a terminal operator.

.NET equivalent: Average

Function Documentation

◆ average()

template<typename F >
auto coveo::linq::average ( const F &  num_f) -> detail::average_impl<F>

Computes the average of all elements in a sequence by repeatedly calling a numerical function. The function receives an element as parameter and must return a numerical value for the element. The final result is the average of all such numerical values.

Does not work on empty sequences.

Use like this:

const std::vector<std::string> STRS = { "42", "23", "66" };
using namespace coveo::linq;
auto avg = from(STRS)
| average([](auto&& s) { return std::stoi(s); });
// avg == 43
Parameters
num_fFunction to get numerical value for each element.
Returns
(Once applied) Average of all extracted numerical values.
Exceptions
coveo::linq::empty_sequenceThe sequence contains no elements.