5 Oct 17:36
Proposal #2659: Add sortOn and friends to Data.List
From: Twan van Laarhoven <twanvl <at> gmail.com>
Subject: Proposal #2659: Add sortOn and friends to Data.List
Newsgroups: gmane.comp.lang.haskell.libraries
Date: 2008-10-05 15:39:40 GMT
Subject: Proposal #2659: Add sortOn and friends to Data.List
Newsgroups: gmane.comp.lang.haskell.libraries
Date: 2008-10-05 15:39:40 GMT
Hello list,
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
-- Naming --
"On": The reason for the "on" suffix is that it relates to the "on" function
from Data.Function: "sortOn f = sort (compare `on` f)". In code, "sortOn fst" is
reasonably natural, "sortBy fst" would be better but that is already taken.
"With": Another possible choice is the "with" suffix. There is some precedence
for this choice [2]. A big disadvantage is that the "with" suffix is commonly
(Continue reading)
RSS Feed