Haskell also has a `comparing` function

    comparing :: (Ord a) => (b -> a) -> b -> b -> Ordering

which applies a function on both the left hand side and the right hand side to 
get two values that can be compared/ordered. 
This makes the call site look something like this:

    sortBy (comparing length) names

The same can be done in Swift, resulting in a similar and very English-like 
calls site:

    names.sort(by: comparing { $0.characters.count })

That said. While this (the `comparing` function) is a fun exercise in higher 
order functions, a more Swifty syntax for this is probably a separate overload 
of the sort/ed/ function.

- David

> On 17 Aug 2016, at 02:17, Silvan Mosberger via swift-evolution 
> <[email protected]> wrote:
> 
>> I'd be in favor. We'd need another name, since the current closure predicate 
>> is already standardized to `by:`.
> 
> Haskell uses "on" for sorting with a mapping. There are both
> 
> sortOn <> :: Ord 
> <http://hackage.haskell.org/package/base-4.9.0.0/docs/Data-Ord.html#t:Ord> b 
> => (a -> b) -> [a] -> [a]
> 
> and
> 
> sortBy <> :: (a -> a -> Ordering 
> <http://hackage.haskell.org/package/base-4.9.0.0/docs/Data-Ord.html#t:Ordering>)
>  -> [a] -> [a]
> 
> in Haskell.
> _______________________________________________
> swift-evolution mailing list
> [email protected]
> https://lists.swift.org/mailman/listinfo/swift-evolution

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

Reply via email to