Feature request: Data.Foldable.asumMap
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:
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
foldMapandAltinstead offoldrand<|>): https://hackage.haskell.org/package/relude-0.4.0/docs/Relude-Foldable-Fold.html#v:asumMap - The Util library includes
Util.altMap, which is just another name for this: https://hackage.haskell.org/package/util-0.1.14.0/docs/Util.html#v:altMap - This can also be thought of as a more useful version of
Data.Foldable.find. For example, if you have a sum type such asEitherand want to find the value in first one of it that'sRight, you could doasumMap rightToMaybe x. If you were to instead dofind isRight x, theEitherwon't be unwrapped even though you know it'sRight, so you'll then have to do something unclean (like a partial function) to get the value out.
Edited by Joseph C. Sible