<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On 28 Sep 2016, at 23:03, Kevin Ballard &lt;<a href="mailto:kevin@sb.org" class="">kevin@sb.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">On Wed, Sep 28, 2016, at 02:02 PM, Tim Vermeulen wrote:<br class=""><blockquote type="cite" class=""><br class=""><blockquote type="cite" class="">On 28 Sep 2016, at 22:57, Kevin Ballard &lt;<a href="mailto:kevin@sb.org" class="">kevin@sb.org</a>&gt; wrote:<br class=""><br class="">On Wed, Sep 28, 2016, at 01:54 PM, Tim Vermeulen wrote:<br class=""><blockquote type="cite" class=""><br class=""><blockquote type="cite" class="">On 28 Sep 2016, at 22:46, Kevin Ballard &lt;<a href="mailto:kevin@sb.org" class="">kevin@sb.org</a>&gt; wrote:<br class=""><br class="">That's a bunch of complexity for no benefit. Why would you ever use this as a collection?<br class=""></blockquote><br class="">I think there is a benefit. Something like `collection.indexed().reversed()` would benefit from that, and I think that could be useful.<br class=""></blockquote><br class="">Perhaps, though you could just say `collection.reversed().indexed()` instead.<br class=""></blockquote><br class="">This isn’t necessarily the same though, is it? The reversed collection might use different indices than the original collection.<br class=""></blockquote><br class="">Either way you write it you're dealing with reversed indices.<br class=""></div></div></blockquote><div><br class=""></div><div>`collection.indexed().reversed()` will contain indices from the original collection (but in reversed order). `collection.reversed().indexed()` will contain indices from the collection returned by `reversed()`, which may have a different type than `Base.Index`. It’s a distinction.</div><div><br class=""></div><div>This would compile:</div><div><br class=""></div><div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> characters = </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"Swift"</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">characters</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">for</span><span style="font-variant-ligatures: no-common-ligatures" class=""> (index, character) </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">in</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">characters</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">indexed</span><span style="font-variant-ligatures: no-common-ligatures" class="">().</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">reversed</span><span style="font-variant-ligatures: no-common-ligatures" 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" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">print</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">characters</span><span style="font-variant-ligatures: no-common-ligatures" class="">[index], character)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div><br class=""></div><div>This wouldn’t:</div><div><br class=""></div><div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> characters = </span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"Swift"</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">characters</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">for</span><span style="font-variant-ligatures: no-common-ligatures" class=""> (index, character) </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">in</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">characters</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81" class="">reversed</span><span style="font-variant-ligatures: no-common-ligatures" class="">().</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">indexed</span><span style="font-variant-ligatures: no-common-ligatures" 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" class="">&nbsp; &nbsp; print(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">characters</span><span style="font-variant-ligatures: no-common-ligatures" class="">[index], character)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class="">-Kevin<br class=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class="">The whole point is to be used in a for loop. If it was a collection then you'd need to have an index for that collection, so now you have an index that lets you get the index for another collection, which is pretty useless because you could just be using that underlying index to begin with.<br class=""></blockquote><br class="">Rather than introducing a new index for this, we can simply use the index of the base collection for subscripting.<br class=""></blockquote><br class="">That's actually a good idea, and if we do make it a collection this is probably how we should handle it. But I still argue that the ability to make something a collection doesn't mean it should be a collection, if there's no good reason for anyone to actually try to use it as such.<br class=""><br class="">-Kevin<br class=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class="">On Wed, Sep 28, 2016, at 01:38 PM, Tim Vermeulen via swift-evolution wrote:<br class=""><blockquote type="cite" class="">+1 for `indexed()`, but I’m not sure about `IndexedSequence`. Why not `IndexedCollection`, which could also conform to Collection? With conditional conformances to BidirectionalCollection and RandomAccessCollection. This wouldn’t penalise the performance with respect to a simple `IndexedSequence`, would it?<br class=""><br class=""><blockquote type="cite" class="">Gist here:<a href="https://gist.github.com/erica/2b2d92e6db787d001c689d3e37a7c3f2" class="">https://gist.github.com/erica/2b2d92e6db787d001c689d3e37a7c3f2</a><br class=""><br class="">Introducingindexed()collections<br class="">Proposal: TBD<br class="">Author:Erica Sadun(<a href="https://github.com/erica" class="">https://github.com/erica</a>),Nate Cook(<a href="https://github.com/natecook1000" class="">https://github.com/natecook1000</a>),Jacob Bandes-Storch(<a href="https://github.com/jtbandes" class="">https://github.com/jtbandes</a>),Kevin Ballard(<a href="https://github.com/kballard" class="">https://github.com/kballard</a>)<br class="">Status: TBD<br class="">Review manager: TBD<br class=""><br class="">Introduction<br class=""><br class="">This proposal introducesindexed()to the standard library, a method on collections that returns an (index, element) tuple sequence.<br class=""><br class=""><br class="">Swift-evolution thread:TBD(<a href="https://gist.github.com/erica/tbd" class="">https://gist.github.com/erica/tbd</a>)<br class=""><br class="">Motivation<br class=""><br class="">The standard library'senumerated()method returns a sequence of pairs enumerating a sequence. The pair's first member is a monotonically incrementing integer starting at zero, and the second member is the corresponding element of the sequence. When working with arrays, the integer is coincidentally the same type and value as anArrayindex but the enumerated value is not generated with index-specific semantics. This may lead to confusion when developers attempt to subscript a non-array collection with enumerated integers. It can introduce serious bugs when developers useenumerated()-based integer subscripting with non-zero-based array slices.<br class=""><br class=""><br class="">Indices have a specific, fixed meaning in Swift, which are used to create valid collection subscripts. This proposal introducesindexed()to produce a more semantically relevant sequence by pairing a collection'sindiceswith its members. While it is trivial to create a solution in Swift, the most common developer approach shown here calculates indexes twice:<br class=""><br class="">extension Collection { &nbsp;&nbsp;/// Returns a sequence of pairs (*idx*, *x*), where *idx* represents a &nbsp;&nbsp;/// consecutive collection index, and *x* represents an element of &nbsp;&nbsp;/// the sequence. &nbsp;&nbsp;func indexed() -&gt;Zip2Sequence&lt;Self.Indices, Self&gt;{ &nbsp;&nbsp;&nbsp;&nbsp;return zip(indices, self) &nbsp;&nbsp;} }<br class=""><br class="">Incrementing an index in some collections can be unnecessarily costly. In a lazy filtered collection, an index increment is potentially O(N). We feel this is better addressed introducing a new function into the Standard Library to provide a more efficient design that avoids the attractive nuisance of the "obvious" solution.<br class=""><br class="">Detailed Design<br class=""><br class="">Our vision ofindexed()bypasses duplicated index generation with their potentially high computation costs. We'd create an iterator that calculates each index once and then applies that index to subscript the collection. Implementation would take place throughIndexedSequence, similar toEnumeratedSequence.<br class=""><br class="">Impact on Existing Code<br class=""><br class="">This proposal is purely additive and has no impact on existing code.<br class=""><br class="">Alternatives Considered<br class="">Not yet<br class=""><br class=""><br class=""><br class=""><br class=""></blockquote>_______________________________________________<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=""></blockquote></blockquote><br class=""></blockquote></blockquote><br class=""></blockquote></div></div></blockquote></div><br class=""></body></html>