<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 23 Mar 2016, at 14:55, Jeremy Pereira via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><br class=""><blockquote type="cite" class="">On 23 Mar 2016, at 11:38, Brent Royal-Gordon <<a href="mailto:brent@architechies.com" class="">brent@architechies.com</a>> wrote:<br class=""><br class=""><blockquote type="cite" class="">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. <br class=""></blockquote><br class="">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.)<br class=""></blockquote><br class="">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<br class=""><br class=""> for i in someIntegerSequence.whileTrue({ i < 5 }) { … } <br class=""><br class="">was understandably a compiler error. <br class=""><br class=""><blockquote type="cite" class=""><br class="">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.<br class=""></blockquote><br class="">True, but we could have argued the same about the `where` keyword and that is in existence. :-)<br class=""></div></blockquote></div><br class=""><div class="">Speaking of where, why wouldn’t you just do:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>for eachElement in theIntegerSequence where eachElement < 5 { … }</font></div><div class=""><br class=""></div><div class="">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:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>var step = 0</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>for (var i = 0; i < 100; i += step) { step += 1 }</font></div><div class=""><br class=""></div><div class="">But I think that’s unusual enough that it doesn’t really matter.</div></body></html>