[swift-dev] C-style For Loops

Dave Abrahams dabrahams at apple.com
Sat Dec 19 14:39:42 CST 2015


> On Dec 19, 2015, at 11:44 AM, Donnacha Oisín Kidney via swift-dev <swift-dev at swift.org> wrote:
> 
> You can define an extension on interval types:
> 
> extension HalfOpenInterval where Bound: Strideable {
>   func by(n: Bound.Stride) -> StrideTo<Bound> {
>     return start.stride(to: end, by: n)
>   }
> }
> 
> extension ClosedInterval where Bound: Strideable {
>   func by(n: Bound.Stride) -> StrideThrough<Bound> {
>     return start.stride(through: end, by: n)
>   }
> }
> 
> Which maybe gives you slightly more elegant usage:
> 
> for lat in (CGFloat(-60)...60).by(30) {
>   print(lat)
> }
> 
> for lat in (CGFloat(-60)..<60).by(30) {
>   print(lat)
> }

This is nice; why don't we put it in the standard library and get rid of the "stride" methods?

>> On 19 Dec 2015, at 18:59, Gavin Eadie via swift-dev <swift-dev at swift.org <mailto:swift-dev at swift.org>> wrote:
>> 
>> With C-style for loops to be removed from the language to general acclaim (including mine), is the following the best way to perform the required looping:
>> 
>>     for lat in (CGFloat(-60.0)).stride(through: +60.0, by: 30.0) {
>>         path.moveToPoint(CGPointMake(-180.0, lat))
>>         path.lineToPoint(CGPointMake(+180.0, lat))
>>     }
>> 
>>     for lon in (CGFloat(-150.0)).stride(through: +150.0, by: 30.0) {
>>         path.moveToPoint(CGPointMake(lon, +90.0))
>>         path.lineToPoint(CGPointMake(lon, -90.0))
>>     }
>> 
>> That seems a slightly cumbersome usage.  Is there a better way to initialize the SequenceType I want to iterate over?
>> 
>> _______________________________________________
>> swift-dev mailing list
>> swift-dev at swift.org <mailto:swift-dev at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-dev <https://lists.swift.org/mailman/listinfo/swift-dev>
> 
>  _______________________________________________
> swift-dev mailing list
> swift-dev at swift.org <mailto:swift-dev at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-dev <https://lists.swift.org/mailman/listinfo/swift-dev>
-Dave



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20151219/548d94dc/attachment.html>


More information about the swift-dev mailing list