From d9ceb2fb51b037a330a6cfaf129c24ea7f1ac644 Mon Sep 17 00:00:00 2001 From: iori tsu <matsuhara.iori@scrive.com> Date: Sun, 25 Apr 2021 08:58:15 +0200 Subject: [PATCH] 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 --- libraries/base/GHC/Exts.hs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libraries/base/GHC/Exts.hs b/libraries/base/GHC/Exts.hs index 0e3cd144078a..d0a32b03d16d 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)) -- GitLab