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

Xiaodi Wu xiaodi.wu at gmail.com
Wed Apr 6 12:57:50 CDT 2016


I think a lightbulb just went on for me:

You're talking about expressing something in the vein of
`(0..<200).striding(by: -2)`, which has I'm sure many use cases, and which
isn't straightforward to express with the current free function--I hadn't
considered that.

Meanwhile, I was trying to talk about something like `stride(from: 200, to:
0, by: -2)`, which is easily expressed today but isn't straightforward at
all to preserve with only ranges. Clearly, given that this is what's on
offer currently, someone who designed the language thinks (or thought) it's
of some use.

In the absence of information as to which is more in demand, couldn't we
have both? If it must be a method on a range, then I would advocate for
having what seems to be an utterly reasonable set of options for striding
backwards:

```
(0...200).striding(by: -2) // [a, b]
(0..<200).striding(by: -2) // [a, b)
(0<..200).striding(by: -2) // (a, b]
```
On Wed, Apr 6, 2016 at 12:10 PM Dave Abrahams via swift-evolution <
swift-evolution at swift.org> wrote:

>
> 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
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160406/fb56468b/attachment.html>


More information about the swift-evolution mailing list