<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="">I would like to avoid what you currently have to do for iterating a subcontainer.&nbsp;<div class=""><br class=""></div><div class="">for a in container[0..container.count-4] {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// do something.&nbsp;</div><div class="">}</div><div class=""><br class=""></div><div class="">The slicing syntax would certainly help in these common situations. Maybe there are easy ways that I am not aware of.&nbsp;</div><div class=""><br class=""></div><div class="">- Paul</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 18, 2015, at 2:39 PM, Dave Abrahams via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><blockquote type="cite" class=""><div class=""><br class="Apple-interchange-newline">On Dec 18, 2015, at 1:46 PM, Joe Groff via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="font-family: AvenirNext-Regular; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><blockquote type="cite" class=""><div class=""><br class="Apple-interchange-newline">On Dec 18, 2015, at 4:42 AM, Amir Michail via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">Examples:<div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">&gt;&gt;&gt; l=[1,2,3,4,5]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">&gt;&gt;&gt; l[-1]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">5</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">&gt;&gt;&gt; l[-2]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">4</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">&gt;&gt;&gt; l[2:4]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">[3, 4]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">&gt;&gt;&gt; l[2:]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">[3, 4, 5]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">&gt;&gt;&gt; l[-2:]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">[4, 5]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">&gt;&gt;&gt; l[:3]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">[1, 2, 3]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">&gt;&gt;&gt; l[::2]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">[1, 3, 5]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">&gt;&gt;&gt; l[::]</div><div class="" style="margin: 0px; font-size: 16px; line-height: normal; font-family: Menlo;">[1, 2, 3, 4, 5]</div></div></div></div></blockquote><br class=""></div><div class="" style="font-family: AvenirNext-Regular; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 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'll quietly load the wrong element instead of trapping. I'd prefer something like D's `$-n` syntax for explicitly annotating end-relative indexes.</div></div></blockquote><br class=""></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">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. &nbsp;Introducing “$” for this purpose could make it all hang together<span class="" style="font-family: AvenirNext-Regular;">&nbsp;and also eliminate the “why does it have to be so hard to look at the 2nd character of a string?!” problem. &nbsp;That is, use the identifier “$” (yes, that’s an identifier in Swift) to denote the beginning-or-end of a collection. &nbsp;Thus,</span></div><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="" style="font-family: AvenirNext-Regular;"><br class=""></div><div class="" style="font-family: AvenirNext-Regular;"><font face="Menlo" class="">&nbsp; c[c.startIndex.advancedBy(3)] =&gt;<span class="Apple-tab-span" style="white-space: pre;">        </span>c[$+3] &nbsp; &nbsp; &nbsp; &nbsp;// Python: c[3]</font></div><div class="" style="font-family: AvenirNext-Regular;"><div class=""><font face="Menlo" class="">&nbsp; c[c.endIndex.advancedBy(-3)] =&gt;<span class="Apple-tab-span" style="white-space: pre;">        </span>c[$-3] &nbsp; &nbsp; &nbsp; &nbsp;// Python: c[-3]</font></div><div class=""></div><div class=""><span class="" style="font-family: Menlo;">&nbsp; c.dropFirst(3) &nbsp;=&gt;</span><span class="Apple-tab-span" style="font-family: Menlo; white-space: pre;">                        </span><span class="" style="font-family: Menlo;">c[$+3...] &nbsp; &nbsp; // Python: c[3:]</span></div></div><div class="" style="font-family: AvenirNext-Regular;"><font face="Menlo" class="">&nbsp; c.dropLast(3) =&gt;<span class="Apple-tab-span" style="white-space: pre;">                        </span>c[..&lt;$-3] &nbsp; &nbsp; // Python: c[:-3]</font></div><div class="" style="font-family: AvenirNext-Regular;"><font face="Menlo" class="">&nbsp; c.prefix(3) =&gt;<span class="Apple-tab-span" style="white-space: pre;">                        </span>c[..&lt;$+3] &nbsp; &nbsp; // Python: c[:3]</font></div><div class="" style="font-family: AvenirNext-Regular;"><font face="Menlo" class="">&nbsp; c.suffix(3) =&gt;&nbsp;<span class="Apple-tab-span" style="white-space: pre;">                        </span>c[$-3...] &nbsp; &nbsp; // Python: c[-3:]</font></div><div class="" style="font-family: AvenirNext-Regular;">&nbsp; &nbsp;</div></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span class="" style="font-family: AvenirNext-Regular;">It even has the nice connotation that, “this might be a little more expen</span><font face="Menlo" class="">$</font><span class="" style="font-family: AvenirNext-Regular;">ive than plain indexing” (which it might, for non-random-access collections). &nbsp;</span>I think the syntax is still a bit heavy, not least because of “..&lt;“ and “...”, but the direction has potential.&nbsp;</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">&nbsp;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. &nbsp;You can do all of this outside the standard library with extensions.</div><br class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">-Dave<div class=""><br class=""></div><br class="Apple-interchange-newline"></div><br class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 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; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; height: 1px !important; width: 1px !important; border-width: 0px !important; margin: 0px !important; padding: 0px !important;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class=""><span class="Apple-converted-space">&nbsp;</span></span><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">_______________________________________________</span><br style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">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; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><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; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">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; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><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; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br class=""></div></body></html>