On Thursday, 4 September 2014 at 11:29:30 UTC, rcor wrote:
auto c = cycle([1,2,3]);
foreach(i ; iota(-4,4)) {
writeln(c[i]);
}
prints the sequence
1
2
3
1
1 <----- c[0] == c[-1]
2
3
I understand this is what would happen if I were to just use
modulus on an index to access the original array, but should
Cycle really mimic this behavior? I feel like most uses of
Cycle would expect element -1 to be the last element of the
original range, not the first. I could manually apply addition
with modulus to ensure that the index is always positive, but
then there's not much benefit to using cycle anyways -- I might
as well be accessing the original range.
Is this behavior intentional or an oversight?
Indexing is done with the unsigned size_t. You aren't indexing at
"-1", but rather, size_t.max. size_t.max % 3 probably doesn't
result in "3 - 1" hence what you are observing.
*Should* cycle be negatively index-able? Personally, I don't
think so. And even if it could, it has been proven non-size_t
indexing is not well supported at all. It was de-facto chosen
after the "iota-map fiasco" that all ranges be indexed with
size_t and nothing else...