I use enumerated in many location in my code and have never expected it to be indexes but a counting of how many times I have looped. It says clearly what is does on the tin: "Returns a sequence of pairs (n, x), where n represents a consecutive integer starting at zero, and x represents an element of the sequence.". I get some new folks have confusion but reading the docs is always part of learning IMHO.<br><br>I would hate to see it removed without strong replacement that is reasonably readable. I think leveraging zip and the potential range style is on the edge of being readable but is learnable.<br><br>-Shawn<br><div class="gmail_quote"><div dir="ltr">On Fri, Feb 3, 2017 at 4:11 PM Erica Sadun via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br class="gmail_msg">
> On Feb 3, 2017, at 4:20 PM, Dave Abrahams <<a href="mailto:dabrahams@apple.com" class="gmail_msg" target="_blank">dabrahams@apple.com</a>> wrote:<br class="gmail_msg">
><br class="gmail_msg">
><br class="gmail_msg">
> on Fri Feb 03 2017, Erica Sadun <erica-AT-ericasadun.com> wrote:<br class="gmail_msg">
><br class="gmail_msg">
>>> On Feb 3, 2017, at 2:58 PM, Ben Cohen <<a href="mailto:ben_cohen@apple.com" class="gmail_msg" target="_blank">ben_cohen@apple.com</a>> wrote:<br class="gmail_msg">
>>><br class="gmail_msg">
>>><br class="gmail_msg">
>>>> On Feb 3, 2017, at 11:12 AM, Erica Sadun via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg">
>> <mailto:<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>>> wrote:<br class="gmail_msg">
>>>><br class="gmail_msg">
>><br class="gmail_msg">
>>>> I believe what people *want* is `indexed` over `enumerated`, and consistently for both array and array slices.<br class="gmail_msg">
>>>><br class="gmail_msg">
>>><br class="gmail_msg">
>>> I don’t know if that’s true.<br class="gmail_msg">
>>><br class="gmail_msg">
>>> Here’s an example (the only use of enumerated) from Alamofire:<br class="gmail_msg">
>>><br class="gmail_msg">
>>> let acceptLanguage = Locale.preferredLanguages.prefix(6).enumerated().map { index, languageCode in<br class="gmail_msg">
>>> let quality = 1.0 - (Double(index) * 0.1)<br class="gmail_msg">
>>> return "\(languageCode);q=\(quality)"<br class="gmail_msg">
>>> }.joined(separator: ", ")<br class="gmail_msg">
>>><br class="gmail_msg">
>>> Here the intent is a counter, not indices. They just happen to be the same. But if they’d used indexed() it would certainly hurt readability, albeit midly.<br class="gmail_msg">
>>><br class="gmail_msg">
>>> Suppose there wasn’t an enumerate or an indexed, and zipped was the standard way of doing it. That might lead to another solution:<br class="gmail_msg">
>>><br class="gmail_msg">
>>> let qualities = stride(from: 1.0, to: 0.4, by: -0.1)<br class="gmail_msg">
>>> let acceptLanguage = Locale.preferredLanguages.zipped(with: qualities).map {<br class="gmail_msg">
>>> languageCode, quality in "\(languageCode);q=\(quality)"<br class="gmail_msg">
>>> }.joined(separator: ", ")<br class="gmail_msg">
>>><br class="gmail_msg">
>>> The use of stride here feels more what was intended, rather than<br class="gmail_msg">
>>> backing into the quality via an “index” value. And avoids any risk<br class="gmail_msg">
>>> with indexed of this getting applied incorrectly to slices.<br class="gmail_msg">
>>><br class="gmail_msg">
>><br class="gmail_msg">
>> I think enumerated as it stands is an attractive nuisance / moral<br class="gmail_msg">
>> hazard. Most of the language learners I interact with miss the point<br class="gmail_msg">
>> and the nuance of how it works.<br class="gmail_msg">
>><br class="gmail_msg">
>> let list = [0, 1, 2, 3, 4]<br class="gmail_msg">
>> let slice = list[2...3]<br class="gmail_msg">
>> for (idx, value) in slice.enumerated() {<br class="gmail_msg">
>> print(idx, value)<br class="gmail_msg">
>> }<br class="gmail_msg">
>><br class="gmail_msg">
>> I think people would not expect 0, 2 / 1, 3. I also don’t think they’d<br class="gmail_msg">
>> expect the actual outcome from a dictionary, whether index or<br class="gmail_msg">
>> enumeration because there’s no inherent semantic “enumeration” of<br class="gmail_msg">
>> dictionary values:<br class="gmail_msg">
>><br class="gmail_msg">
>> let dict = [0:"a", 1:"b", 2:"c"]<br class="gmail_msg">
>> for (idx, value) in dict.enumerated() {<br class="gmail_msg">
>> print(idx, value)<br class="gmail_msg">
>> }<br class="gmail_msg">
>><br class="gmail_msg">
>> 0 (2, "c")<br class="gmail_msg">
>> 1 (0, "a")<br class="gmail_msg">
>> 2 (1, "b")<br class="gmail_msg">
>><br class="gmail_msg">
>> I’d like to see enumerated gone and I have a mild preference for<br class="gmail_msg">
>> introducing indexed, either under its own name or as a new behavior<br class="gmail_msg">
>> for enumerated (although T where T.Iterator.Element is Int)<br class="gmail_msg">
>><br class="gmail_msg">
>> 120 gists with “enumerated”, of which a casual scan shows that almost<br class="gmail_msg">
>> none of them are actually using it meaningfully. (Take a look.) I<br class="gmail_msg">
>> think I did this API right:<br class="gmail_msg">
>> <a href="https://api.github.com/search/repositories?q=enumerate+language:swift" rel="noreferrer" class="gmail_msg" target="_blank">https://api.github.com/search/repositories?q=enumerate+language:swift</a><br class="gmail_msg">
>> <<a href="https://api.github.com/search/repositories?q=enumerate+language:swift" rel="noreferrer" class="gmail_msg" target="_blank">https://api.github.com/search/repositories?q=enumerate+language:swift</a>><br class="gmail_msg">
>> and if so, not much in repos.<br class="gmail_msg">
><br class="gmail_msg">
> Ben's not arguing that enumerated should stay. He's just saying that<br class="gmail_msg">
> there's no good reason to provide indexed(), and I agree with that.<br class="gmail_msg">
><br class="gmail_msg">
> --<br class="gmail_msg">
> -Dave<br class="gmail_msg">
<br class="gmail_msg">
And I think I just argued my way to agreeing with him.<br class="gmail_msg">
<br class="gmail_msg">
-- E<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
_______________________________________________<br class="gmail_msg">
swift-evolution mailing list<br class="gmail_msg">
<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg">
</blockquote></div>