@@ -27,6 +27,19 @@ maybe :: forall a b. b -> (a -> b) -> Maybe a -> b
27
27
maybe b _ Nothing = b
28
28
maybe _ f (Just a) = f a
29
29
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
+
30
43
-- | Takes a default value, and a `Maybe` value. If the `Maybe` value is
31
44
-- | `Nothing` the default value is returned, otherwise the value inside the
32
45
-- | `Just` is returned.
@@ -38,6 +51,18 @@ maybe _ f (Just a) = f a
38
51
fromMaybe :: forall a . a -> Maybe a -> a
39
52
fromMaybe a = maybe a (id :: forall a . a -> a )
40
53
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
+
41
66
-- | Returns `true` when the `Maybe` value was constructed with `Just`.
42
67
isJust :: forall a . Maybe a -> Boolean
43
68
isJust = maybe false (const true )
0 commit comments