<html><head></head><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue-Light, Helvetica Neue Light, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px"><div id="yui_3_16_0_1_1474441093361_6256"><span id="yui_3_16_0_1_1474441093361_8022">Hi,</span></div><div id="yui_3_16_0_1_1474441093361_6257"><span><br></span></div><div id="yui_3_16_0_1_1474441093361_6259"><span id="yui_3_16_0_1_1474441093361_6258">I'm a Swift newbie so forgive me if my question sounds a bit silly, or perhaps had been asked previously. My question is, why Swift doesn't have built-in operator for backward or decremented range? Especially to be used in for-in loop.</span></div><div id="yui_3_16_0_1_1474441093361_6259"><span><br></span></div><div id="yui_3_16_0_1_1474441093361_6259"><span id="yui_3_16_0_1_1474441093361_6334">Swift only has&nbsp;... (closed range) and ..&lt; (half-opened range) that both go forward (incremented). To use backward (decremented) range, we have to use several techniques (stride, sequence, etc). Of course those techniques work well. But I don't think they're very swifty, so to say.</span></div><div id="yui_3_16_0_1_1474441093361_6259"><span><br></span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span id="yui_3_16_0_1_1474441093361_6496">So, is it possible to propose a new operator and a modification to existing operator regarding range? Would such proposal be considered and implemented after Swift 3? If it is, here's my pre-proposal about it.</span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span><br></span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span id="yui_3_16_0_1_1474441093361_6497">1. Add new ..&gt; operator that goes backward (decremented) as companion to ..&lt; operator that goes forward (incremented).</span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span><br></span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span>Example:</span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span>&nbsp; for i in 1 ..&lt; 5 { print(i) }</span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span>will prints: 1 2 3 4</span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span>&nbsp; for i in 5 ..&gt; 1</span>&nbsp;{ print(i) }</div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span>will prints: 5 4 3 2</span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span><br></span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span id="yui_3_16_0_1_1474441093361_6699">2. Modify the ... operator to be able to go both ways. If the left operand is greater than the right, it goes backward (decremented). While if the left operand is less than the right, it goes forward (incremented).</span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span><br></span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span id="yui_3_16_0_1_1474441093361_6640">Example:</span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span id="yui_3_16_0_1_1474441093361_6645">&nbsp; for i in 1&nbsp;... 5</span>&nbsp;{ print(i) }</div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span id="yui_3_16_0_1_1474441093361_6646">will prints: 1 2 3 4 5</span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span id="yui_3_16_0_1_1474441093361_6647">&nbsp; for i in 5&nbsp;... 1</span>&nbsp;{ print(i) }</div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span>will prints: 5 4 3 2 1</span></div><div id="yui_3_16_0_1_1474441093361_6259"><span><br></span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span id="yui_3_16_0_1_1474441093361_7036">3. Add new pair of operator to accompany the ..&gt; and ..&lt; (half opened range on the right side) pair operator. It's &gt;.. and &lt;.. operators which are half opened range on the left side. As you might guess, the &gt;.. is the opposite of ..&gt; operator and the &lt;.. is the opposite of the ..&lt; operator.</span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span><br></span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span>Example:</span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span>&nbsp; for i in 1 &lt;.. 5 { print(i) }</span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span>will prints: 2 3 4 5</span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span>&nbsp; for i in 5 &gt;.. 1 { print(i) }</span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr">will prints: 4 3 2 1</div><div id="yui_3_16_0_1_1474441093361_6259"><span><br></span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span id="yui_3_16_0_1_1474441093361_7433">4. I would like to go even further by introducing a new attribute into for-in loop syntax. It's `step` keyword which is used to define the interval of the loop (like BASIC). Of course this additional attribute only works if the range is countable or indexable. If not, the compiler should complain.</span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span><br></span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span>Example:</span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span>&nbsp; for i in 1 ... 10 step 2</span>&nbsp;{ print(i) }</div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span>will prints: 1 3 5 7 9</span></div><div dir="ltr" id="yui_3_16_0_1_1474441093361_7513">&nbsp; for i in 10 ... 0 step 2 { print(i) }</div><div dir="ltr" id="yui_3_16_0_1_1474441093361_7514">will prints: 10 8 6 4 2 0</div><div dir="ltr" id="yui_3_16_0_1_1474441093361_7515"><div dir="ltr" style="margin-top: 0.1em; margin-bottom: 0.1em;" id="yui_3_16_0_1_1474441093361_7692">&nbsp; for i in 1 ..&lt; 9 step 3 { print(i) }</div><div dir="ltr" style="margin-top: 0.1em; margin-bottom: 0.1em;" id="yui_3_16_0_1_1474441093361_7693">will prints: 1 3 6&nbsp;</div><div dir="ltr" style="margin-top: 0.1em; margin-bottom: 0.1em;" id="yui_3_16_0_1_1474441093361_7693">&nbsp; // note: 9 is omitted since it's on the opened side.</div><div dir="ltr" id="yui_3_16_0_1_1474441093361_7694"><div dir="ltr" style="margin-top: 0.1em; margin-bottom: 0.1em;" id="yui_3_16_0_1_1474441093361_7724">&nbsp; for i in 9 &gt;.. 1 step 2 { print(i) }</div><div dir="ltr" style="margin-top: 0.1em; margin-bottom: 0.1em;" id="yui_3_16_0_1_1474441093361_7725">will prints: 7 5 3 1</div><div dir="ltr" id="yui_3_16_0_1_1474441093361_7726"><div dir="ltr" style="margin-top: 0.1em; margin-bottom: 0.1em;" id="yui_3_16_0_1_1474441093361_7766"><div dir="ltr" style="text-align: start; color: rgb(0, 0, 0); font-family: HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" id="yui_3_16_0_1_1474441093361_7780"></div></div><div dir="ltr" style="margin: 0.1em 0px; padding: 0px; text-align: start; display: block; color: rgb(0, 0, 0); font-family: HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" id="yui_3_16_0_1_1474441093361_7781">&nbsp; // note: 9 is omitted since it's on the opened side.</div><div dir="ltr" style="margin: 0.1em 0px; padding: 0px; text-align: start; display: block; color: rgb(0, 0, 0); font-family: HelveticaNeue-Light, 'Helvetica Neue Light', 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 16px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" id="yui_3_16_0_1_1474441093361_7781"><br></div></div></div></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span id="yui_3_16_0_1_1474441093361_6737">I hope you get the idea. I think such a rich for-in loop syntax would make Swift smarter, more robust, and easier to be learned and understood. They're also required since the flexibiliy of c-style for-loop is no longer available from Swift 3 and on. Decremented or backward range is sometimes needed in some algorithms. Making it built into the language would be better, instead of using non-language solutions. This proposal also doesn't break old codes since it doesn't change the old behavior.</span></div><div id="yui_3_16_0_1_1474441093361_6259"><span><br></span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span id="yui_3_16_0_1_1474441093361_6895">If such proposal is possible to be implemented —or at least considered— in the next version of Swift, I'll do the work of the formal proposal on GitHub. If it's not, well, I don't want to spend my time doing something that will be ignored.</span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span><br></span></div><div id="yui_3_16_0_1_1474441093361_6259" dir="ltr"><span>Thank you.</span></div><div id="yui_3_16_0_1_1474441093361_6259"><span><br></span></div><div></div><div id="yui_3_16_0_1_1474441093361_6092">Regards,&nbsp;</div><div class="signature" id="yui_3_16_0_1_1474441093361_7170"><div id="yui_3_16_0_1_1474441093361_7169"><br></div>–Mr Bee<div id="yui_3_16_0_1_1474441093361_7171"><br></div><div id="yui_3_16_0_1_1474441093361_7171">PS. I apologize if my English isn't well enough. I hope you all understand what I meant. :)</div><div id="yui_3_16_0_1_1474441093361_7171"><br></div></div></div></body></html>