> On Feb 3, 2017, at 11:12 AM, Erica Sadun via swift-evolution 
> <swift-evolution@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.



_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to