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

◆ fold_right() [2/2]

template<typename Function , product_type Tuple, typename Value >
constexpr auto kumi::fold_right ( Function  f,
Tuple &&  t,
Value  init 
)
constexpr

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

Parameters
fBinary callable function to apply
tTuple to operate on
initInitial value of the sum
Returns
The value of f(get<0>(t), f(... , f(get<N-1>(t), init))

Helper type

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

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

Example

#include <kumi/tuple.hpp>
#include <iostream>
#include <vector>
int main()
{
auto t = kumi::tuple{2.,1,short{55},'z'};
auto output = kumi::fold_right( [](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_right( [](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:35