<div dir="ltr">On Sun, Apr 24, 2016 at 5:28 AM, Haravikk via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Is there a reason that NaN can’t just compare in a more useful way, e.g- always return true for the less than operator unless the other value is also NaN, thus ensuring it always comes first in ascending order? Or is there too much of a performance cost to make it a special case?</div></div></blockquote><div><br></div><div>Because NaN is not less than the other value. I&#39;d be very much against changing how comparison operators work with floating point values to deviate from standards. What you&#39;re describing is the IEEE 754 function minNum, which is proposed as `minimum` in the Swift floating point protocol proposal.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>That said I’m a +1 to the idea, especially as only the new operator would really need to be implemented in cases happy to use the defaults for everything else (as the new operator covers them all, so long as it’s implemented with O(1) complexity).<br></div><div><br></div><div>For naming I’d prefer the simpler .Before, .Same and .After, but that’s minor detail, as it reads as Order.Before and so-on.</div></div></blockquote><div><br></div><div>At least with respect to floating point, the IEEE 754 standard goes with the terminology &quot;below&quot; and &quot;above&quot;--which is reflected also in the names &quot;nextUp&quot; and &quot;nextDown&quot;. </div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Regarding how this affects sorting methods though, some people (myself included) like the simplicity of being able to do the following:<br></div><div><br></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>myArray.sort(&gt;)<span style="white-space:pre-wrap">        </span>// If array is of Comparable elements, just throw in the operator</font></div><div><br></div><div>While for less-than you could just pass in the new operator instead, is there an easy way to flip the operator to achieve a similar result?</div></div></blockquote><div><br></div><div>What&#39;s wrong with reverse()?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>When dealing with Comparable elements you usually only need the ascending and descending options after all, so they’re pretty common ways to sort.</div><div><div class="h5"><div><br></div><div><blockquote type="cite"><div>On 24 Apr 2016, at 02:28, Brent Royal-Gordon via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br><div>Currently, Comparable looks like this:<br><br><span style="white-space:pre-wrap">        </span>public protocol Comparable : Equatable {<br><span style="white-space:pre-wrap">        </span>  /// A [strict total order](<a href="http://en.wikipedia.org/wiki/Total_order#Strict_total_order" target="_blank">http://en.wikipedia.org/wiki/Total_order#Strict_total_order</a>)<br><span style="white-space:pre-wrap">        </span>  /// over instances of `Self`.<br><span style="white-space:pre-wrap">        </span>  @warn_unused_result<br><span style="white-space:pre-wrap">        </span>  func &lt; (lhs: Self, rhs: Self) -&gt; Bool<br><br><span style="white-space:pre-wrap">        </span>  @warn_unused_result<br><span style="white-space:pre-wrap">        </span>  func &lt;= (lhs: Self, rhs: Self) -&gt; Bool<br><br><span style="white-space:pre-wrap">        </span>  @warn_unused_result<br><span style="white-space:pre-wrap">        </span>  func &gt;= (lhs: Self, rhs: Self) -&gt; Bool<br><br><span style="white-space:pre-wrap">        </span>  @warn_unused_result<br><span style="white-space:pre-wrap">        </span>  func &gt; (lhs: Self, rhs: Self) -&gt; Bool<br><span style="white-space:pre-wrap">        </span>}<br><br>Simple and straightforward, but not actually accurate. In a strict total order, all elements are ordered, but that&#39;s not true of the current Comparable. For instance, floating-point NaNs are not ordered.<br><br>The FloatingPoint proposal (SE-0067, &lt;<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0067-floating-point-protocols.md" target="_blank">https://github.com/apple/swift-evolution/blob/master/proposals/0067-floating-point-protocols.md</a>&gt;) suggests that Comparable&#39;s requirements should be weakened so that only &quot;normal&quot; members of types need to be ordered, while &quot;exceptional&quot; members like NaNs are permitted to violate the rules. In practice, though, this ends up making algorithms give bizarre and incorrect results:<br><br><span style="white-space:pre-wrap">        </span>Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.1 clang-703.0.29). Type :help for assistance. <br><span style="white-space:pre-wrap">        </span>  1&gt; let numbers = Array(0.0.stride(to: 1.0, by: 0.2)) + [.NaN] + Array(1.0.stride(to: 0.0, by: -0.25)) <br><span style="white-space:pre-wrap">        </span>numbers: [Double] = 10 values {<br><span style="white-space:pre-wrap">        </span>  [0] = 0<br><span style="white-space:pre-wrap">        </span>  [1] = 0.20000000000000001<br><span style="white-space:pre-wrap">        </span>  [2] = 0.40000000000000002<br><span style="white-space:pre-wrap">        </span>  [3] = 0.60000000000000009<br><span style="white-space:pre-wrap">        </span>  [4] = 0.80000000000000004<br><span style="white-space:pre-wrap">        </span>  [5] = NaN<br><span style="white-space:pre-wrap">        </span>  [6] = 1<br><span style="white-space:pre-wrap">        </span>  [7] = 0.75<br><span style="white-space:pre-wrap">        </span>  [8] = 0.5<br><span style="white-space:pre-wrap">        </span>  [9] = 0.25<br><span style="white-space:pre-wrap">        </span>}<br><span style="white-space:pre-wrap">        </span>  2&gt; numbers.sort()<br><span style="white-space:pre-wrap">        </span>$R1: [Double] = 10 values {<br><span style="white-space:pre-wrap">        </span>  [0] = 0<br><span style="white-space:pre-wrap">        </span>  [1] = 0.20000000000000001<br><span style="white-space:pre-wrap">        </span>  [2] = 0.40000000000000002<br><span style="white-space:pre-wrap">        </span>  [3] = 0.60000000000000009<br><span style="white-space:pre-wrap">        </span>  [4] = 0.80000000000000004<br><span style="white-space:pre-wrap">        </span>  [5] = NaN<br><span style="white-space:pre-wrap">        </span>  [6] = 0.25<br><span style="white-space:pre-wrap">        </span>  [7] = 0.5<br><span style="white-space:pre-wrap">        </span>  [8] = 0.75<br><span style="white-space:pre-wrap">        </span>  [9] = 1<br><span style="white-space:pre-wrap">        </span>}<br><br>(Note that the behavior is actually much stranger than simply having the NaN act as a partition of the list—try sorting `Array(0.0.stride(to: 1.0, by: 0.1)) + [.NaN] + Array(0.0.stride(to: 1.0, by: 0.1))` to see what I mean. I&#39;m sure there are sensible implementation reasons why `sort()` behaves this way, but they aren&#39;t really relevant to this discussion.)<br><br>To address this, FloatingPoint introduces an ad-hoc mechanism: there is a `totalOrder` method in the protocol which actually *does* sort NaNs in a useful way. But because this is ad-hoc, it can&#39;t be extended to other, non-floating-point types. And since it&#39;s not part of Comparable, the plain `sort()` (well, `sorted()` in Swift 3) method on Sequences of Comparable elements doesn&#39;t use it. That&#39;s not great; the type system shouldn&#39;t lead us astray like this.<br><br>I think we should go in the other direction. Rather than weakening Comparable&#39;s promises, I think we should instead strengthen and clarify them.<br><br>In short, I propose we:<br><br>* Introduce a new `&lt;=&gt;` operator which implements a strict total ordering on the Comparable type. Rather than returning a `Bool`, it returns a new `Order` type which is similar to `NSComparisonResult`. This provides a semantic hint that non-ordering is not an option for `&lt;=&gt;`.<br>* Introduce a new `&lt;&gt;` operator which captures the concept of two values being unordered relative to one another. For example, `1.0 &lt;&gt; .nan` would be `true`.<br>* Define the friendly comparison operators like `&lt;` and `==` as being a partial order covering all values which are not `&lt;&gt;`.<br>* Include default implementations such that you only need to define `&lt;=&gt;`; `&lt;&gt;` is always `false` by default, and the other operators call through to `&lt;=&gt;` and `&lt;&gt;` to determine the correct values to return.<br>* Redefine functions like `sorted(_:)` and `max(_:)` to take a total ordering function returning an `Order`, not a partial ordering function returning a `Bool`. In other words, you would pass `&lt;=&gt;` instead of `&lt;`.<br><br>Here&#39;s what the new `Comparable` might look like:<br><br><span style="white-space:pre-wrap">        </span>public enum Order {<br><span style="white-space:pre-wrap">        </span>  case firstEarlier<br><span style="white-space:pre-wrap">        </span>  case bothEqual<br><span style="white-space:pre-wrap">        </span>  case firstLater<br><span style="white-space:pre-wrap">        </span>}<br><br><span style="white-space:pre-wrap">        </span>/// Instances of conforming types can be compared using relational operators.<br><span style="white-space:pre-wrap">        </span>/// <br><span style="white-space:pre-wrap">        </span>/// Comparable includes both a total order, which sorts all possible values, <br><span style="white-space:pre-wrap">        </span>/// and a partial order, which compares only &quot;normal&quot; or &quot;common&quot; values.<br><span style="white-space:pre-wrap">        </span>/// The partial order may consider some elements &quot;unordered&quot; and return `false` <br><span style="white-space:pre-wrap">        </span>/// for all operations.<br><span style="white-space:pre-wrap">        </span>/// <br><span style="white-space:pre-wrap">        </span>/// The `&lt;=&gt;` operator implements the total order; the others implement the <br><span style="white-space:pre-wrap">        </span>/// partial order. You may define only the total order, and `Comparable` will <br><span style="white-space:pre-wrap">        </span>/// provide default implementations which use it. You may also define both the <br><span style="white-space:pre-wrap">        </span>/// `&lt;=&gt;` operator and the `&lt;&gt;` &quot;unordered&quot; operator, and Comparable will <br><span style="white-space:pre-wrap">        </span>/// provide default implementations for the rest of the partial order which them. <br><span style="white-space:pre-wrap">        </span>/// You may also choose to implement the `&lt;`, `&gt;`, `&lt;=`, `&gt;=`, `==`, and <br><span style="white-space:pre-wrap">        </span>/// `!=` operators to completely customize the implementation.<br><span style="white-space:pre-wrap">        </span>public protocol Comparable : Equatable {<br><span style="white-space:pre-wrap">        </span>  /// A [total order](<a href="http://en.wikipedia.org/wiki/Total_order#Strict_total_order" target="_blank">http://en.wikipedia.org/wiki/Total_order#Strict_total_order</a>)<br><span style="white-space:pre-wrap">        </span>  /// over instances of `Self`. In a total order, no element is permitted to be <br><span style="white-space:pre-wrap">        </span>  /// unordered relative to any other.<br><span style="white-space:pre-wrap">        </span>  @warn_unused_result<br><span style="white-space:pre-wrap">        </span>  func &lt;=&gt; (lhs: Self, rhs: Self) -&gt; Order<br><span style="white-space:pre-wrap">        </span>  <br><span style="white-space:pre-wrap">        </span>  /// Returns `true` if, to partial order operators like `&lt;` and `==`, `lhs` is <br><span style="white-space:pre-wrap">        </span>  /// unordered relative to `rhs`.<br><span style="white-space:pre-wrap">        </span>  @warn_unused_result<br><span style="white-space:pre-wrap">        </span>  func &lt;&gt; (lhs: Self, rhs: Self) -&gt; Bool<br><span style="white-space:pre-wrap">        </span>  <br><span style="white-space:pre-wrap">        </span>  /// Returns `true` if `lhs` is less than `rhs`. Should be consistent with `&lt;=&gt;` except<br><span style="white-space:pre-wrap">        </span>  /// when the elements are unordered relative to each other.<br><span style="white-space:pre-wrap">        </span>  @warn_unused_result<br><span style="white-space:pre-wrap">        </span>  func &lt; (lhs: Self, rhs: Self) -&gt; Bool<br><span style="white-space:pre-wrap">        </span>  <br><span style="white-space:pre-wrap">        </span>  /// Returns `true` if `lhs` is greater than `rhs`. Should be consistent with `&lt;=&gt;` except<br><span style="white-space:pre-wrap">        </span>  /// when the elements are unordered relative to each other.<br><span style="white-space:pre-wrap">        </span>  @warn_unused_result<br><span style="white-space:pre-wrap">        </span>  func &gt; (lhs: Self, rhs: Self) -&gt; Bool<br><span style="white-space:pre-wrap">        </span>  <br><span style="white-space:pre-wrap">        </span>  /// Returns `true` if `lhs` is less than or equal to `rhs`. Should be consistent with `&lt;=&gt;` <br><span style="white-space:pre-wrap">        </span>  /// except when the elements are unordered relative to each other.<br><span style="white-space:pre-wrap">        </span>  @warn_unused_result<br><span style="white-space:pre-wrap">        </span>  func &lt;= (lhs: Self, rhs: Self) -&gt; Bool<br><span style="white-space:pre-wrap">        </span>  <br><span style="white-space:pre-wrap">        </span>  /// Returns `true` if `lhs` is greater than or equal to `rhs`. Should be consistent with `&lt;=&gt;` except<br><span style="white-space:pre-wrap">        </span>  /// when the elements are unordered relative to each other.<br><span style="white-space:pre-wrap">        </span>  @warn_unused_result<br><span style="white-space:pre-wrap">        </span>  func &gt;= (lhs: Self, rhs: Self) -&gt; Bool<br><span style="white-space:pre-wrap">        </span>}<br><br>Some APIs on Order which might be useful:<br><br><span style="white-space:pre-wrap">        </span>public extension Order {<br><span style="white-space:pre-wrap">        </span>  /// Returns the equivalent order for the two arguments reversed.<br><span style="white-space:pre-wrap">        </span>  func reversed() -&gt; Order {…}<br><span style="white-space:pre-wrap">        </span>  /// Returns `x` and `y` reordered according to `self`, with the earlier one first.<br><span style="white-space:pre-wrap">        </span>  func reorder&lt;T&gt;(_ x: T, _ y: T) -&gt; (T, T) {…}<br><span style="white-space:pre-wrap">        </span>  /// Returns `x` and `y` reordered with the earlier one first.<br><span style="white-space:pre-wrap">        </span>  static func reorder&lt;T: Comparable&gt;(_ x: T, _ y: T) -&gt; (T, T) {…}<br><span style="white-space:pre-wrap">        </span>}<br><br>Alternate designs:<br><br>* The `&lt;&gt;` operator is arguably not very obvious, or too confusable with some languages&#39; use of that operator for &quot;not equals&quot;. It could instead be a different operator, an instance method, or a class method.<br>* It might make sense to instead use `&lt;&gt;` to say &quot;is comparable&quot; and `!&lt;&gt;` to say &quot;is incomparable&quot;.<br>* It may also be better to define Comparable such that certain *values* are incomparable with any value, rather than certain *pairs* of values being incomparable. If so, we would want an `isIncomparable` property instead of a method or function. That works for `FloatingPoint`, but it might not suit other types. (For instance, with the `&lt;&gt;` operator in place, `String.Index` could be made incomparable with indices from other strings, but all `String.Index`es would still have a total order. That design wouldn&#39;t be possible with an `isIncomparable` property.)<br>* The `&lt;=&gt;` operator is common from other languages, but it might still be too jargony. One interesting design for this would be to expose the total order as a method on `Comparable` which is used as an implementation hook for an `Order.init(_:_:)` initializer.<br>* The cases of Order are highly bikesheddable. I like these names more than `ascending` and `descending` because I have an easier time understanding what they mean, but others might disagree.<br>* I&#39;m also toying with the idea that the partial order, which includes `==`, may have a looser definition of equality than the total order; this would mean that, for instance, `String`&#39;s total order could fall back to `UnicodeScalar.value` comparison to distinguish between strings which have equal graphemes. I&#39;m not sure how useful that would be in practice, though.<br><br>Any thoughts?<br><br>-- <br>Brent Royal-Gordon<br>Architechies<br><br>_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></blockquote></div><br></div></div></div><br>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div></div>