[swift-evolution] Feature proposal: Range operator with step

Dave Abrahams dabrahams at apple.com
Wed Apr 6 12:10:09 CDT 2016


on Wed Apr 06 2016, Brent Royal-Gordon <brent-AT-architechies.com> wrote:

>> For example, there are all kinds of other ways to slice this:
>> 
>>    stride(over: 0..<200, by: -2)
>
> This seems like a particularly good solution. The way I understand it
> at least, it would allow ranges to always be ordered, with the only
> difference being whether it went start-to-end or end-to-start,
> determined by the stride's sign.

This is no different in principle from

     (0..<200).striding(by: -2)

Again, I wasn't trying to suggest any of the solutions listed there.
The point I was making was that we don't have enough information to
design more than

    (0..<200).striding(by: -2)

> It would also avoid the need for additional range operators. The main
> reason you would need `>..` is so you could say
> `array.endIndex>..array.startIndex`, but by using the sign to decide
> which direction to stride over the range, you instead stride over
> `array.startIndex..<array.endIndex`, which is exactly what we already
> have.
>
> Unfortunately, moving away from `stride(from:to/through:by:)` would
> kind of mess up an idea I've been developing for providing an
> "induction sequence" to replace the more complicated C-style for use
> cases, but I suppose that's the way it goes...
>
> (Link to that: https://gist.github.com/brentdax/b24dd89a770d9fe376984498d3185187)

-- 
Dave


More information about the swift-evolution mailing list