kumi v3.1.0
Exquisite Epidote
 
Loading...
Searching...
No Matches

◆ map_reduce() [2/2]

template<monoid M, product_type T, typename Function , typename Value >
constexpr auto kumi::map_reduce ( Function &&  f,
M &&  m,
T &&  t,
Value  init 
)
inlineconstexpr

Performs a tree-like reduction of all elements of a product type. The given map function is applied before excution the reduction to each element of the input.

Note
For associative operations, this produces the same result as a left or right fold preceeded by map, but have different intermediate evaluation order.
Parameters
fMapping function to apply
mMonoid callable function to apply
tProduct type to reduce
initOptional initial value of the reduction.
Returns
The result of reducing t by recursively combining elements in a tree structure

Helper type

namespace kumi::result
{
template<monoid M, product_type T, typename Value> struct map_reduce;
template<monoid M, product_type T, typename Value>
using map_reduce_t = typename map_reduce<M,T,Value>::type;
}
constexpr auto map_reduce(Function &&f, M &&m, T &&t)
Performs a tree-like reduction of all elements of a product type. The given map function is applied b...
Definition reduce.hpp:150

Computes the return type of a call to kumi::map_reduce

Example

#include <kumi/kumi.hpp>
#include <iostream>
int main()
{
auto times_two = [&](auto e){ return 2*e; };
auto t1 = kumi::tuple{2.,5,short{3},'\4'};
std::cout << kumi::map_reduce(times_two, kumi::function::multiplies, t1) << "\n";
auto is_pair = [&](auto e){ return e%2==0; };
auto t2 = kumi::tuple{2,1,short{55},' '};
std::cout << kumi::map_reduce(is_pair, kumi::function::plus, t2) << "\n";
}
constexpr numeric_prod multiplies
Forms a binary monoid callable that can be used in kumi::algoritm. It represents the multiplication.
Definition monoid.hpp:136
constexpr numeric_add plus
Forms a binary monoid callable that can be used in kumi::algoritm. It represents the addition.
Definition monoid.hpp:129
Fixed-size collection of heterogeneous values.
Definition tuple.hpp:37