<div dir="ltr">Dave, perhaps we could use &quot;^&quot; as an anchor point for the start index and $ as the anchor point for the end index? It&#39;s familiar to anyone who knows a bit of regex, and all vim users. My main worry would be ^ is already infix xor operator.</div><br><div class="gmail_quote"><div dir="ltr">On Fri, Dec 18, 2015 at 5:43 PM Paul Ossenbruggen via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">I would like to avoid what you currently have to do for iterating a subcontainer. <div><br></div><div>for a in container[0..container.count-4] {</div><div><span style="white-space:pre-wrap">        </span>// do something. </div><div>}</div><div><br></div><div>The slicing syntax would certainly help in these common situations. Maybe there are easy ways that I am not aware of. </div></div><div style="word-wrap:break-word"><div><br></div><div>- Paul</div></div><div style="word-wrap:break-word"><div><br><div><blockquote type="cite"><div>On Dec 18, 2015, at 2:39 PM, Dave Abrahams via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><blockquote type="cite"><div><br>On Dec 18, 2015, at 1:46 PM, Joe Groff via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div><div style="font-family:AvenirNext-Regular;font-size:15px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><blockquote type="cite"><div><br>On Dec 18, 2015, at 4:42 AM, Amir Michail via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div><div style="word-wrap:break-word">Examples:<div><br></div><div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo">&gt;&gt;&gt; l=[1,2,3,4,5]</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo">&gt;&gt;&gt; l[-1]</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo">5</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo">&gt;&gt;&gt; l[-2]</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo">4</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo">&gt;&gt;&gt; l[2:4]</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo">[3, 4]</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo">&gt;&gt;&gt; l[2:]</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo">[3, 4, 5]</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo">&gt;&gt;&gt; l[-2:]</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo">[4, 5]</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo">&gt;&gt;&gt; l[:3]</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo">[1, 2, 3]</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo">&gt;&gt;&gt; l[::2]</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo">[1, 3, 5]</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo">&gt;&gt;&gt; l[::]</div><div style="margin:0px;font-size:16px;line-height:normal;font-family:Menlo">[1, 2, 3, 4, 5]</div></div></div></div></blockquote><br></div><div style="font-family:AvenirNext-Regular;font-size:15px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">Accepting negative indices is problematic for two reasons: it imposes runtime overhead in the index operation to check the sign of the index; also, it masks fencepost errors, since if you do foo[m-n] and n is accidentally greater than m, you&#39;ll quietly load the wrong element instead of trapping. I&#39;d prefer something like D&#39;s `$-n` syntax for explicitly annotating end-relative indexes.</div></div></blockquote><br></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">Yes, we already have facilities to do most of what Python can do here, but one major problem IMO is that the “language” of slicing is so non-uniform: we have [a..&lt;b], dropFirst, dropLast, prefix, and suffix.  Introducing “$” for this purpose could make it all hang together<span style="font-family:AvenirNext-Regular"> and also eliminate the “why does it have to be so hard to look at the 2nd character of a string?!” problem.  That is, use the identifier “$” (yes, that’s an identifier in Swift) to denote the beginning-or-end of a collection.  Thus,</span></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div style="font-family:AvenirNext-Regular"><br></div><div style="font-family:AvenirNext-Regular"><font face="Menlo">  c[c.startIndex.advancedBy(3)] =&gt;<span style="white-space:pre-wrap">        </span>c[$+3]        // Python: c[3]</font></div><div style="font-family:AvenirNext-Regular"><div><font face="Menlo">  c[c.endIndex.advancedBy(-3)] =&gt;<span style="white-space:pre-wrap">        </span>c[$-3]        // Python: c[-3]</font></div><div></div><div><span style="font-family:Menlo">  c.dropFirst(3)  =&gt;</span><span style="font-family:Menlo;white-space:pre-wrap">                        </span><span style="font-family:Menlo">c[$+3...]     // Python: c[3:]</span></div></div><div style="font-family:AvenirNext-Regular"><font face="Menlo">  c.dropLast(3) =&gt;<span style="white-space:pre-wrap">                        </span>c[..&lt;$-3]     // Python: c[:-3]</font></div><div style="font-family:AvenirNext-Regular"><font face="Menlo">  c.prefix(3) =&gt;<span style="white-space:pre-wrap">                        </span>c[..&lt;$+3]     // Python: c[:3]</font></div><div style="font-family:AvenirNext-Regular"><font face="Menlo">  c.suffix(3) =&gt; <span style="white-space:pre-wrap">                        </span>c[$-3...]     // Python: c[-3:]</font></div><div style="font-family:AvenirNext-Regular">   </div></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:AvenirNext-Regular">It even has the nice connotation that, “this might be a little more expen</span><font face="Menlo">$</font><span style="font-family:AvenirNext-Regular">ive than plain indexing” (which it might, for non-random-access collections).  </span>I think the syntax is still a bit heavy, not least because of “..&lt;“ and “...”, but the direction has potential. </div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br></div><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"> I haven’t had the time to really experiment with a design like this; the community might be able to help by prototyping and using some alternatives.  You can do all of this outside the standard library with extensions.</div><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">-Dave<div><br></div><br></div><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=emrIhnP1hIf76Foxxv4NNJQX-2FWhcznESwKBSwD1MEwyj0iXgKJmkONj9Rbnwt3q2dEUDPmlaWgQO0ZpHBFxUxtruLEXt6cSO2nPi8zqkNon0c9o0Q5EsF2-2FQXAKvkmtBQyQYHq64ld1UumiHyAexfaduXkigo8nARwHpoJDbqPdMRb2UKj4RB-2FRz7fR34BiY0Rhn8B5-2BbU7da-2FqjAABF-2FKaF3FxfGW27qMZJYGv7o5I-3D" alt="" width="1" height="1" border="0" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;min-height:1px!important;width:1px!important;border-width:0px!important;margin:0px!important;padding:0px!important"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important"><span> </span></span><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">_______________________________________________</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">swift-evolution mailing list</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><a href="mailto:swift-evolution@swift.org" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">swift-evolution@swift.org</a><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=8CZIdLciSFC-2BO5jF-2FiP8qN7dBFsgCUZ50wdTsolcRPex1NHvgVUPj30eqJZg2O-2BdgmN1nwDigHsC35SEyG0FPu8fkFgBn8QnwbSSx03OSw4-2FM69V-2BAQB-2FIweTGSIeu0r93FelAyiVcphQAXC63JR-2BYy159w0sGQ-2Bu7i8s8fC6TyUPyEOvPiI3lto92tQkZoHvIq9XEvU5rzAmsA2TmP30sQ5e5qELFgEGoG-2F9vBdTJc-3D" alt="" width="1" height="1" border="0" style="min-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">
</div>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">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>
</blockquote></div>