<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 <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><div class=""><div class=""><br class="">on Mon Apr 11 2016, Ross O'Brien <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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'. 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 < 10 }) {</span></div><div class=""><font face="Menlo" class=""> // ...</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=""> // ...</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=""><<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class=""> on Mon Apr 11 2016, Michel Fortin <<a href="http://michel.fortin-at-michelf.ca" class="">michel.fortin-AT-michelf.ca</a>> wrote:<br class=""><br class=""><blockquote type="cite" class="">Le 11 avr. 2016 à 14:36, Dave Abrahams<br class=""></blockquote> <<a href="mailto:dabrahams@apple.com" class="">dabrahams@apple.com</a>> 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 < 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=""> Please write a proposal and ideally, submit a patch :-).<br class=""><br class=""> Seriously, if this is something you believe in, we could really use the<br class=""> 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) -><br class=""></blockquote> Sequence {<br class=""><blockquote type="cite" class="">var currentStep = 0<br class="">return loop(from: start, while: { _ in<br class="">currentStep < 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=""> Aside from the fact that you can't return Sequence, this seems like a<br class=""> much better way to do that in Swift 3.0:<br class=""><br class=""> func uniform(<br class=""> start: Double, end: Double, numberOfSteps totalSteps: Int<br class=""> ) -> LazyMapRandomAccessCollection<CountableRange<Int>, Double> {<br class=""> return (0..<totalSteps).lazy.map {<br class=""> start * (Double(totalSteps-$0) / Double(totalSteps)) +<br class=""> end * (Double($0) / Double(totalSteps))<br class=""> }<br class=""> }<br class=""><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=""> <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>