[swift-evolution] [Review] Tuple comparison operators (was: Add a Lazy flatMap for Sequences of Optionals)

Dave Abrahams dabrahams at apple.com
Tue Dec 22 20:40:59 CST 2015


> On Dec 22, 2015, at 6:28 PM, Guillaume Lessard <glessard at tffenterprises.com> wrote:
> 
> 
>> On 22 déc. 2015, at 18:11, Dave Abrahams <dabrahams at apple.com> wrote:
>> Yes, the mathematical definition of lexicographical order (the only definition that can apply here) is the same everywhere.
> 
> Okay. I got sidetracked by “lexicography”. I like to buy dictionaries.
> 
> This converges on the problem, though: this-precedes-that is *not* the same as this-is-less-than-that.
> (Maybe we were talking about different things.)
> 
> Note the signature of sort:
> func sort(isOrderedBefore: (T, T) -> Bool) -> [T]
> 
> Not “lessThan”, but “isOrderedBefore”.
> 
> This proposal would redefine the less-than operator to mean “isOrderedBefore”.
> It’s been done with String for the sake of convenience, but it should not be generalized.
> (Again, a function isOrderedBefore<NTupleOfComparables>(t1,t2) would be useful.)

It’s very convenient for “<“ to correspond to the standard strict-weak ordering for a type where possible.  

> Note that there is a proper definition of less-than for vectors. (tuples being generalized vectors).
> Under that definition, *every* element of vector 1 must be less-than the corresponding elements of vector 2 in order to establish that vector 1 is less-than vector 2.
> Back to my example: ((0,3,4)<(0,5,0)) is false and ((0,3,4)>(0,5,0)) is false.

Tuples are not vectors.  If you want your vector type to have a different ordering semantics, just define a separate vector type.

> One may well be ordered before the other.
> 
> Cheers,
> Guillaume Lessard
> 

-Dave





More information about the swift-evolution mailing list