Loading [MathJax]/extensions/tex2jax.js
kumi v3.1.0
Exquisite Epidote
 
All Classes Namespaces Functions Variables Friends Modules Pages Concepts
Loading...
Searching...
No Matches

◆ fold_left() [1/2]

template<typename Function , sized_product_type_or_more< 1 > Tuple>
constexpr auto kumi::fold_left ( Function  f,
Tuple &&  t 
)
constexpr

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

Parameters
fAssociative binary callable function to apply
tTuple of size 1 or more to operate on
Returns
The value of f( f( f(get<0>(t), get<1>(t)), ...), get<N-1>(t))

Helper type

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

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

Example

#include <kumi/tuple.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";
}
constexpr auto from_tuple(tuple< Ts... > const &t)
Converts a kumi::tuple to an instance of an arbitrary type.
Definition convert.hpp:59
Fixed-size collection of heterogeneous values.
Definition tuple.hpp:35