KUMI v5.0.0
Gorgeous Garnet
Loading...
Searching...
No Matches

◆ map_reduce

map_reduce_t kumi::map_reduce {}
inlineconstexprnodiscard

Callable object performing a tree-like reduction of all elements of a product type. The given map function is applied before executing the reduction to each element of the input.

On record types, this function operates on the underlying values, not on the fields themselves.

Note
For associative operations, this produces the same result as a left or right fold preceded by map, but has a different intermediate evaluation order.

inline constexpr

Header file

#include <kumi/algorithm/reduce.hpp>

Call Signature

template<typename Function, monoid M, product_type T>
constexpr auto map_reduce(Function f, M && m, T && t);
constexpr map_reduce_t map_reduce
Callable object performing a tree-like reduction of all elements of a product type....
Definition reduce.hpp:315
template<typename Function, monoid M, product_type T, typename V>
constexpr auto map_reduce(Function f, M && m, T && t, V init);

Parameters

  • f: Mapping function to apply
  • m: Monoid callable function to apply
  • t: Product Type to reduce
  • init: Optional initial value of the reduction.

Return value

The result of reducing the elements of `t` by `m` after being processed by `f`,
recursively combining elements in a tree structure.

Helper type

namespace kumi::result
{
template<monoid M, product_type T> struct map_reduce;
template<monoid M, product_type T>
using map_reduce_t = typename map_reduce<M,T>::type;
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;
}

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

Examples

  • Tuple
    #include <kumi/kumi.hpp>
    #include <iostream>
    int main()
    {
    auto t1 = kumi::tuple{2.,5,short{3},'\4'};
    auto t2 = kumi::tuple{2,1,short{55},' '};
    auto times_two = [&](auto e){ return 2*e; };
    std::cout << kumi::map_reduce(times_two, kumi::function::multiplies, t1) << "\n";
    auto is_pair = [&](auto e){ return e%2==0; };
    std::cout << kumi::map_reduce(is_pair, kumi::function::plus, t2) << "\n";
    }
    constexpr kumi::function::numeric_add plus
    Callable object matching the kumi::numeric_add monoid, usable with kumi::algorithm.
    Definition monoid.hpp:118
    constexpr kumi::function::numeric_prod multiplies
    Callable object matching the kumi::numeric_prod monoid, usable with kumi::algorithm.
    Definition monoid.hpp:126
    Fixed-size collection of heterogeneous values.
    Definition tuple.hpp:35
  • Record
    #include <kumi/kumi.hpp>
    #include <iostream>
    int main()
    {
    using namespace kumi::literals;
    auto r1 = kumi::record{"a"_id = 2.,"b"_id = 5,"c"_id = short{3},"d"_id = '\4'};
    auto r2 = kumi::record{"a"_id = 2 ,"b"_id = 1,"c"_id = short{55},"d"_id = ' '};
    auto times_two = [&](auto e){ return 2*e; };
    std::cout << kumi::map_reduce(times_two, kumi::function::multiplies, r1) << "\n";
    auto is_pair = [&](auto e){ return e%2==0; };
    std::cout << kumi::map_reduce(is_pair, kumi::function::plus, r2) << "\n";
    }
    Fixed-size collection of heterogeneous tagged fields, tags are unique.
    Definition record.hpp:36