[swift-evolution] [Draft] Rename Sequence.elementsEqual

Martin R martinr448 at gmail.com
Tue Oct 17 04:23:11 CDT 2017



> On 17. Oct 2017, at 09:48, Jonathan Hull via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
>> On Oct 17, 2017, at 12:04 AM, Gwendal Roué via swift-evolution <swift-evolution at swift.org> wrote:
>> 
>>>> Modeling is, by definition, imperfect. The question is, what imperfect model is most useful _to Swift_. The idea is that conforming Set and Dictionary to Collection is on balance more useful than not doing so; that having two protocols, Sequence and Collection, is on balance more useful than having one or three, and that the set of semantic guarantees of Collection are on balance more useful than other possible sets of semantic guarantees.
>>> 
>>> That is your idea which is disputed and underlined with arguments whereas you keep repeating that Set behaves as dictated by its conformance without giving use cases why this should be useful.
>> 
>> Hello,
>> 
>> You can't *control* the ordering of a set or a dictionary, but you can still *rely* on it.
>> 
>> For example, to find a key in a dictionary that is associated a given value, you can rely on the fact that a dictionary's order is guaranteed to be stable, and that on top of that its indexes can address the dictionary itself, but also its keys and values sequences. The code below has no bug;
>> 
>> let dict = ["a": "foo", "b": "bar", "c": "needle"]
>> 
>> // Find a key associated with "needle"
>> if let index = dict.values.index(of: "needle") {
>>     let key = dict.keys[index]
>>     print(key) // prints "c"
>> }
> 
> You are using the index from one collection to index into another collection.  Isn’t that something the documentation explicitly tells us not to do?  Or is there a special (documented) guarantee that these collections will always sync indices?

The documentation of Dictionary.values states that

"When iterated over, values appear in this collection in the same order as they occur in the dictionary’s key-value pairs."

and similarly for Dictionary.keys. So that should be safe, as I understand it.

Martin

> 
> I guess you could do:
> 
> 	if let key = dict.first(where {$1 == “needle}).0 {
> 		print(key)
> 	}
> 
> …although note that which element is “first” if there are multiple keys with needle as the value may shift around on you as you mess with the dictionary (and also on separate runs even if you don’t mess with the dictionary).
> 
>> It's more difficult to find a use case for set's ordering and indexes. But since you ask, here is an example. The goal is to find any element which is not equal to another value, in any collection:
>> 
>> extension Collection where Element: Equatable {
>>     /// Returns any element which is not equal to the given element
>>     func anyElement(notEqualTo v: Element) -> Element? {
>>         if let i = index(of: v) {
>>             if let alt = index(i, offsetBy: 1, limitedBy: endIndex), alt != endIndex {
>>                 return self[alt]
>>             }
>>             if i == startIndex {
>>                 return nil
>>             }
>>             return first
>>         }
>>         return first
>>     }
>> }
>> 
>> Set([1, 2, 3]).anyElement(notEqualTo: 1) // 2 or 3
>> Set([1, 2]).anyElement(notEqualTo: 1)    // 2
>> Set([1]).anyElement(notEqualTo: 1)       // nil
>> Set([2]).anyElement(notEqualTo: 1)       // 2
>> Set([]).anyElement(notEqualTo: 1)        // nil
>> 
>> That *can* be useful, isn't it?
> 
> Yes, although this could easily be provided using iteration without indexing.
> 
> 
> Note that my current favorite solution is to simply provide an additional guarantee on set/dictionary that the iteration order will always be the same for the same contents (regardless of history), but the order would still be otherwise arbitrary.  That is a non-source-breaking change (renaming IS source breaking).  It won’t fix everything, but it will fix some of the biggest gotchas.  As a bonus, elementsEqual will 'just work'™ for sets/dictionaries. 
> 
> Thanks,
> Jon
> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list