[GHC] #12044: Remove sortWith in favor of sortOn
GHC
ghc-devs at haskell.org
Fri Oct 14 16:57:45 UTC 2016
#12044: Remove sortWith in favor of sortOn
-------------------------------------+-------------------------------------
Reporter: cblp | Owner: cblp
Type: feature request | Status: patch
Priority: normal | Milestone:
Component: libraries/base | Version: 7.10.3
Resolution: | Keywords: newcomer
Operating System: Unknown/Multiple | Architecture:
| Unknown/Multiple
Type of failure: Other | Test Case:
Blocked By: | Blocking:
Related Tickets: #2659 | Differential Rev(s): Phab:D2587
Wiki Page: |
-------------------------------------+-------------------------------------
Changes (by bgamari):
* owner: vkonton => cblp
Comment:
It's really not clear to me that this is actually a refactoring that we
would want to undertake. While `sortOn` and `sortWith` are semantically
equivalent, they are operationally much different. Namely, for "cheap"
mapping functions (e.g. projections like `fst`) `sortOn` will allocate
significantly more than `sortWith`. Consider the case of,
{{{#!hs
x :: [(Int, String)]
x = ...
by = sortBy (comparing fst) x
on = sortOn fst
}}}
The evaluation of `on` will allocate a list of type `[(Int, (Int,
String))]`, `sortBy (comparing fst)` that intermediate list, and then
project back to a `[(Int, String)]`. It seems to me like this would be
quite undesirable.
While we may not want to deprecate `GHC.Exts.sortWith` in favor of
`sortOn` in particular, perhaps we want to deprecate it nevertheless. The
more standard form `sortBy (comparing f)` is only a bit longer and
arguably you should be forced to consider whether or not you want to use
`sortOn` instead.
--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/12044#comment:5>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the ghc-tickets
mailing list