
#16332: Feature request: Data.Foldable.asumMap -------------------------------------+------------------------------------- Reporter: josephcsible | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: libraries/base | Version: 8.6.3 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: | -------------------------------------+------------------------------------- Description changed by josephcsible: Old description:
I'd like to request that an `asumMap` function be added to `Data.Foldable`, which would be a generalization of `concatMap` in the same way that `asum` is a generalization of `concat`. Its proposed definition:
{{{#!hs asumMap :: (Foldable t, Alternative f) => (a -> f b) -> t a -> f b asumMap f = foldr ((<|>) . f) empty }}}
This is almost equivalent to a combination of "asum" and "fmap", but it doesn't require a "Functor t" constraint like that does.
Notes on its usefulness: * Here's a Stack Overflow question that asks for a function ((a -> Maybe b) -> [a] -> Maybe b) that this is a more general form of: https://stackoverflow.com/questions/19643664/standard-haskell- function-a-maybe-b-a-maybe-b * The alternative prelude Relude includes this function (although theirs is implemented in terms of foldMap and Alt instead of foldr and <|>): https://hackage.haskell.org/package/relude-0.4.0/docs/Relude-Foldable- Fold.html#v:asumMap * This can also be thought of as a more useful version of `Data.Foldable.find`. For example, if you have a sum type such as `Either` and want to find the value in first one of it that's `Right`, you could do `asumMap rightToMaybe x`. If you were to instead do `find isRight x`, the `Either` won't be unwrapped even though you know it's `Right`, so you'll then have to do something unclean (like a partial function) to get the value out.
New description: I'd like to request that an `asumMap` function be added to `Data.Foldable`, which would be a generalization of `concatMap` in the same way that `asum` is a generalization of `concat`. Its proposed definition: {{{#!hs asumMap :: (Foldable t, Alternative f) => (a -> f b) -> t a -> f b asumMap f = foldr ((<|>) . f) empty }}} This is almost equivalent to a combination of `asum` and `fmap`, but it doesn't require a `Functor t` constraint like that does. Notes on its usefulness: * Here's a Stack Overflow question that asks for a function (`(a -> Maybe b) -> [a] -> Maybe b`) that this is a more general form of: https://stackoverflow.com/questions/19643664/standard-haskell- function-a-maybe-b-a-maybe-b * The alternative prelude Relude includes this function (although theirs is implemented in terms of `foldMap` and `Alt` instead of `foldr` and `<|>`): https://hackage.haskell.org/package/relude-0.4.0/docs/Relude- Foldable-Fold.html#v:asumMap * This can also be thought of as a more useful version of `Data.Foldable.find`. For example, if you have a sum type such as `Either` and want to find the value in first one of it that's `Right`, you could do `asumMap rightToMaybe x`. If you were to instead do `find isRight x`, the `Either` won't be unwrapped even though you know it's `Right`, so you'll then have to do something unclean (like a partial function) to get the value out. -- -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/16332#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler