<div dir="ltr">I&#39;m in support of a method of getting a sequence of (Index,Value) pairs (as you know from the other thread). I think I&#39;m leaning toward option three if it&#39;s equivalent to `zip(self.indices, self)`, ideally as a concise property like keys/values on a Dictionary.<div><br></div><div>I&#39;ve been using zip in production. I presumed enumerate was intended for enumerating indices, and that it had just been forgotten when the slice changes went through. I&#39;m in favour of removing it.<div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Apr 16, 2016 at 7:59 AM, Brent Royal-Gordon 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">A discussion in the &quot;mapValues&quot; thread reminded me of a longstanding issue I have with Swift.<br>
<br>
`enumerate()` is an attractive nuisance. (That is, it looks like the thing you want, but it&#39;s not actually the right one.) Most people use it because they want to enumerate over indices and elements at the same time. In reality, though, the first element of an `enumerate()` tuple is not the index—it&#39;s a monotonically increasing integer starting at 0. That *happens* to work for `Array`:<br>
<br>
&gt;         1&gt;     let array = Array(0..&lt;10)<br>
&gt;         2.     for (i, elem) in array.enumerate() {<br>
&gt;         3.         print(array[i])<br>
&gt;         4.     }<br>
&gt;       0<br>
&gt;       1<br>
&gt;       2<br>
&gt;       3<br>
&gt;       4<br>
&gt;       5<br>
&gt;       6<br>
&gt;       7<br>
&gt;       8<br>
&gt;       9<br>
<br>
But if you stray even a little bit from the golden path, things start to go wrong:<br>
<br>
&gt;         5&gt;     let range = array[2..&lt;8]<br>
&gt;         6.     for (i, elem) in range.enumerate() {<br>
&gt;         7.         print(range[i])<br>
&gt;         8.     }<br>
&gt;       fatal error: Index out of bounds<br>
<br>
You can scarcely blame users for making this mistake, though—&quot;The Swift Programming Language&quot; encourages the misconception. &quot;Iterating Over an Array&quot; in &quot;Collection Types&quot;:<br>
<br>
&gt; If you need the integer index of each item as well as its value, use the `enumerate()` method to iterate over the array instead. For each item in the array, the `enumerate()` method returns a tuple composed of the index and the value for that item.<br>
<br>
<br>
While the text is technically accurate—it only talks about iterating over arrays and the numbers generated by `enumerate()` happen to correspond to array indices—it creates a false implication that `enumerate()` is defined to return indices, which isn&#39;t true of other collections.<br>
<br>
This is made worse by the fact that `enumerate()` is not really a good name. It is not a common word, so people don&#39;t read it and immediately understand what it does; they memorize a Swift-specific meaning, and that meaning may incorporate the misconception that `enumerate()` includes indices. It is also not technically accurate: although it has &quot;number&quot; in its Latin roots, &quot;enumerate&quot; means either &quot;to count&quot; or &quot;to list&quot;, not &quot;to number&quot; (i.e. assign numbers to). I know `enumerate()` is used in a couple of other languages (certainly Python, possibly others), but I don&#39;t think that overrides the fact that it&#39;s confusing.<br>
<br>
I have three possible solutions to propose.<br>
<br>
<br>
<br>
OPTION ONE<br>
<br>
* Remove `enumerate()`.<br>
<br>
* Provide a type and postfix operator which allows you to write an infinite sequence as `0...`. (This might call a ClosedRange constructor which constrains the type to a protocol which provides `max`. This would imply that `FloatingPoint` and `Integer` protocols would need to conform to a common protocol declaring a `max` property, and in the case of `FloatingPoint`, `max` should probably be positive infinity.)<br>
<br>
* Fix-It calls to `x.enumerate()` as `zip(0..., x)`. This is more complicated to look at, but it&#39;s *far* more explicit about what the operation actually does. (For bonus points, we could perhaps look at how the EnumerateSequence is used, and if the number is used as an index, go with `zip(x.indices, x)` instead.)<br>
<br>
<br>
<br>
OPTION TWO<br>
<br>
* Rename `enumerate()` to something more explicit, like `withIntegers()` or `numbered()`. (It might even make sense to add a `from:` parameter which defaults to 0.)<br>
<br>
* Add to Collection an equivalent method with a similar name that provides indices, like `withIndices()` or `indexed()`.<br>
<br>
* Fix-It calls to `enumerate()` into either `numbered()` or `indexed()` (or whatever they end up being called) depending on the way they are used.<br>
<br>
<br>
<br>
OPTION THREE<br>
<br>
Combine the other two options:<br>
<br>
* Provide the infinite numeric sequence type from Option One.<br>
<br>
* Provide `numbered()` and `indexed()` (or whatever) methods which are explicitly typed to merely zip over the sequence/collection with an infinite integer sequence/Indices collection.<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Brent Royal-Gordon<br>
Architechies<br>
<br>
_______________________________________________<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/mailman/listinfo/swift-evolution</a><br>
</font></span></blockquote></div><br></div>