Data.Maybe
- Package
- purescript-maybe
- Repository
- purescript/purescript-maybe
#Maybe Source
data Maybe a
The Maybe
type is used to represent optional values and can be seen as
something like a type-safe null
, where Nothing
is null
and Just x
is the non-null value x
.
Constructors
Instances
Functor Maybe
Apply Maybe
The
Apply
instance allows functions contained within aJust
to transform a value contained within aJust
using theapply
operator:Just f <*> Just x == Just (f x)
Nothing
values are left untouched:Just f <*> Nothing == Nothing Nothing <*> Just x == Nothing
Combining
Functor
's<$>
withApply
's<*>
can be used transform a pure function to takeMaybe
-typed arguments sof :: a -> b -> c
becomesf :: Maybe a -> Maybe b -> Maybe c
:f <$> Just x <*> Just y == Just (f x y)
The
Nothing
-preserving behaviour of both operators means the result of an expression like the above but where any one of the values isNothing
means the whole result becomesNothing
also:f <$> Nothing <*> Just y == Nothing f <$> Just x <*> Nothing == Nothing f <$> Nothing <*> Nothing == Nothing
Applicative Maybe
The
Applicative
instance enables lifting of values intoMaybe
with thepure
orreturn
function (return
is an alias forpure
):pure x :: Maybe _ == Just x return x :: Maybe _ == Just x
Combining
Functor
's<$>
withApply
's<*>
andApplicative
'spure
can be used to pass a mixture ofMaybe
and non-Maybe
typed values to a function that does not usually expect them, by usingpure
for any value that is not alreadyMaybe
typed:f <$> Just x <*> pure y == Just (f x y)
Even though
pure = Just
it is recommended to usepure
in situations like this as it allows the choice ofApplicative
to be changed later without having to go through and replaceJust
with a new constructor.Alt Maybe
The
Alt
instance allows for a choice to be made between twoMaybe
values with the<|>
operator, where the firstJust
encountered is taken.Just x <|> Just y == Just x Nothing <|> Just y == Just y Nothing <|> Nothing == Nothing
Plus Maybe
The
Plus
instance provides a defaultMaybe
value:empty :: Maybe _ == Nothing
Alternative Maybe
The
Alternative
instance guarantees that there are bothApplicative
andPlus
instances forMaybe
.Bind Maybe
The
Bind
instance allows sequencing ofMaybe
values and functions that return aMaybe
by using the>>=
operator:Just x >>= f = f x Nothing >>= f = Nothing
Monad Maybe
The
Monad
instance guarantees that there are bothApplicative
andBind
instances forMaybe
. This also enables thedo
syntactic sugar:do x' <- x y' <- y pure (f x' y')
Which is equivalent to:
x >>= (\x' -> y >>= (\y' -> pure (f x' y')))
MonadZero Maybe
Extend Maybe
The
Extend
instance allows sequencing ofMaybe
values and functions that accept aMaybe a
and return a non-Maybe
result using the<<=
operator.f <<= Nothing = Nothing f <<= x = Just (f x)
Invariant Maybe
(Semigroup a) => Semigroup (Maybe a)
The
Semigroup
instance enables use of the operator<>
onMaybe
values whenever there is aSemigroup
instance for the type theMaybe
contains. The exact behaviour of<>
depends on the "inner"Semigroup
instance, but generally captures the notion of appending or combining things.Just x <> Just y = Just (x <> y) Just x <> Nothing = Just x Nothing <> Just y = Just y Nothing <> Nothing = Nothing
(Semigroup a) => Monoid (Maybe a)
(Eq a) => Eq (Maybe a)
Eq1 Maybe
(Ord a) => Ord (Maybe a)
Ord1 Maybe
(Bounded a) => Bounded (Maybe a)
(Show a) => Show (Maybe a)
The
Show
instance allowsMaybe
values to be rendered as a string withshow
whenever there is anShow
instance for the type theMaybe
contains.
#maybe Source
maybe :: forall b a. b -> (a -> b) -> Maybe a -> b
Takes a default value, a function, and a Maybe
value. If the Maybe
value is Nothing
the default value is returned, otherwise the function
is applied to the value inside the Just
and the result is returned.
maybe x f Nothing == x
maybe x f (Just y) == f y
#maybe' Source
maybe' :: forall b a. (Unit -> b) -> (a -> b) -> Maybe a -> b
Similar to maybe
but for use in cases where the default value may be
expensive to compute. As PureScript is not lazy, the standard maybe
has
to evaluate the default value before returning the result, whereas here
the value is only computed when the Maybe
is known to be Nothing
.
maybe' (\_ -> x) f Nothing == x
maybe' (\_ -> x) f (Just y) == f y
#fromMaybe' Source
fromMaybe' :: forall a. (Unit -> a) -> Maybe a -> a
Similar to fromMaybe
but for use in cases where the default value may be
expensive to compute. As PureScript is not lazy, the standard fromMaybe
has to evaluate the default value before returning the result, whereas here
the value is only computed when the Maybe
is known to be Nothing
.
fromMaybe' (\_ -> x) Nothing == x
fromMaybe' (\_ -> x) (Just y) == y
#optional Source
optional :: forall a f. Alternative f => f a -> f (Maybe a)
One or none.
optional empty = pure Nothing
optional (pure x) = pure (Just x)
The
Functor
instance allows functions to transform the contents of aJust
with the<$>
operator:Nothing
values are left untouched: