Maybe a -> ma and converts No...">

Is there a name for "maybe mzero return"?

Is a name set for maybe mzero return ?

It has type:

 MonadPlus m => Maybe a -> ma 

and converts Nothing to failure and Just a to return a .

+5
source share
1 answer

All of the above is one and the same.

The obvious option would be

 maybeAlt :: Alternative f => Maybe a -> fa maybeAlt = maybe empty pure 

And this is a special case of the next, similar to asum .

 import Data.Monoid import Control.Applicative foldAlt :: (Foldable f, Alternative m) => fa -> ma foldAlt = getAlt . foldMap (Alt . pure) 

The reason you won't find anywhere is pure a <|> x === pure a . So itโ€™s good for this and not much more. It could be improved to

 foldAltMap f = getAlt . foldMap (Alt . f) 

or

 foldrAltMap f = foldr (\xr -> fx <|> r) empty 

but itโ€™s probably easier to understand.

+8
source

All Articles