> On 23 Mar 2016, at 14:55, Jeremy Pereira via swift-evolution 
> <[email protected]> wrote:
> 
> 
>> On 23 Mar 2016, at 11:38, Brent Royal-Gordon <[email protected]> wrote:
>> 
>>> One advantage of the old C style for loop is that everything to do with 
>>> loop control is in one place, usually on one line. There is currently no 
>>> way of doing that for the (quite common) use case of iterating through a 
>>> sequence until a particular condition (other than the end of the sequence) 
>>> is true except by using a break. 
>> 
>> If you can stand using method chains, I believe that role would be filled by 
>> the `takeWhile(_:)` method that Kevin Ballard (IIRC) wants to add to 
>> Sequence. (Although `takeWhile(_:)` would be greedy by default.)
> 
> After writing the last email, I tried adding a method to SequenceType called 
> whileTrue(_:) that did pretty much the same thing. It wrapped a Generator in 
> another custom Generator that ended when the supplied closure returned true 
> and it worked fine. However, the closure had no visibility of the iterator so
> 
>    for i in someIntegerSequence.whileTrue({ i < 5 }) { … } 
> 
> was understandably a compiler error. 
> 
>> 
>> But honestly, other than distaste, I don't see much of a practical issue 
>> with putting an `if` or `guard` on the first line with a `break` in it. That 
>> still clusters the iteration logic at the top of the loop, even if it's not 
>> quite in a single statement.
> 
> True, but we could have argued the same about the `where` keyword and that is 
> in existence. :-)

Speaking of where, why wouldn’t you just do:

        for eachElement in theIntegerSequence where eachElement < 5 { … }

I think simplified striding is really all we need to cover c-style loop 
replacement, the only case that can’t be covered is one that uses some kind of 
variable stride amount, i.e- replacing:

        var step = 0
        for (var i = 0; i < 100; i += step) { step += 1 }

But I think that’s unusual enough that it doesn’t really matter.
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to