I've long found it annoying that, while we have a nice notation for ascending ranges,
we don't have one for descending ranges like [4, 3, 2, 1]. No, we can't allow 4..1, because that's an empty range. (1..4).reversed works, but is long-winded. range.from 4 downTo 1 is the primitive constructor, and it's even longer-winded.
A possible soution is to devise a general notation for "strided ranges" (or should it be "stridden" ranges?) For example
1 .. 4 .. 1 => same as 1 .. 4, because 1 is the default stride
1 .. 10 .. 2 => [1, 3, 5, 7, 9]: a stride of 2, starting at 1 and continuing until the next element would exceed the second bound
4 .. 1 .. -1 => [4, 3, 2, 1]: a stride of -1, starting at 4 and continuing until the next element would be less than the second bound
This could be implemented pretty easily by defining the .. operator on ranges.
I've long found it annoying that, while we have a nice notation for ascending ranges,
we don't have one for descending ranges like
[4, 3, 2, 1]. No, we can't allow4..1, because that's an empty range.(1..4).reversedworks, but is long-winded.range.from 4 downTo 1is the primitive constructor, and it's even longer-winded.A possible soution is to devise a general notation for "strided ranges" (or should it be "stridden" ranges?) For example
This could be implemented pretty easily by defining the
..operator on ranges.