diff options
Diffstat (limited to 'src/ranges.hh')
| -rw-r--r-- | src/ranges.hh | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/ranges.hh b/src/ranges.hh index fa23f180..599396dc 100644 --- a/src/ranges.hh +++ b/src/ranges.hh @@ -6,6 +6,8 @@ #include <iterator> #include <numeric> +#include "constexpr_utils.hh" + namespace Kakoune { @@ -337,6 +339,21 @@ auto gather() }); } +template<typename ExceptionType, size_t... Indexes> +auto elements() +{ + return make_view_factory([] (auto&& range) { + using std::begin; using std::end; + auto elem = [it = begin(range), end = end(range), i = 0u](size_t index) mutable { + for (; i < index; ++i, ++it) + if (it == end) throw ExceptionType{i}; + return *it; + }; + // Note that initializer lists elements are guaranteed to be sequenced + return Array<std::decay_t<decltype(*begin(range))>, sizeof...(Indexes)>{{elem(Indexes)...}}; + }); +} + } #endif // ranges_hh_INCLUDED |
