> On 2016-11-03, at 19:39, Chris Eidhof via swift-evolution 
> <[email protected]> wrote:
> I was wondering if there's any interest in adding Swift-like sort descriptors 
> to the language. Currently, we have `sort` and `sorted`, which take a 
> function of type `(A, A) -> Bool`. Foundation has `NSSortDescriptor`, which 
> corresponds more to `(A, A) -> ComparisonResult`. NSSortDescriptor is a lot 
> easier to work with than Swift's stdlib way: it's more declarative, it's easy 
> to combine multiple sort descriptors into one (e.g. sort by last name, then 
> sort by first name, and so on). However, NSSortDescriptor uses runtime 
> programming, and is not very typesafe.
> 
> We could do a lot better in Swift. There are at least a few possibilities:
> 
> typealias SortDescriptor<A> = (A, A) -> Bool
> typealias SortDescriptor<A> = (A, A) -> ComparisonResult
> struct SortDescriptor<A> { let compare: (A, A) -> Bool }
> struct SortDescriptor<A> { let compare: (A, A) -> ComparisonResult }
> 
> I've experimented a bit with this, and it seems like the `struct` based 
> version with `ComparisonResult` is quite nice, because then we can add 
> methods/properties on it (e.g. to reverse a sort descriptor, or to return a 
> `Bool` instead of a `ComparisonResult`). 
> 
> My "SortDescriptor" could also be called "Comparator", and in fact, there is 
> already something like that in Foundation (of type (Any,Any) -> Bool).
> 
> Is there interest in adding support for this to the stdlib? If yes, is anyone 
> interested in writing a good proposal together?

There is definitely interest!

In fact, Robert Widmann, Jaden Geller, Harlan Haskins and Pyry Jahkola had 
previously worked on a draft proposal to clean up Comparable's semantics, and 
to move from a < + == model to a single comparator operator called "<=>”. Sadly 
this happened just before Swift 3 deadline, and the proposal was lost in the 
noise.

https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160725/025466.html
 
<https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160725/025466.html>
https://github.com/pyrtsa/swift-evolution/blob/ca89e7b3a1dffc99baa695a03544fcba75afd0f3/proposals/NNNN-formalized-ordering.md
 
<https://github.com/pyrtsa/swift-evolution/blob/ca89e7b3a1dffc99baa695a03544fcba75afd0f3/proposals/NNNN-formalized-ordering.md>

The new sort functions are included under Future Directions above, but it would 
also be possible to add them before resolving the general case. 

If this is a good time to reopen this discussion, then I’m all for it! 

-- 
Karoly

_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to