[GHC] #16332: Feature request: Data.Foldable.asumMap
GHC
ghc-devs at haskell.org
Sun Feb 17 02:52:17 UTC 2019
#16332: Feature request: Data.Foldable.asumMap
-------------------------------------+-------------------------------------
Reporter: josephcsible | Owner: (none)
Type: feature | Status: new
request |
Priority: normal | Milestone:
Component: | Version: 8.6.3
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:
-------------------------------------+-------------------------------------
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>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the ghc-tickets
mailing list