[GHC] #15566: Implement minimumOn, maximumOn to mirror sortOn

#15566: Implement minimumOn, maximumOn to mirror sortOn -------------------------------------+------------------------------------- Reporter: Garciat | Owner: (none) Type: feature | Status: new request | Priority: normal | Milestone: Research needed Component: | Version: 8.5 libraries/base | Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: None/Unknown Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- By importing Data.List, we currently get: {{{#!hs sortBy :: (a -> a -> Ordering) -> [a] -> [a] maximumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a minimumBy :: Foldable t => (a -> a -> Ordering) -> t a -> a sortOn :: Ord b => (a -> b) -> [a] -> [a] }}} I believe sortOn to be a very useful 'shortcut'. In that spirit, I propose we add: {{{#!hs maximumOn :: (Foldable t, Ord b) => (a -> b) -> t a -> a minimumOn :: (Foldable t, Ord b) => (a -> b) -> t a -> a }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15566 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#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: | -------------------------------------+------------------------------------- Changes (by Iceland_jack): * cc: Iceland_jack (added) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15566#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#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

#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): Well, `maximumBy` and `minimumBy` are already partial by their use of `Foldable.foldl1`, so I'm thinking it's probably alright to do the more obvious: {{{#!hs import Data.Foldable import Data.Function (on) maximumOn :: (Foldable t, Ord b) => (a -> b) -> t a -> a maximumOn f = maximumBy (compare `on` f) minimumOn :: (Foldable t, Ord b) => (a -> b) -> t a -> a minimumOn f = minimumBy (compare `on` f) }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15566#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#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): D5110 Wiki Page: | -------------------------------------+------------------------------------- Changes (by Garciat): * differential: => D5110 Comment: Went ahead and drafted a Phab diff. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15566#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#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): D5110 Wiki Page: | -------------------------------------+------------------------------------- Comment (by Iceland_jack): Will we want the total variants, with `Foldable1` constraints (#13573) {{{#!hs maximumOn, minimumOn :: Foldable1 f1 => Ord cmp => (a -> cmp) -> (f1 a -> a) maximumOn = maximumBy . comparing minimumOn = minimumBy . comparing }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15566#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#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: #13573 | Differential Rev(s): D5110 Wiki Page: | -------------------------------------+------------------------------------- Changes (by Iceland_jack): * related: => #13573 Comment: Will we want the total variants, with `Foldable1` constraints (#13573) {{{#!hs maximumOn, minimumOn :: Foldable1 f1 => Ord cmp => (a -> cmp) -> (f1 a -> a) maximumOn = maximumBy . comparing minimumOn = minimumBy . comparing }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15566#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#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: #13573 | Differential Rev(s): Phab:D5110 Wiki Page: | -------------------------------------+------------------------------------- Changes (by potato44): * differential: D5110 => Phab:D5110 -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15566#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC