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

◆ fold_left() [2/2]

template<typename Function, concepts::product_type T, typename Value>
auto kumi::fold_left ( Function f,
T && t,
Value init )
inlinenodiscardconstexpr

Computes the generalized combination of all elements using a tail recursive call.

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

Parameters
fBinary callable function to apply
tProduct type to operate on
initOptional initial value of the fold
Returns
The value of f( f( f(init, get<0>(t)), ...), get<N-1>(t))

Helper type

namespace kumi::result
{
template<typename Function, product_type T, typename Value> struct fold_left;
template<typename Function, product_type T, typename Value>
using fold_left_t = typename fold_left<Function,T,Value>::type;
}
constexpr auto fold_left(Function f, T &&t, Value init)
Computes the generalized combination of all elements using a tail recursive call.
Definition fold.hpp:45

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

Examples:

Tuple:

#include <kumi/kumi.hpp>
#include <iostream>
#include <vector>
int main()
{
auto t = kumi::tuple{2.,1,short{55},'z'};
auto output = kumi::fold_left( [](auto a, auto m) { a.push_back(sizeof(m)); return a; }
, t
, std::vector<std::size_t>{}
);
for(auto s : output) std::cout << s << " ";
std::cout << "\n";
auto u = kumi::tuple{1,3,2,4,0,5,9,6,7};
std::cout << kumi::fold_left( [](auto acc, auto e)
{
std::cout << '(' << acc << ',' << e << ")\n";
return (e <acc) ? e : acc;
}
, u
)
<< "\n";
}
Fixed-size collection of heterogeneous values.
Definition tuple.hpp:33

Record:

#include <kumi/kumi.hpp>
#include <iostream>
#include <vector>
int main()
{
using namespace kumi::literals;
auto t = kumi::record{"a"_id = 2.,"b"_id = 1,"c"_id = short{55},"d"_id = 'z'};
auto output = kumi::fold_left( [](auto a, auto m) { a.push_back(sizeof(m)); return a; }
, t
, std::vector<std::size_t>{}
);
for(auto s : output) std::cout << s << " ";
std::cout << "\n";
auto u = kumi::record{"a"_id = 1,"b"_id = 3,"c"_id = 2,"d"_id = 4,"e"_id = 0,
"f"_id = 5,"g"_id = 9,"h"_id = 6,"i"_id = 7};
std::cout << kumi::fold_left( [](auto acc, auto e)
{
std::cout << '(' << acc << ',' << e << ")\n";
return (e <acc) ? e : acc;
}
, u
)
<< "\n";
}
Fixed-size collection of heterogeneous tagged fields, tags are unique.
Definition record.hpp:36