<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="">Personally, I think that the range operator is easier to understand than a free range function. People who already program <i class="">might</i> find it easier to understand a free function rather than <span style="font-family: Menlo; font-size: 11px;" class="">..<</span>, which looks like special syntax, but I think beginners (especially in Python, actually) are really floored by range functions. Take, for example:<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class="">range<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class="">5</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">)</span></div></div><div class=""><br class=""></div><div class="">To someone unfamiliar with the function, it could (quite reasonably) mean:</div><div class=""><br class=""></div><div class=""><ul class="MailOutline"><li class="">1, 2, 3, 4, 5</li><li class="">0, 1, 2, 3, 4, 5</li><li class="">0, 1, 2, 3, 4</li><li class="">None of the above</li></ul><div class=""><br class=""></div><div class="">In fact, for people unfamiliar with programming, I’d say that the correct answer is the <i class="">least</i> obvious. Compare that to:</div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class="">0</span>..<<span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class="">5</span></div></div><div class=""><br class=""></div><div class="">or:</div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class=""><br class=""></span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class="">0</span>...<span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class="">4</span></div></div><div class=""><br class=""></div><div class="">I think the operator is much easier to understand, and more than makes up for the disadvantages associated with extra operators.</div><div class=""><br class=""></div><div class="">That said, I <i class="">do</i> think that <span style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 11px;" class="">Strideable</span> is a little unclear. I can’t think of a much better option, though. Maybe something like:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Range</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">where</span> Element: Strideable {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">public</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> by(n: </span>Element<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">.</span>Stride<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) -> </span>StrideTo<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""><</span>Element<span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">startIndex</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">stride</span>(to: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">endIndex</span>, by: n)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 143, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class="">0</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">..<</span><span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class="">10</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">).</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">by</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #312cdd" class="">2</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">) </span>// [0, 2, 4, 6, 8]</div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(79, 143, 0);" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">But I’d also want that to work as a subscript. </div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On 8 Dec 2015, at 19:01, Kyle Bashour via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div style="font-size:13px" class="">Based on much of the discussion regarding removing C-style for-loops, I'd like to propose a discussion on revamping how range works in Swift. The lack of a reverse range operator and the fact and the range operator and stride() seem to do a lot of the same work have made me wonder why there isn't merely a range() function, as in Python. </div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">I believe this would be easier for newcomers to learn, remove the need for stride() (though there are probably use cases for stride() I don't know about, I haven't used it too much), and actually be more clear than ..< and ...</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">Here are some examples of how it could work:<br class=""></div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">range(10) // equivalent to 0..<10</div><div style="font-size:13px" class="">range(-1, to: 10) // equivalent to -1..<10</div><div style="font-size:13px" class="">range(10, through: 0) equivalent to (0...10).reverse()</div><div style="font-size:13px" class="">range(0, through: 10, by: 2) // equivalent to <span style="background-color:rgba(255,255,255,0)" class=""><span style="font-style:inherit;font-variant:inherit;margin:0px;padding:0px;border:0px;line-height:inherit;vertical-align:baseline" class="">0.stride</span><span style="font-style:inherit;font-variant:inherit;margin:0px;padding:0px;border:0px;line-height:inherit;vertical-align:baseline" class="">(through</span><span style="font-style:inherit;font-variant:inherit;margin:0px;padding:0px;border:0px;line-height:inherit;vertical-align:baseline" class="">:</span><span style="font-style:inherit;font-variant:inherit;margin:0px;padding:0px;border:0px;line-height:inherit;vertical-align:baseline" class=""> </span><span style="font-style:inherit;font-variant:inherit;margin:0px;padding:0px;border:0px;line-height:inherit;vertical-align:baseline" class="">10</span><span style="font-style:inherit;font-variant:inherit;margin:0px;padding:0px;border:0px;line-height:inherit;vertical-align:baseline" class="">,</span><span style="font-style:inherit;font-variant:inherit;margin:0px;padding:0px;border:0px;line-height:inherit;vertical-align:baseline" class=""> </span><span style="font-style:inherit;font-variant:inherit;margin:0px;padding:0px;border:0px;line-height:inherit;vertical-align:baseline" class="">by</span><span style="font-style:inherit;font-variant:inherit;margin:0px;padding:0px;border:0px;line-height:inherit;vertical-align:baseline" class="">:</span><span style="font-style:inherit;font-variant:inherit;margin:0px;padding:0px;border:0px;line-height:inherit;vertical-align:baseline" class=""> </span><span style="font-style:inherit;font-variant:inherit;margin:0px;padding:0px;border:0px;line-height:inherit;vertical-align:baseline" class="">2</span><span style="font-style:inherit;font-variant:inherit;margin:0px;padding:0px;border:0px;line-height:inherit;vertical-align:baseline" class="">)</span></span></div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">Or, to avoid a global function, .range() should probably be a function like stride, but with more features (equivalent to above)</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">10.range()</div><div style="font-size:13px" class="">-1.range(to: 10)</div><div style="font-size:13px" class="">10.range(through: 0)</div><div style="font-size:13px" class="">0.range(through: 10, by: 2)</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">Would love thoughts on why this is good or bad, and if it's worth creating an actual proposal. </div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">Regards,</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">Kyle</div><div class=""><br class=""></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=Lo8TP3b1oIn3yQXUt9zA1UCQfR-2BMBCuqnubTuDg47-2B0ImihsetnYqNMUweXMan6y5iHeeu-2FY8SamoqpKRR4fgIKCADtpvdNpMazLAN0pU-2BAB7t3azfRBNljAyJ-2BGgWnixQUVf4Oan06Kb4zOH7a3oVBnMgH-2F8EuolD-2B5aH-2BBNIZrZdr-2FY8xa6cyhDTa-2FrZ4S2R0N8jlniWAEjDDBoyPoUQoOjY8JcNKztXORXQ-2BuCY8-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" 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></blockquote></div><br class=""></div></body></html>