Add reduce
This commit is contained in:
parent
0a2694314f
commit
25cd6e9eed
2 changed files with 23 additions and 4 deletions
|
@ -1,12 +1,20 @@
|
|||
#include <utempl/utils.hpp>
|
||||
|
||||
struct Container {
|
||||
float data{};
|
||||
};
|
||||
|
||||
auto main() -> int {
|
||||
using namespace utempl;
|
||||
constexpr TupleLike auto tuple = Tuple{1, 2, 3, 4, 5, 6}
|
||||
constexpr auto value = Tuple{1, 2, 3, 4, 5, 6}
|
||||
| Take<5>()
|
||||
| Map([](int arg) {return arg + 1;})
|
||||
| Map([](int arg) -> float {return arg;})
|
||||
| Map([](float arg) -> Container {return {.data = arg};})
|
||||
| Reverse()
|
||||
| Take<3>(); // Lazy evaluate
|
||||
static_assert(tuple == Tuple{6.0f, 5.0f, 4.0f});
|
||||
| Take<3>()
|
||||
| Reduce(0.f, [](auto accumulator, Container arg) -> float {
|
||||
return accumulator + arg.data;
|
||||
}); // Lazy evavalue
|
||||
static_assert(value == 15.0f);
|
||||
};
|
||||
|
|
|
@ -389,6 +389,17 @@ inline constexpr auto LeftFold(Tuple&& tuple, T&& init, F&& f) {
|
|||
});
|
||||
};
|
||||
|
||||
template <TupleLike Tuple, typename T, typename F>
|
||||
inline constexpr auto Reduce(Tuple&& tuple, T&& init, F&& f) {
|
||||
return LeftFold(std::forward<Tuple>(tuple), std::forward<T>(init), std::forward<F>(f));
|
||||
};
|
||||
|
||||
template <typename T, typename F>
|
||||
inline constexpr auto Reduce(T&& init, F&& f) {
|
||||
return [init = std::forward<T>(init), f = std::forward<F>(f)]<TupleLike Tuple>(Tuple&& tuple){
|
||||
return Reduce(std::forward<Tuple>(tuple), std::move(init), std::move(f));
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
template <TupleLike Tuple, TupleLike Tuple2>
|
||||
|
|
Loading…
Reference in a new issue