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

davesweeris at mac.com davesweeris at mac.com
Sat Apr 9 18:29:34 CDT 2016


> On Apr 9, 2016, at 4:33 AM, Haravikk via swift-evolution <swift-evolution at swift.org> wrote:
> 
> While I’m in favour of the basic idea I think the operator selection is too complex, and I’m not sure about the need for negative strides. Really all I want are the following:
> 
> 	(0 ... 6).striding(by: 2)	// [0, 2, 4, 6]		x from 0 to 6
> 	(0 ..< 6).striding(by: 2)	// [0, 2, 4]		x from 0 while <6
> 	(6 ... 0).striding(by: 2)	// [6, 4, 2, 0]		x from 6 to 0
> 	(6 ..> 0).striding(by: 2)	// [6, 4, 2]		x from 6 while >0
> 
> Everything else should be coverable either by flipping the order, or using .reverse(). The main advantage is that there’s only one new operator to clarify the 6 ..> 0 case, though you could always just reuse the existing operator if you just interpret it as “x from 6 to, but not including, 0"

`.reverse()` returns an array, though, not a StrideTo<>, which means it’ll get in an infinite loop on infinite sequences. This works fine:
for i in stride(from: 0.0, to: Double.infinity, by: M_PI) {
    if someTestInvolving(i) { break }
    ...
}

But this never even starts executing the loop because of the infinite loop inside `.reverse()`:
for i in stride(from: -Double.infinity, to: 0.0, by: M_PI).reverse() {
    if someTestInvolving(i) { break }
    ...
}

- Dave Sweeris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160409/335e226a/attachment.html>


More information about the swift-evolution mailing list