[GHC] #15566: Implement minimumOn, maximumOn to mirror sortOn
GHC
ghc-devs at haskell.org
Sat Aug 25 16:27:37 UTC 2018
#15566: Implement minimumOn, maximumOn to mirror sortOn
-------------------------------------+-------------------------------------
Reporter: Garciat | Owner: (none)
Type: feature request | Status: new
Priority: normal | Milestone: Research
| needed
Component: libraries/base | Version: 8.5
Resolution: | Keywords:
Operating System: Unknown/Multiple | Architecture:
| Unknown/Multiple
Type of failure: None/Unknown | Test Case:
Blocked By: | Blocking:
Related Tickets: | Differential Rev(s):
Wiki Page: |
-------------------------------------+-------------------------------------
Comment (by Garciat):
This is just food for thought:
{{{#!hs
import Data.Foldable
import Data.Semigroup
newtype Tagged t a = Tagged (t, a)
instance Eq t => Eq (Tagged t a) where
Tagged (x, _) == Tagged (y, _) = x == y
instance Ord t => Ord (Tagged t a) where
compare (Tagged (x, _)) (Tagged (y, _)) = compare x y
maximumOn :: (Foldable t, Ord b) => (a -> b) -> t a -> a
maximumOn f xs =
case foldMap tag xs of
Nothing -> error "maximumOn: empty structure"
Just (Max (Tagged (_, x))) -> x
where
tag x = Just . Max . Tagged $ (f x, x)
minimumOn :: (Foldable t, Ord b) => (a -> b) -> t a -> a
minimumOn f xs =
case foldMap tag xs of
Nothing -> error "minimumOn: empty structure"
Just (Min (Tagged (_, x))) -> x
where
tag x = Just . Min . Tagged $ (f x, x)
}}}
I can anticipate the partiality of the functions being a potential problem
for the general case. I guess that suggests these should be `Foldable`
methods w/ defaults, instead of standalone functions? And that makes the
proposal that much hairy due to evolution, etc.
--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/15566#comment:2>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the ghc-tickets
mailing list