[swift-evolution] [Draft]: Introducing a striding(by:) method on 3.0 ranges
davesweeris at mac.com
davesweeris at mac.com
Sat Apr 9 18:45:53 CDT 2016
> On Apr 9, 2016, at 5:58 PM, Haravikk via swift-evolution <swift-evolution at swift.org> wrote:
>
>
>> On 9 Apr 2016, at 10:50, Xiaodi Wu <xiaodi.wu at gmail.com <mailto:xiaodi.wu at gmail.com>> wrote:
>>
>> But, ranges are useful for more than just stride, and so I would say it doesn't make sense to have an intrinsic direction for Range, which pointedly has lowerBound and upperBound but not start and end. For sample, there shouldn't be (0...9).contains(1) and (9...0).contains(1).
>
> While I can appreciate that, I wonder if it’s really that important that 0…9 and 9…0 are identical? As long as we can be clear on which direction of range we want to generate (so probably still need two new operators), can tell what the direction is, and can convert between them if we need to for some reason (reverse should be fine for that?), then I think we’re okay.
>
> i.e- 9 … 0 would still cause an error at compile or run-time, we’d have some other operator for doing that, not sure what, plus 9 ..> 0, with both explicitly creating ranges in the reverse direction to avoid mistakes with the order or for computed indices. When it comes down to it the current Range has an implicitly forward direction, so I don’t see the problem with having the same in reverse personally.
>
>
> I dunno, at the very least we might want to consider overloading the striding method as .striding(forwardBy:) and .striding(backwardBy:) or something similar, each taking a positive value of some kind to help avoid mistakes in cases where the stride size is computed rather than constant, this would make it more explicit at least.
I can’t imagine any scenario in which getting the step's sign wrong wouldn’t just be 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
(OTOH, I don’t understand why 6…0 is currently a crash, rather than the, IMHO, obviously intended sequence of “6,5,4,3,2,1,0”, so maybe I’m not the best person to ask)
- Dave Sweeris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160409/11661a10/attachment.html>
More information about the swift-evolution
mailing list