Commit 44512e3c authored by bernalex's avatar bernalex Committed by Herbert Valerio Riedel

Add Data.List.sortOn function (re #9004 and #2659)

`sortOn` sorts a list by comparing the results of a key function applied to each
element.  `sortOn f` is equivalent to `sortBy . comparing f`, but has the
performance advantage of only evaluating `f` once for each element in the
input list.

Historical note: This was already proposed in 2008 as part of

  http://www.haskell.org/pipermail/libraries/2008-October/010797.html

It was, however, the recent re-attempt

  http://www.haskell.org/pipermail/libraries/2014-April/022489.html

that let `sortOn` make it into base at last. Maybe the other functions
mentioned in #2659 might be worth reconsidering as well.
parent 1bf6c0e4
......@@ -164,6 +164,7 @@ module Data.List
-- ** Ordered lists
, sort
, sortOn
, insert
-- * Generalized functions
......@@ -207,6 +208,8 @@ module Data.List
import Data.Maybe
import Data.Char ( isSpace )
import Data.Ord ( comparing )
import Data.Tuple ( fst, snd )
import GHC.Num
import GHC.Real
......@@ -957,6 +960,17 @@ rqpart cmp x (y:ys) rle rgt r =
#endif /* USE_REPORT_PRELUDE */
-- | Sort a list by comparing the results of a key function applied to each
-- element. @sortOn f@ is equivalent to @sortBy . comparing f@, but has the
-- performance advantage of only evaluating @f@ once for each element in the
-- input list. This is called the decorate-sort-undecorate paradigm, or
-- Schwartzian transform.
--
-- /Since: 4.7.1.0/
sortOn :: Ord b => (a -> b) -> [a] -> [a]
sortOn f =
map snd . sortBy (comparing fst) . map (\x -> let y = f x in y `seq` (y, x))
-- | The 'unfoldr' function is a \`dual\' to 'foldr': while 'foldr'
-- reduces a list to a summary value, 'unfoldr' builds a list from
-- a seed value. The function takes the element and returns 'Nothing'
......
......@@ -6,6 +6,8 @@
* Add reverse application operator `Data.Function.(&)`
* Add `Data.List.sortOn` sorting function
## 4.7.0.0 *Apr 2014*
* Bundled with GHC 7.8.1
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment