[swift-evolution] Removing enumerated?

Ben Cohen ben_cohen at apple.com
Fri Feb 3 15:58:59 CST 2017


> On Feb 3, 2017, at 11:12 AM, Erica Sadun via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I believe what people *want* is `indexed` over `enumerated`, and consistently for both array and array slices.
> 

I don’t know if that’s true.

Here’s an example (the only use of enumerated) from Alamofire:

let acceptLanguage = Locale.preferredLanguages.prefix(6).enumerated().map { index, languageCode in
    let quality = 1.0 - (Double(index) * 0.1)
    return "\(languageCode);q=\(quality)"
}.joined(separator: ", ")

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.

Suppose there wasn’t an enumerate or an indexed, and zipped was the standard way of doing it. That might lead to another solution:

let qualities = stride(from: 1.0, to: 0.4, by: -0.1)
let acceptLanguage = Locale.preferredLanguages.zipped(with: qualities).map {
    languageCode, quality in "\(languageCode);q=\(quality)"
}.joined(separator: ", ")

The use of stride here feels more what was intended, rather than backing into the quality via an “index” value. And avoids any risk with indexed of this getting applied incorrectly to slices.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170203/aee7f8ee/attachment.html>


More information about the swift-evolution mailing list