[swift-dev] C-style For Loops

Wallacy wallacyf at gmail.com
Sat Dec 19 15:20:54 CST 2015


Curiously I was analyzing this part of the library a few minutes ago.
It will probably be necessary to extend "Range":

extension Range where Element: Strideable {
    func by(n: Element.Stride) -> StrideThrough<Element> {
        return startIndex.stride(through: endIndex, by: n)
    }
}

The first thing that came into my head was:

var rangeA = -150.0..<150 // HalfOpenInterval<Double>
var rangeB = -150.0...150 // ClosedInterval<Double>
var rangeC = -150..<150 // Range<Int>
var rangeD = -150...150 // Range<Int>

For "Range" we need to use startIndex and endIndex respectively, and with
HalfOpenInterval/ClosedInterval only start and end.

I do not know for you, but it seems inconsistent and redundant to me.


Em sáb, 19 de dez de 2015 às 18:39, Dave Abrahams via swift-dev <
swift-dev at swift.org> escreveu:

> 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>
> 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
>
>
>  _______________________________________________
> swift-dev mailing list
> swift-dev at swift.org
> https://lists.swift.org/mailman/listinfo/swift-dev
>
>
> -Dave
>
>
>
> _______________________________________________
> 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/893b966d/attachment.html>


More information about the swift-dev mailing list