maybe

The maybe function takes a default value, a function, and a Maybe value. If the Maybe value is Nothing, the function returns the default value. Otherwise, it applies the function to the value inside the Just and returns the result.

Examples

Basic usage:
>>> maybe False odd (Just 3)
True
>>> maybe False odd Nothing
False
Read an integer from a string using readMaybe. If we succeed, return twice the integer; that is, apply (*2) to it. If instead we fail to parse an integer, return 0 by default:
>>> import GHC.Internal.Text.Read ( readMaybe )

>>> maybe 0 (*2) (readMaybe "5")
10

>>> maybe 0 (*2) (readMaybe "")
0
Apply show to a Maybe Int. If we have Just n, we want to show the underlying Int n. But if we have Nothing, we return the empty string instead of (for example) "Nothing":
>>> maybe "" show (Just 5)
"5"

>>> maybe "" show Nothing
""
Generates a Nothing some of the time.
The maybe function takes a default value, a function, and a Maybe value. If the Maybe value is Nothing, the function returns the default value. Otherwise, it applies the function to the value inside the Just and returns the result.

Examples

Basic usage:
>>> maybe False odd (Just 3)
True
>>> maybe False odd Nothing
False
Read an integer from a string using readMaybe. If we succeed, return twice the integer; that is, apply (*2) to it. If instead we fail to parse an integer, return 0 by default:
>>> import Text.Read ( readMaybe )

>>> maybe 0 (*2) (readMaybe "5")
10

>>> maybe 0 (*2) (readMaybe "")
0
Apply show to a Maybe Int. If we have Just n, we want to show the underlying Int n. But if we have Nothing, we return the empty string instead of (for example) "Nothing":
>>> maybe "" show (Just 5)
"5"

>>> maybe "" show Nothing
""
Given a default value, a function and a Maybe value, yields the default value if the Maybe value is Nothing and applies the function to the value stored in the Just otherwise.
Decode a Maybe.
>>> input (maybe natural) "Some 1"
Just 1
The maybe function takes a default value, a function, and a Maybe value. If the Maybe value is Nothing, the function returns the default value. Otherwise, it applies the function to the value inside the Just and returns the result.

Examples

Basic usage:
>>> maybe False odd (Just 3)
True
>>> maybe False odd Nothing
False
Read an integer from a string using readMaybe. If we succeed, return twice the integer; that is, apply (*2) to it. If instead we fail to parse an integer, return 0 by default:
>>> import Text.Read ( readMaybe )

>>> maybe 0 (*2) (readMaybe "5")
10

>>> maybe 0 (*2) (readMaybe "")
0
Apply show to a Maybe Int. If we have Just n, we want to show the underlying Int n. But if we have Nothing, we return the empty string instead of (for example) "Nothing":
>>> maybe "" show (Just 5)
"5"

>>> maybe "" show Nothing
""
Consume a single input and transform it using the supplied Maybe returning function. Pre-release
Map a Maybe returning function on the next element in the stream. The parser fails if the function returns Nothing otherwise returns the Just value.
>>> toEither = Maybe.maybe (Left "maybe: predicate failed") Right

>>> maybe f = Parser.either (toEither . f)
>>> maybe f = Parser.fromFoldMaybe "maybe: predicate failed" (Fold.maybe f)
Pre-release
The maybe function takes a default value, a function, and a Maybe value. If the Maybe value is Nothing, the function returns the default value. Otherwise, it applies the function to the value inside the Just and returns the result.

Examples

Basic usage:
>>> maybe False odd (Just 3)
True
>>> maybe False odd Nothing
False
Read an integer from a string using readMaybe. If we succeed, return twice the integer; that is, apply (*2) to it. If instead we fail to parse an integer, return 0 by default:
>>> import Text.Read ( readMaybe )

>>> maybe 0 (*2) (readMaybe "5")
10

>>> maybe 0 (*2) (readMaybe "")
0
Apply show to a Maybe Int. If we have Just n, we want to show the underlying Int n. But if we have Nothing, we return the empty string instead of (for example) "Nothing":
>>> maybe "" show (Just 5)
"5"

>>> maybe "" show Nothing
""
Case analysis for symbolic Maybes. If the value isNothing, return the default value; if it isJust, apply the function.
>>> maybe 0 (`sMod` 2) (sJust (3 :: SInteger))
1 :: SInteger

>>> maybe 0 (`sMod` 2) (sNothing :: SMaybe Integer)
0 :: SInteger

>>> let f = uninterpret "f" :: SInteger -> SBool

>>> prove $ \x d -> maybe d f (sJust x) .== f x
Q.E.D.

