Data.Maybe
- Package
- purescript-maybe
- Repository
- purescript/purescript-maybe
#Maybe Source
data Maybe aThe 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 MaybeApply MaybeThe
Applyinstance allows functions contained within aJustto transform a value contained within aJustusing theapplyoperator:Just f <*> Just x == Just (f x)Nothingvalues are left untouched:Just f <*> Nothing == Nothing Nothing <*> Just x == NothingCombining
Functor's<$>withApply's<*>can be used transform a pure function to takeMaybe-typed arguments sof :: a -> b -> cbecomesf :: 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 isNothingmeans the whole result becomesNothingalso:f <$> Nothing <*> Just y == Nothing f <$> Just x <*> Nothing == Nothing f <$> Nothing <*> Nothing == NothingApplicative MaybeThe
Applicativeinstance enables lifting of values intoMaybewith thepurefunction:pure x :: Maybe _ == Just xCombining
Functor's<$>withApply's<*>andApplicative'spurecan be used to pass a mixture ofMaybeand non-Maybetyped values to a function that does not usually expect them, by usingpurefor any value that is not alreadyMaybetyped:f <$> Just x <*> pure y == Just (f x y)Even though
pure = Justit is recommended to usepurein situations like this as it allows the choice ofApplicativeto be changed later without having to go through and replaceJustwith a new constructor.Alt MaybeThe
Altinstance allows for a choice to be made between twoMaybevalues with the<|>operator, where the firstJustencountered is taken.Just x <|> Just y == Just x Nothing <|> Just y == Just y Nothing <|> Nothing == NothingPlus MaybeThe
Plusinstance provides a defaultMaybevalue:empty :: Maybe _ == NothingAlternative MaybeThe
Alternativeinstance guarantees that there are bothApplicativeandPlusinstances forMaybe.Bind MaybeThe
Bindinstance allows sequencing ofMaybevalues and functions that return aMaybeby using the>>=operator:Just x >>= f = f x Nothing >>= f = NothingMonad MaybeThe
Monadinstance guarantees that there are bothApplicativeandBindinstances forMaybe. This also enables thedosyntactic sugar:do x' <- x y' <- y pure (f x' y')Which is equivalent to:
x >>= (\x' -> y >>= (\y' -> pure (f x' y')))Which is equivalent to:
case x of Nothing -> Nothing Just x' -> case y of Nothing -> Nothing Just y' -> Just (f x' y')Extend MaybeThe
Extendinstance allows sequencing ofMaybevalues and functions that accept aMaybe aand return a non-Mayberesult using the<<=operator.f <<= Nothing = Nothing f <<= x = Just (f x)Invariant Maybe(Semigroup a) => Semigroup (Maybe a)The
Semigroupinstance enables use of the operator<>onMaybevalues whenever there is aSemigroupinstance for the type theMaybecontains. The exact behaviour of<>depends on the "inner"Semigroupinstance, 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)(Semiring a) => Semiring (Maybe a)(Eq a) => Eq (Maybe a)The
Eqinstance allowsMaybevalues to be checked for equality with==and inequality with/=whenever there is anEqinstance for the type theMaybecontains.Eq1 Maybe(Ord a) => Ord (Maybe a)The
Ordinstance allowsMaybevalues to be compared withcompare,>,>=,<and<=whenever there is anOrdinstance for the type theMaybecontains.Nothingis considered to be less than anyJustvalue.Ord1 Maybe(Bounded a) => Bounded (Maybe a)(Show a) => Show (Maybe a)The
Showinstance allowsMaybevalues to be rendered as a string withshowwhenever there is anShowinstance for the type theMaybecontains.Generic (Maybe a) _
#maybe Source
maybe :: forall a b. b -> (a -> b) -> Maybe a -> bTakes 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 a b. (Unit -> b) -> (a -> b) -> Maybe a -> bSimilar 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 -> aSimilar 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 f a. Alt f => Applicative f => f a -> f (Maybe a)One or none.
optional empty = pure Nothing
The behaviour of optional (pure x) depends on whether the Alt instance
satisfy the left catch law (pure a <|> b = pure a).
Either e does:
optional (Right x) = Right (Just x)
But Array does not:
optional [x] = [Just x, Nothing]
The
Functorinstance allows functions to transform the contents of aJustwith the<$>operator:Nothingvalues are left untouched: