Intended behavior of std.range.cycle?

rcor via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 4 04:29:28 PDT 2014


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?


More information about the Digitalmars-d-learn mailing list