summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriori tsu <matsuhara.iori@scrive.com>2021-04-25 08:58:15 +0200
committerMarge Bot <ben+marge-bot@smart-cactus.org>2021-04-27 00:00:08 -0400
commitd9ceb2fb51b037a330a6cfaf129c24ea7f1ac644 (patch)
tree6b79ed23abfb1694172aaeab6e3793ac8a242633
parentc1549069c15117b06c521bd3de966754f3fc62ec (diff)
downloadhaskell-d9ceb2fb51b037a330a6cfaf129c24ea7f1ac644.tar.gz
Add documentation for GHC.Exts.sortWith
sortWith has the same type definition as `Data.List.sortOn` (eg: `Ord b => (a -> b) -> [a] -> [a]`). Nonetheless, they behave differently, sortOn being more efficient. This merge request add documentation to reflect on this differences
-rwxr-xr-xlibraries/base/GHC/Exts.hs5
1 files changed, 5 insertions, 0 deletions
diff --git a/libraries/base/GHC/Exts.hs b/libraries/base/GHC/Exts.hs
index 0e3cd14407..d0a32b03d1 100755
--- a/libraries/base/GHC/Exts.hs
+++ b/libraries/base/GHC/Exts.hs
@@ -149,6 +149,11 @@ the [] = errorWithoutStackTrace "GHC.Exts.the: empty list"
-- | The 'sortWith' function sorts a list of elements using the
-- user supplied function to project something out of each element
+--
+-- In general if the user supplied function is expensive to compute then
+-- you should probably be using 'Data.List.sortOn', as it only needs
+-- to compute it once for each element. 'sortWith', on the other hand
+-- must compute the mapping function for every comparison that it performs.
sortWith :: Ord b => (a -> b) -> [a] -> [a]
sortWith f = sortBy (\x y -> compare (f x) (f y))