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

davesweeris at mac.com davesweeris at mac.com
Sat Apr 9 22:48:29 CDT 2016


Oh, right, we’re talking about Swift 3.0… I really need to get that up and running on my computer.


> On Apr 9, 2016, at 8:58 PM, Dave Abrahams <dabrahams at apple.com> wrote:
> 
> 
> on Sat Apr 09 2016, davesweeris-AT-mac.com wrote:
> 
>>    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<>, 
> 
> .reversed() returns a ReversedCollection when the underlying collection
> is bidirectional:
> 
> https://github.com/apple/swift/blob/swift-3-indexing-model/stdlib/public/core/Reverse.swift#L250
> 
> That's lazy and cheap.
> 
>> 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
>> 
> 
> -- 
> Dave



More information about the swift-evolution mailing list