<div dir="ltr">I like Sean’s idea.<div><br></div><div>Nevin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Sep 28, 2016 at 2:34 PM, Sean Heber via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This might just be me being silly, but is there any way to be able to do something like this instead:<br>
<br>
for (index, value) in sequence {<br>
}<br>
<br>
Maybe by adding another variant of makeIterator() that only differs by the return type or something like that?<br>
<br>
I sort of dislike that enumerated() and indexed() would co-exist and potentially lead to really subtle bugs when getting them confused. Obviously removing enumerated() would be a breaking change, though, and maybe it has valuable uses that I’m not really thinking about (although it seems to me that the index/value pair is what you want like, 99% of the time and plenty of people - myself included - have been using the index of enumerated() as an array index even though that’s technically maybe not quite ‘correct&#39;).<br>
<br>
l8r<br>
Sean<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
&gt; On Sep 28, 2016, at 12:55 PM, Erica Sadun via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt;<br>
&gt; Gist here: <a href="https://gist.github.com/erica/2b2d92e6db787d001c689d3e37a7c3f2" rel="noreferrer" target="_blank">https://gist.github.com/erica/<wbr>2b2d92e6db787d001c689d3e37a7c3<wbr>f2</a><br>
&gt;<br>
&gt; Introducing indexed() collections<br>
&gt;<br>
&gt;       • Proposal: TBD<br>
&gt;       • Author: Erica Sadun, Nate Cook, Jacob Bandes-Storch, Kevin Ballard<br>
&gt;       • Status: TBD<br>
&gt;       • Review manager: TBD<br>
&gt; Introduction<br>
&gt;<br>
&gt; This proposal introduces indexed() to the standard library, a method on collections that returns an (index, element) tuple sequence.<br>
&gt;<br>
&gt; Swift-evolution thread: TBD<br>
&gt;<br>
&gt; Motivation<br>
&gt;<br>
&gt; The standard library&#39;s enumerated() method returns a sequence of pairs enumerating a sequence. The pair&#39;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 an Array index 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 use enumerated()-based integer subscripting with non-zero-based array slices.<br>
&gt;<br>
&gt; Indices have a specific, fixed meaning in Swift, which are used to create valid collection subscripts. This proposal introduces indexed() to produce a more semantically relevant sequence by pairing a collection&#39;s indices with its members. While it is trivial to create a solution in Swift, the most common developer approach shown here calculates indexes twice:<br>
&gt;<br>
&gt; extension Collection {<br>
&gt;     /// Returns a sequence of pairs (*idx*, *x*), where *idx* represents a<br>
&gt;     /// consecutive collection index, and *x* represents an element of<br>
&gt;     /// the sequence.<br>
&gt;     func indexed() -&gt; Zip2Sequence&lt;Self.Indices, Self&gt; {<br>
&gt;         return zip(indices, self)<br>
&gt;     }<br>
&gt; }<br>
&gt;<br>
&gt; 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 &quot;obvious&quot; solution.<br>
&gt;<br>
&gt; Detailed Design<br>
&gt;<br>
&gt; Our vision of indexed() bypasses duplicated index generation with their potentially high computation costs. We&#39;d create an iterator that calculates each index once and then applies that index to subscript the collection. Implementation would take place through IndexedSequence, similar to EnumeratedSequence.<br>
&gt;<br>
&gt; Impact on Existing Code<br>
&gt;<br>
&gt; This proposal is purely additive and has no impact on existing code.<br>
&gt;<br>
&gt; Alternatives Considered<br>
&gt;<br>
&gt; Not yet<br>
</div></div><div class="HOEnZb"><div class="h5">&gt; ______________________________<wbr>_________________<br>
&gt; swift-evolution mailing list<br>
&gt; <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br>
______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
</div></div></blockquote></div><br></div>