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)
}

> On 19 Dec 2015, at 18:59, Gavin Eadie via swift-dev <swift-dev@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@swift.org
> https://lists.swift.org/mailman/listinfo/swift-dev

_______________________________________________
swift-dev mailing list
swift-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-dev

Reply via email to