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

◆ reduce() [1/2]

template<monoid M, product_type T>
constexpr auto kumi::reduce ( M &&  m,
T &&  t 
)
inlineconstexpr

Performs a tree-like reduction of all elements of a product type.

Note
For associative operations, this produces the same result as a left or right fold, but have different intermediate evaluation order.
Parameters
mMonoid callable function to apply
tProduct type to reduce
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> struct reduce;
template<monoid M, product_type T>
using reduce_t = typename reduce<M,T>::type;
}
constexpr auto reduce(M &&m, T &&t)
Performs a tree-like reduction of all elements of a product type.
Definition reduce.hpp:62

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

Example

#include <kumi/kumi.hpp>
#include <iostream>
int main()
{
auto t1 = kumi::tuple{14,short{7}, 255ULL};
std::cout << kumi::reduce(kumi::function::bit_and, t1) << "\n";
std::cout << kumi::reduce(kumi::function::bit_and, t1, 65535) << "\n";
auto t2 = kumi::tuple{1 ,short{8},' ', 4ULL};
std::cout << kumi::reduce(kumi::function::bit_or, t2) << "\n";
std::cout << kumi::reduce(kumi::function::bit_or, t2, 0) << "\n";
auto t3 = kumi::tuple{3, short{23}, '\t'};
std::cout << kumi::reduce(kumi::function::bit_xor, t3) << "\n";
std::cout << kumi::reduce(kumi::function::bit_xor, t3, 1) << "\n";
auto t4 = kumi::tuple{2.,5,short{3},'\4'};
std::cout << kumi::reduce(kumi::function::multiplies, t4) << "\n";
std::cout << kumi::reduce(kumi::function::multiplies, t4, 1) << "\n";
auto t5 = kumi::tuple{2.,1,short{55},' '};
std::cout << kumi::reduce(kumi::function::plus, t5) << "\n";
std::cout << kumi::reduce(kumi::function::plus, t5, 42) << "\n";
}
constexpr boolean_and bit_and
Forms a binary monoid callable that can be used in kumi::algoritm. It represents the logical and oper...
Definition monoid.hpp:143
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
constexpr boolean_or bit_or
Forms a binary monoid callable that can be used in kumi::algoritm. It represents the logical or opera...
Definition monoid.hpp:150
constexpr boolean_xor bit_xor
Forms a binary monoid callable that can be used in kumi::algoritm. It represents the logical xor oper...
Definition monoid.hpp:157
Fixed-size collection of heterogeneous values.
Definition tuple.hpp:37