[swift-evolution] [Draft] Rationalizing Sequence end-operation names
brent at architechies.com
Mon Jun 27 23:15:29 CDT 2016
I apologize for not getting the threading right in this email, Anton; Mail seems to have glitched and lost your message.
> • Renaming `index(of:/where:)` to `earliestIndex(…)` and `first(where:)`
> to `earliest(where:)`
> -1, because `index` is considered state-of-art. `first` does not exist in
> all languages, but `earliest` is used nowhere.
> • Redesigning `prefix(upTo:)`, `prefix(through:)` and `suffix(from:)` as
> subscripts with "partial" ranges, like `people[..<idx]` or perhaps
> -1, because prefix and suffix are state-of-art again, plus they match the
> theme you are proposing, while subscripts do not.
I know that there are terms of art in this area, but I am explicitly ignoring them. This paragraph appears in the `drop` section, but it really applies to the entire proposal:
>> The term-of-art exception is not a suicide pact; it is meant to aid understanding by importing common terminology, not bind us to follow every decision made by any language that came before us. In this case, I think we should ignore precedent and forge our own path.
Almost every change in this proposal violates the term-of-art rule. I'm proposing them anyway because, in my opinion, we have too closely followed many weak terms of art in an area which would benefit more from a design with consistent, simple rules.
I will address the prefix/suffix thing in a joint reply to you and several others, but your complaints about `index` are, I believe, unique.
`index` is not a strong term of art; in my survey of a few languages that , languages seem to use "find", "index", or both, but in a number of different ways. In alphabetical order by function/method name:
• Haskell: `elemIndex`
• C++: `find`
• Python: `find` and `index` (the former returns a sentinel value on error; the latter throws)
• Ruby: `find_index`
• Perl: `first_index` (in List::MoreUtils)
• Objective-C: `indexOf:`
• Java: `indexOf`
• Scala: `indexOf`
• C#: `IndexOf`
Moreover, <https://github.com/apple/swift-evolution/pull/329> presents a compelling reason to add a direction to these methods: so we can provide a matching set of index-finding methods from the end of the collection. Languages in this list which offer one usually end up creating two asymmetrically-named methods, like `indexOf` and `lastIndexOf`. I don't think that's a good solution.
You are quite right that `earliest` is a novel invention, unprecedented in other languages as far as I know. I think there's value in distinguishing between the "first" operations (which operate on the sequence's very first element without doing any searching) and the "earliest" operations (which perform a usually linear search from the beginning of the collection to the end). I also think that, if you look at the tables in "Adding sequence and collection operations", some of the potential methods in that table might be confusing if the "earliest" operations instead use "first". For instance, `removeFirst()` would remove the very first element, while `removeFirst(_:)` would search for a matching element and remove it.
Nevertheless, it *is* possible to use `first` instead of `earliest`, and in fact my decision to post the version using `earliest` was a last-minute one. I think `earliest` is a cleaner solution, but `first` is a reasonable alternative.
More information about the swift-evolution