MaybeT package:universum

The parameterizable maybe monad, obtained by composing an arbitrary monad with the Maybe monad. Computations are actions that may produce a value or exit. The return function yields a computation that produces that value, while >>= sequences two subcomputations, exiting if either computation does.
Maps Maybe to Either wrapping default value into Right.
>>> maybeToLeft True (Just "aba")
Left "aba"

>>> maybeToLeft True Nothing
Right True
Maps Maybe to Either wrapping default value into Left.
>>> maybeToRight True (Just "aba")
Right "aba"

>>> maybeToRight True Nothing
Left True
Convert a MaybeT computation to ExceptT, with a default exception value.
The maybeToList function returns an empty list when given Nothing or a singleton list when given Just.

Examples

Basic usage:
>>> maybeToList (Just 7)
[7]
>>> maybeToList Nothing
[]
One can use maybeToList to avoid pattern matching when combined with a function that (safely) works on lists:
>>> import Text.Read ( readMaybe )

>>> sum $ maybeToList (readMaybe "3")
3

>>> sum $ maybeToList (readMaybe "")
0
Extracts Monoid value from Maybe returning mempty if Nothing.
>>> maybeToMonoid (Just [1,2,3] :: Maybe [Int])
[1,2,3]

>>> maybeToMonoid (Nothing :: Maybe [Int])
[]
Convert a ExceptT computation to MaybeT, discarding the value of any exception.