<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 Apr 11, 2016, at 6:49 PM, Dave Abrahams via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><div class=""><div class=""><br class="">on Mon Apr 11 2016, Ross O'Brien &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class=""><blockquote type="cite" class="">I think I'd like to +1 a 'for x in loop(from: while: next:)'. (Possibly<br class="">'iterate' rather than 'loop'?)<br class=""></blockquote><br class="">Maybe 'iterations'. &nbsp;It should be a noun, I think.<br class=""></div></div></blockquote><div><br class=""></div><div><div class="">There's a proposal awaiting review right now that would add an <font face="Menlo" class="">iterate</font> function and <font face="Menlo" class="">takeWhile </font>sequence method, so these could be written as:</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">for x in iterate(0.1, apply: { $0 + 2 })</font><span style="font-family: Menlo;" class="">.takeWhile({ $0 &lt; 10 }) {</span></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; // ...</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><br class=""></div><div class="">and</div><div class=""><br class=""></div><div class=""><font face="Menlo" class="">for view in iterate(startingSubview, apply: { $0.superview })</font><span style="font-family: Menlo;" class="">.takeWhile({ $0 != nil }) {</span></div><div class=""><font face="Menlo" class="">&nbsp; &nbsp; // ...</font></div><div class=""><font face="Menlo" class="">}</font></div><div class=""><br class=""></div><div class="">If the <font face="Menlo" class="">iterate </font>function were overloaded with a <font face="Menlo" class="">while</font> argument, this would be exactly what we're discussing. Perhaps this proposal should be a jumping off point?</div><div class=""><br class=""></div><div class=""><a href="https://github.com/apple/swift-evolution/blob/master/proposals/0045-scan-takewhile-dropwhile.md" class="">https://github.com/apple/swift-evolution/blob/master/proposals/0045-scan-takewhile-dropwhile.md</a></div><div class=""><br class=""></div><div class="">Nate</div><div class=""><br class=""></div><div class=""><br class=""></div></div><blockquote type="cite" class=""><div class=""><div class=""><blockquote type="cite" class="">I've not missed the C-style for-loop so I've not argued to keep it, but recently<br class="">I was refactoring a function which started with a UIView and iterated up the<br class="">hierarchy through the superview property, and it occurred to me recently that<br class="">neither stride nor sequences/generators handle recursive iterations well.<br class=""><br class="">So, I imagine that would look like this:<br class="">for view in loop(from: startingSubview, while: { $0 != nil }, next: { $0 =<br class="">$0.superview })<br class=""><br class="">On Mon, Apr 11, 2016 at 11:31 PM, Dave Abrahams via swift-evolution<br class="">&lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class=""> &nbsp;&nbsp;&nbsp;on Mon Apr 11 2016, Michel Fortin &lt;<a href="http://michel.fortin-at-michelf.ca" class="">michel.fortin-AT-michelf.ca</a>&gt; wrote:<br class=""><br class=""><blockquote type="cite" class="">Le 11 avr. 2016 à 14:36, Dave Abrahams<br class=""></blockquote> &nbsp;&nbsp;&nbsp;&lt;<a href="mailto:dabrahams@apple.com" class="">dabrahams@apple.com</a>&gt; a écrit :<br class=""><blockquote type="cite" class=""><br class=""><blockquote type="cite" class="">3. The fact that we're migrating C-style for loops to<br class="">uses of stride, as noted in <a href="https://github.com/apple/swift/pull/2125" class="">https://github.com/apple/swift/pull/2125</a>,<br class="">has convinced me that, sadly, we may need an answer that doesn't<br class="">involve ranges. But maybe something like<br class=""><br class="">for x in loop(from: 0.1, while: { $0 &lt; 10 }, next: { $0 + .2 })<br class=""><br class="">is sufficient for this purpose.<br class=""></blockquote><br class="">Please add that.<br class=""></blockquote><br class=""> &nbsp;&nbsp;&nbsp;Please write a proposal and ideally, submit a patch :-).<br class=""><br class=""> &nbsp;&nbsp;&nbsp;Seriously, if this is something you believe in, we could really use the<br class=""> &nbsp;&nbsp;&nbsp;help.<br class=""><br class=""><blockquote type="cite" class="">First, it would relieve `stride` from some of the pressure of<br class="">excelling at replacing existing C-style for loops. But it would also<br class="">become pretty easy to write custom sequences like this one:<br class=""><br class="">func uniform(start: Double, end: Double, numberOfSteps totalSteps: Int) -&gt;<br class=""></blockquote> &nbsp;&nbsp;&nbsp;Sequence {<br class=""><blockquote type="cite" class="">var currentStep = 0<br class="">return loop(from: start, while: { _ in<br class="">currentStep &lt; totalSteps<br class="">}, next: { _ in<br class="">currentStep += 1<br class="">return start * (Double(totalSteps-currentStep) / Double(totalSteps)) +<br class="">end * (Double(currentStep) / Double(totalSteps))<br class="">})<br class="">}<br class=""></blockquote><br class=""> &nbsp;&nbsp;&nbsp;Aside from the fact that you can't return Sequence, this seems like a<br class=""> &nbsp;&nbsp;&nbsp;much better way to do that in Swift 3.0:<br class=""><br class=""> &nbsp;&nbsp;&nbsp;func uniform(<br class=""> &nbsp;&nbsp;&nbsp;start: Double, end: Double, numberOfSteps totalSteps: Int<br class=""> &nbsp;&nbsp;&nbsp;) -&gt; LazyMapRandomAccessCollection&lt;CountableRange&lt;Int&gt;, Double&gt; {<br class=""> &nbsp;&nbsp;&nbsp;return (0..&lt;totalSteps).lazy.map {<br class=""> &nbsp;&nbsp;&nbsp;start * (Double(totalSteps-$0) / Double(totalSteps)) +<br class=""> &nbsp;&nbsp;&nbsp;end * (Double($0) / Double(totalSteps))<br class=""> &nbsp;&nbsp;&nbsp;}<br class=""> &nbsp;&nbsp;&nbsp;}<br class=""><br class=""> &nbsp;&nbsp;&nbsp;--<br class=""> &nbsp;&nbsp;&nbsp;Dave<br class=""><br class=""> &nbsp;&nbsp;&nbsp;_______________________________________________<br class=""> &nbsp;&nbsp;&nbsp;swift-evolution mailing list<br class=""> &nbsp;&nbsp;&nbsp;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""> &nbsp;&nbsp;&nbsp;<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></blockquote><br class="">-- <br class="">Dave<br class=""><br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></div></blockquote></div><br class=""></body></html>