Was the idea of an "Ordered" protocol already discussed or even dismissed? If so, I think I missed it...
However, recently I thought about a purely theoretical (just noticed that it isn't that theoretical: Pitch: ContainmentSet) data structure (I don't know if everyone would even call it a data structure) inspired by mathematical sets that are sometimes expressed by a condition: M = { x element IR | x < 0 } (So M contains all real numbers smaller then 0). Now the data structure does the same:
public struct ConditionMathSet<Element> {
/// The condition whether an Element is contained in this set or not.
private var condition: (Element) -> Bool
/// The only relevant function of this structure
public func contains(_ element: Element) -> Bool {
return condition(element)
}
}
This is once an example of a data structure that is neither a Collection, nor a Sequence and you can truly not iterate it (without going to the address level and checking and interpreting the whole storage, as far as I can see). So it is practically not iteratable because it just don't know the elements in it.
However, if Element is Comparable, the structure is ordered and you can perform sense-full operations on it. The ones I can see are:
- get all Elements in it smaller than a certain element
func allSmaller(then element: Element) -> ConditionMathSet - the same with greater then and greater-equal then, etc, generalised
func all(_ operation: (Element, Element) -> Bool, then element: Element) -> ConditionMathSet - still more generalised
fun all(toWhichApplies additionalCondition: (Element) -> Bool)- This includes certain "ranges", so elements smaller then one element and also bigger then another one.
Maybe (hopefully) there are more. I am wondering whether introducing an "Ordered" protocol might be a good addition to swift, if ordered types, like sorted sets or priority lists are added as well. This protocol would have a Key type alias that would need to be Comparable and would determine the ordering. So in an ordered set, the elements are also the keys, but in a priority liked list, the priority is the key. In my opinion arrays are also ordered with the indices being the keys, but in this case it overlaps with the iteratability and the methods from Sequence.
But maybe this has been discussed before and I just don't know about it.
This is obviously no real solution to the problem discussed in this thread, but I could imagine that array conforming to Ordered with these methods and Set not having them might highlight the difference between ordering and iterability and therefor clarify a bit what Sequences methods are about and about what they are not (persistent and defined order).