>>> prove $ \d -> maybe d f sNothing .== d
Q.E.D.
This function is intended for checking and parsing paths provided as user input.
fmap Posix.toString (Posix.maybePath "/" :: Maybe Posix.AbsDir) == Just "/"
fmap Posix.toString (Posix.maybePath "/" :: Maybe Posix.AbsFile) == Nothing
fmap Posix.toString (Posix.maybePath "/" :: Maybe Posix.RelDir) == Nothing
fmap Posix.toString (Posix.maybePath "/" :: Maybe Posix.RelFile) == Nothing
fmap Posix.toString (Posix.maybePath "/tmp" :: Maybe Posix.AbsDir) == Just "/tmp"
fmap Posix.toString (Posix.maybePath "/tmp" :: Maybe Posix.AbsFile) == Just "/tmp"
fmap Posix.toString (Posix.maybePath "/tmp" :: Maybe Posix.RelDir) == Nothing
fmap Posix.toString (Posix.maybePath "/tmp" :: Maybe Posix.RelFile) == Nothing
fmap Posix.toString (Posix.maybePath "/tmp/" :: Maybe Posix.AbsDir) == Just "/tmp"
fmap Posix.toString (Posix.maybePath "/tmp/" :: Maybe Posix.AbsFile) == Nothing
fmap Posix.toString (Posix.maybePath "/tmp/" :: Maybe Posix.RelDir) == Nothing
fmap Posix.toString (Posix.maybePath "/tmp/" :: Maybe Posix.RelFile) == Nothing
fmap Posix.toString (Posix.maybePath "/tmp" :: Maybe Posix.AbsRelFileDir) == Just "/tmp"
fmap Posix.toString (Posix.maybePath "/tmp/" :: Maybe Posix.AbsRelFileDir) == Just "/tmp"
fmap Posix.toString (Posix.maybePath "file.txt" :: Maybe Posix.RelFile) == Just "file.txt"
fmap Posix.toString (Posix.maybePath "file.txt" :: Maybe Posix.AbsFile) == Nothing
fmap Windows.toString (Windows.maybePath "\\tmp" :: Maybe Windows.AbsDir) == Just "\\tmp"
fmap Windows.toString (Windows.maybePath "a:\\tmp" :: Maybe Windows.AbsDir) == Just "a:\\tmp"
fmap Windows.toString (Windows.maybePath "a:tmp" :: Maybe Windows.AbsDir) == Just "a:tmp"
fmap Windows.toString (Windows.maybePath "a:\\" :: Maybe Windows.AbsDir) == Just "a:\\"
fmap Windows.toString (Windows.maybePath "a:" :: Maybe Windows.AbsDir) == Just "a:"
fmap Windows.toString (Windows.maybePath "tmp" :: Maybe Windows.RelDir) == Just "tmp"
fmap Windows.toString (Windows.maybePath "\\tmp" :: Maybe Windows.RelDir) == Nothing
fmap Windows.toString (Windows.maybePath "a:\\tmp" :: Maybe Windows.RelDir) == Nothing
fmap Windows.toString (Windows.maybePath "a:tmp" :: Maybe Windows.RelDir) == Nothing
fmap Windows.toString (Windows.maybePath "tmp" :: Maybe Windows.AbsDir) == Nothing
Try a decoder and get back a 'Just a' if it succeeds and Nothing if it fails. In other words, this decoder always succeeds with a 'Maybe a' value.
>>> decode (maybe string) "42"
Just Nothing

>>> decode (maybe int) "42"
Just (Just 42)
The Maybe type encapsulates an optional value. A value of type Maybe a either contains a value of type a (represented as Just a), or it is empty (represented as Nothing). Using Maybe is a good way to deal with errors or exceptional cases without resorting to drastic measures such as error. The Maybe type is also a monad. It is a simple kind of error monad, where all errors are represented by Nothing. A richer error monad can be built using the Either type.
The Maybe type, and associated operations.
The Maybe type.
The MaybeT monad transformer extends a monad with the ability to exit the computation without returning a value. A sequence of actions produces a value only if all the actions in the sequence do. If one exits, the rest of the sequence is skipped and the composite action exits. For a variant allowing a range of exception values, see Control.Monad.Trans.Except.
The Maybe type encapsulates an optional value. A value of type Maybe a either contains a value of type a (represented as Just a), or it is empty (represented as Nothing). Using Maybe is a good way to deal with errors or exceptional cases without resorting to drastic measures such as error. The Maybe type is also a monad. It is a simple kind of error monad, where all errors are represented by Nothing. A richer error monad can be built using the Either type.