<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">> 2) for var i = length; --i >= 0; { ...<br>
><br>
> This is a "count-down" loop, and isn't handled that well in Swift. The "guessing" translation is both wrong and fails to compile ("for i in length ... 0"), another attempt compiles but is easy to get wrong:<br>
><br>
> for length.stride(to: 0, by: -1) { // wrong, includes length but not zero<br>
> for length.stride(through: 0, by: -1) { // wrong, still includes length<br>
> for (length - 1).stride(through: 0, by: -1) { // works, but people have switched back to Java by now<br>
><br>
> Probably the best practice is to use reverse() on the range, since then you'd be using the same method on a "range literal", the indices range, or a collection:<br>
><br>
> for (0 ..< length).reverse() { ...<br>
><br></blockquote><div><br></div><div>Have there been any ideas for addressing this in the standard library? Perhaps a modification to Range to allow reverse intervals, or perhaps a ReverseRange type, and a new operator ">.." which would mean the reverse of "..<" ?</div><div><br></div><div>It seems "<span>for</span><span> i </span><span>in</span><span> </span><span>ReverseCollection</span><span>(</span><span>a</span><span>..<</span><span>b</span><span>)" currently works, though it looks a little weird. Of course Nate's example "(a..<b).reverse()" works too, but it's far too many parentheses for my taste.</span></div>
</div></div></div>