@@ -27,6 +27,19 @@ maybe :: forall a b. b -> (a -> b) -> Maybe a -> b
2727maybe b _ Nothing = b
2828maybe _ f (Just a) = f a
2929
30+ -- | Similar to `maybe` but for use in cases where the default value may be
31+ -- | expensive to compute. As PureScript is not lazy, the standard `maybe` has
32+ -- | to evaluate the default value before returning the result, whereas here
33+ -- | the value is only computed when the `Maybe` is known to be `Nothing`.
34+ -- |
35+ -- | ``` purescript
36+ -- | maybe' (\_ -> x) f Nothing == x
37+ -- | maybe' (\_ -> x) f (Just y) == f y
38+ -- | ```
39+ maybe' :: forall a b . (Unit -> b ) -> (a -> b ) -> Maybe a -> b
40+ maybe' g _ Nothing = g unit
41+ maybe' _ f (Just a) = f a
42+
3043-- | Takes a default value, and a `Maybe` value. If the `Maybe` value is
3144-- | `Nothing` the default value is returned, otherwise the value inside the
3245-- | `Just` is returned.
@@ -38,6 +51,18 @@ maybe _ f (Just a) = f a
3851fromMaybe :: forall a . a -> Maybe a -> a
3952fromMaybe a = maybe a (id :: forall a . a -> a )
4053
54+ -- | Similar to `fromMaybe` but for use in cases where the default value may be
55+ -- | expensive to compute. As PureScript is not lazy, the standard `fromMaybe`
56+ -- | has to evaluate the default value before returning the result, whereas here
57+ -- | the value is only computed when the `Maybe` is known to be `Nothing`.
58+ -- |
59+ -- | ``` purescript
60+ -- | fromMaybe' (\_ -> x) Nothing == x
61+ -- | fromMaybe' (\_ -> x) (Just y) == y
62+ -- | ```
63+ fromMaybe' :: forall a . (Unit -> a ) -> Maybe a -> a
64+ fromMaybe' a = maybe' a (id :: forall a . a -> a )
65+
4166-- | Returns `true` when the `Maybe` value was constructed with `Just`.
4267isJust :: forall a . Maybe a -> Boolean
4368isJust = maybe false (const true )
0 commit comments