#2659: Add sortOn and other *On functions
------------------------------+---------------------------------------------
Reporter: twanvl | Owner:
Type: proposal | Status: new
Priority: normal | Milestone: Not GHC
Component: libraries/base | Version: 6.9
Severity: normal | Resolution:
Keywords: | Difficulty: Unknown
Testcase: | Architecture: Unknown/Multiple
Os: Unknown/Multiple |
------------------------------+---------------------------------------------
Comment (by lpsmith):
I think the {{{sortOn}}} suggestion is a good one. Honestly, the need to
import to import two libraries *and then* use the {{{sortBy (comparing
someFunctionName)}}} idiom is rather cumbersome for something so useful,
absurdly so if you use qualified names. (I tend to use qualified names.)
"sortOn" is a good name as well, much better than my suggested "sortFun".
(ticket:2406)
However, I'm a bit ambivalent towards the other suggestions. Take
{{{maximumOn}}}. What would elements would the following examples
return?
{{{
maximumOn abs [-30, 4, 8, 30]
maximumOn abs [ 30, 4, 8, -30]
}}}
I suppose this function is supposed to be equivalent to {{{maximumBy
(comparing abs)}}}, but I wouldn't know what this does off the top of my
head. I would guess that it returns the "first" maximum, -30 and 30
respectively in the example. But the documentation is incomplete: I'd
have to look at the sources to be absolutely sure of what this function
does.
The thing is, {{{maximumBy}}} returns the maximum element in some
quotient type; essentially re-defining ordering (and possibly equality)
in the result, something I don't find to be particularly useful.
However, {{{sortBy}}} still has nice properties without referring to the
quotient type: I'm guaranteed some kind of permutation that interests me,
assuming that the comparison function is indeed a total linear order.
However, I do find the functions {{{maximalsOn}}} and {{{maximalsBy}}} to
be rather useful, which evaluate as follows:
{{{
maximalsOn abs [-30, 4, 8, 30] == [-30, 30]
maximalsOn abs [30, 4, 8, -30] == [30, -30]
maximalsOn abs [30, 4, 30, 8, -30] == [30, 30, -30]
maximalsOn abs [] == []
}}}
This has the advantage of being defined on empty lists too.
--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/2659#comment:2>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs