Proposal #2659: Add sortOn and friends to Data.List
Henning Thielemann
lemming at henning-thielemann.de
Sun Oct 5 12:00:33 EDT 2008
On Sun, 5 Oct 2008, Twan van Laarhoven wrote:
> Almost all uses of sortBy in user code use 'comparing', 'on' or a similar
> construction [1]. I think we should add a function that makes this common
> behavior more convenient:
>
> sortOn :: Ord b => (a -> b) -> [a] -> [a]
>
> For consistency we should also add *On for the other *By functions in
> Data.List:
>
> nubOn :: Eq b => (a -> b) -> [a] -> [a]
> deleteOn :: Eq b => (a -> b) -> a -> [a] -> [a]
> deleteFirstsOn :: Eq b => (a -> b) -> [a] -> [a] -> [a]
> unionOn :: Eq b => (a -> b) -> [a] -> [a] -> [a]
> intersectOn :: Eq b => (a -> b) -> [a] -> [a] -> [a]
> groupOn :: Eq b => (a -> b) -> [a] -> [[a]]
> sortOn :: Ord b => (a -> b) -> [a] -> [a]
> insertOn :: Ord b => (a -> b) -> a -> [a] -> [a]
> maximumOn :: Ord b => (a -> b) -> [a] -> a
> minimumOn :: Ord b => (a -> b) -> [a] -> a
> (nubSortOn :: Ord b => (a -> b) -> [a] -> [a]) -- see #2629
I also prefer these functions and have called them '*Key', because they
work on a key:
http://darcs.haskell.org/htam/src/Useful.hs
> Questions:
> 1. should sortOn be added to Data.List?
yes
> 2. should all other *On functions be added as well?
yes
> 3. what name should these functions get?
'On' is ok for me
> 4. should the sortOn' variations be added? What about the naming?
Both variants differ only in efficiency, where none is superior over the
other. I don't think that the prime is a good way to indicate the
difference. Maybe you can use 'sortKey' for the variant for selector
functions and 'sortOn' for the caching variant.
> 5. should Down be added to Data.Ord?
I find it useful.
More information about the Libraries
mailing list