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

◆ reduce() [1/2]

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

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:66

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

Examples:

#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:138
constexpr numeric_prod multiplies
Forms a binary monoid callable that can be used in kumi::algoritm. It represents the multiplication.
Definition monoid.hpp:131
constexpr numeric_add plus
Forms a binary monoid callable that can be used in kumi::algoritm. It represents the addition.
Definition monoid.hpp:124
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:145
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:152
Fixed-size collection of heterogeneous values.
Definition tuple.hpp:29
#include <kumi/kumi.hpp>
#include <iostream>
int main()
{
using namespace kumi::literals;
auto r1 = kumi::record{"a"_id = 14,"b"_id = short{7}, "c"_id = 255ULL};
std::cout << kumi::reduce(kumi::function::bit_and, r1) << "\n";
std::cout << kumi::reduce(kumi::function::bit_and, r1, 65535) << "\n";
auto r2 = kumi::record{"a"_id = 1 ,"b"_id = short{8},"c"_id = ' ', "d"_id = 4ULL};
std::cout << kumi::reduce(kumi::function::bit_or, r2) << "\n";
std::cout << kumi::reduce(kumi::function::bit_or, r2, 0) << "\n";
auto r3 = kumi::record{"a"_id = 3, "b"_id = short{23}, "c"_id = '\t'};
std::cout << kumi::reduce(kumi::function::bit_xor, r3) << "\n";
std::cout << kumi::reduce(kumi::function::bit_xor, r3, 1) << "\n";
auto r4 = kumi::record{"a"_id = 2.,"b"_id = 5,"c"_id = short{3},"d"_id = '\4'};
std::cout << kumi::reduce(kumi::function::multiplies, r4) << "\n";
std::cout << kumi::reduce(kumi::function::multiplies, r4, 1) << "\n";
auto r5 = kumi::record{"a"_id = 2.,"b"_id = 1,"c"_id = short{55},"d"_id = ' '};
std::cout << kumi::reduce(kumi::function::plus, r5) << "\n";
std::cout << kumi::reduce(kumi::function::plus, r5, 42) << "\n";
}
Fixed-size collection of heterogeneous fields necessarily named, names are unique.
Definition record.hpp:29