[swift-evolution] [Draft]: Introducing a striding(by:) method on 3.0 ranges

Xiaodi Wu xiaodi.wu at gmail.com
Sat Apr 9 03:25:03 CDT 2016


On Sat, Apr 9, 2016 at 2:49 AM, Michel Fortin <michel.fortin at michelf.ca> wrote:
> Le 8 avr. 2016 à 14:37, Erica Sadun via swift-evolution <swift-evolution at swift.org> a écrit :
>>
>> (0 ... 9).striding(by: -2) == [9, 7, 5, 3, 1]
>
> The above reads wrong to me. The expression has to be read differently depending on the tinny detail that is the sign of the step that comes last:
>
> * positive step: from 0 to 9 striding by 2
> * negative step: to 0 from 9 striding by -2
>
> Am I the only one thinking it's a bit too clever to swap the start and stop parts like this?

The issue here is that ranges both do and don't have a direction.
0...9 is supposed to represent all numbers between and including 0 and
9 (agnostic of direction). However, when you have `for i in 0...9 {
code }`, the sequence very much goes from 0 through 9 and not the
other way round.

Note that it's not possible to write 9...0. Once that is clear, it's
fairly intuitive (IMO) to look to the stride size for indicating the
direction in which we stride, since that is the only other value there
is. Thus, we propose using a negative stride size means stride
backwards through the range.

>
> --
> Michel Fortin
> https://michelf.ca
>


More information about the swift-evolution mailing list