Skip to content

Commit 0e09192

Browse files
committed
util: add polyfill for std::ranges::fold_left
1 parent dcf37c1 commit 0e09192

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

src/util/std23.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <algorithm>
99
#include <functional>
1010
#include <iterator>
11+
#include <numeric>
1112
#include <ranges>
1213
#include <type_traits>
1314
#include <utility>
@@ -30,6 +31,7 @@ template <typename E>
3031
namespace ranges {
3132
#if __cplusplus >= 202302L
3233
using std::ranges::contains;
34+
using std::ranges::fold_left;
3335
#else
3436
/**
3537
* @tparam R range type, automatically deduced
@@ -47,6 +49,21 @@ inline constexpr bool contains(R&& range, const T& value, Proj proj = {})
4749
return std::invoke(proj, std::forward<decltype(elem)>(elem)) == value;
4850
});
4951
}
52+
53+
/**
54+
* @tparam R range type, automatically deduced
55+
* @tparam T initial value type, automatically deduced
56+
* @tparam F binary operation type, automatically deduced
57+
* @param range the range to fold
58+
* @param init the initial value
59+
* @param f binary operation to apply
60+
* @return the result of folding the range
61+
*/
62+
template <typename R, typename T, typename F>
63+
inline constexpr auto fold_left(R&& range, T init, F f)
64+
{
65+
return std::accumulate(std::ranges::begin(range), std::ranges::end(range), std::move(init), std::move(f));
66+
}
5067
#endif // __cplusplus >= 202302L
5168
namespace views {
5269
#if __cplusplus >= 202302L

0 commit comments

Comments
 (0)