Commit 7281d632 authored by Tom Niget's avatar Tom Niget

Finish implementation of range

parent 9ced6e74
......@@ -7,16 +7,35 @@
#include <ranges>
namespace view = std::views;
#include <python/basedef.hpp>
auto stride = [](int n) {
return [s = -1, n](auto const&) mutable { s = (s + 1) % n; return !s; };
};
// todo: proper range support
struct range_s : TyBuiltin<range_s>
{
template <typename T>
auto sync(T stop) { return std::views::iota(0, stop); }
auto sync(T stop) { return sync(0, stop); }
template <typename T>
auto sync(T start, T stop) { return std::views::iota(start, stop); }
auto sync(T start, T stop, T step = 1) {
// https://www.modernescpp.com/index.php/c-20-pythons-map-function/
if(step == 0) {
throw std::invalid_argument("Step cannot be 0");
}
auto Step = start < stop ? step : -step;
auto Begin = std::min(start, stop);
auto End = Step < 0 ? Begin : std::max(start, stop);
return view::iota(Begin, End)
| view::filter(stride(std::abs(Step)))
| view::transform([start, stop](std::size_t i){
return start < stop ? i : stop - (i - start);
});
}
} range;
#endif // TYPON_RANGE_HPP
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment