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

Dave Abrahams dabrahams at apple.com
Sun Apr 10 12:01:25 CDT 2016


on Sun Apr 10 2016, Brent Royal-Gordon <swift-evolution at swift.org> wrote:

>> I can’t imagine any scenario in which getting the step's sign wrong wouldn’t just be a typo.
>
> Well, it could be an error if you derive the stride by subtracting two
> numbers, and the second number is unexpectedly larger than the
> first. Of course, that makes it a *bug*, not a typo.
>
>> Why not just assign it the correct sign during the init function?
>> (0 ... 6).striding(by: 2) // [0, 2, 4, 6], end > start, so stride = by
>> (6 ... 0).striding(by: 2) // [6, 4, 2, 0], start > end, so stride = -by
>
> One reason not to do it this way is that, if we extend `striding(by:)`
> to other collections, they will not be as easy to walk backwards
> through as this. You will have to do something like
> `collection.reversed().striding(by:)` which will be a hassle.

IMO that is an acceptable result for collections that are not
bidirectional, just as in Swift 2 you can't measure the distance between
two ForwardIndex'es that are in the wrong order.

-- 
Dave



More information about the swift-evolution mailing list