[swift-evolution] A (better) Swift Equivalent For The Classical For-Loop With Numeric Scalars

Biala bialata at yahoo.com
Wed Mar 23 09:58:48 CDT 2016


Some code :
WAS:>>>>>>>>>>>>>>>>>>>>>>>>>var i:Int = 0for (i=0;i<len;i++){    //do something}
....
for (i=0;i<len;i++){    if something is true    {
        break    }
}
use i
>>>>>>>>>>>>>>>>>>>>>>>
Now without C style forafter mechanically repair the code
I get this
var i:Int = 0for i in (0..<len){    //do something}
....
for i in (0..<len)
{    if something is true    {
        break    }
}
use i ??What is i now :)  !!!!

  

    On Wednesday, March 23, 2016 1:38 PM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> 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.)

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.

-- 
Brent Royal-Gordon
Architechies

_______________________________________________
swift-evolution mailing list
swift-evolution at swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160323/beb3cf68/attachment.html>


More information about the swift-evolution mailing list