Is there an established name for maybe mzero return?
It has the type:
MonadPlus m => Maybe a -> m a
and converts Nothing to failure and Just a to return a.
optparse-applicative has hoistMaybe.
monad-extras has liftMaybe.
errors has justZ.
IfElse has maybeMP
All of the above are the same.
An obvious variant would be
maybeAlt :: Alternative f => Maybe a -> f a
maybeAlt = maybe empty pure
And this is a special case of the following, similar to asum.
import Data.Monoid
import Control.Applicative
foldAlt :: (Foldable f, Alternative m) => f a -> m a
foldAlt = getAlt . foldMap (Alt . pure)
The reason you won't find this anywhere is that pure a <|> x === pure a. So it's good for this and not much else. It could be improved to
foldAltMap f = getAlt . foldMap (Alt . f)
or
foldrAltMap f = foldr (\x r -> f x <|> r) empty
but it's probably clearer just to write it out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With