<div dir="ltr">I think I'd like to +1 a 'for x in loop(from: while: next:)'. (Possibly 'iterate' rather than 'loop'?)<div><br></div><div>I've not missed the C-style for-loop so I've not argued to keep it, but recently I was refactoring a function which started with a UIView and iterated up the hierarchy through the superview property, and it occurred to me recently that neither stride nor sequences/generators handle recursive iterations well.</div><div><br></div><div>So, I imagine that would look like this:</div><div>for view in loop(from: startingSubview, while: { $0 != nil }, next: { $0 = $0.superview })</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Apr 11, 2016 at 11:31 PM, Dave Abrahams via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
on Mon Apr 11 2016, Michel Fortin <<a href="http://michel.fortin-AT-michelf.ca" rel="noreferrer" target="_blank">michel.fortin-AT-michelf.ca</a>> wrote:<br>
<br>
> Le 11 avr. 2016 à 14:36, Dave Abrahams <<a href="mailto:dabrahams@apple.com">dabrahams@apple.com</a>> a écrit :<br>
><br>
>> 3. The fact that we're migrating C-style for loops to<br>
>> uses of stride, as noted in <a href="https://github.com/apple/swift/pull/2125" rel="noreferrer" target="_blank">https://github.com/apple/swift/pull/2125</a>,<br>
>> has convinced me that, sadly, we may need an answer that doesn't<br>
>> involve ranges. But maybe something like<br>
>><br>
>> for x in loop(from: 0.1, while: { $0 < 10 }, next: { $0 + .2 })<br>
>><br>
>> is sufficient for this purpose.<br>
><br>
> Please add that.<br>
<br>
</span>Please write a proposal and ideally, submit a patch :-).<br>
<br>
Seriously, if this is something you believe in, we could really use the<br>
help.<br>
<span class=""><br>
> First, it would relieve `stride` from some of the pressure of<br>
> excelling at replacing existing C-style for loops. But it would also<br>
> become pretty easy to write custom sequences like this one:<br>
><br>
> func uniform(start: Double, end: Double, numberOfSteps totalSteps: Int) -> Sequence {<br>
> var currentStep = 0<br>
> return loop(from: start, while: { _ in<br>
> currentStep < totalSteps<br>
> }, next: { _ in<br>
> currentStep += 1<br>
> return start * (Double(totalSteps-currentStep) / Double(totalSteps)) +<br>
> end * (Double(currentStep) / Double(totalSteps))<br>
> })<br>
> }<br>
<br>
</span>Aside from the fact that you can't return Sequence, this seems like a<br>
much better way to do that in Swift 3.0:<br>
<br>
func uniform(<br>
<span class=""> start: Double, end: Double, numberOfSteps totalSteps: Int<br>
</span>) -> LazyMapRandomAccessCollection<CountableRange<Int>, Double> {<br>
return (0..<totalSteps).lazy.map {<br>
start * (Double(totalSteps-$0) / Double(totalSteps)) +<br>
end * (Double($0) / Double(totalSteps))<br>
}<br>
}<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Dave<br>
</font></span><div class="HOEnZb"><div class="h5">_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</div></div></blockquote></div><br></div>