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

Computes sum of a sequence of values. More...

Functions

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

Detailed Description

The sum operator computes the sum 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: Sum

Function Documentation

◆ sum()

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

Computes the sum 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 sum 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 tot = from(STRS)
| sum([](auto&& s) { return std::stoi(s); });
// tot == 131
Parameters
num_fFunction to get numerical value for each element.
Returns
(Once applied) Sum of all extracted numerical values.
Exceptions
coveo::linq::empty_sequenceThe sequence contains no elements.