[swift-dev] C-style For Loops
Donnacha OisÃn Kidney
oisin.kidney at gmail.com
Sat Dec 19 13:44:18 CST 2015
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 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
> https://lists.swift.org/mailman/listinfo/swift-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20151219/2841442b/attachment.html>
More information about the swift-dev
mailing list