Skip to content

Commit a0ff5eb

Browse files
committed
Merge pull request #15 from purescript/lazy-folds
Add "lazy" version of `maybe`
2 parents 7d20791 + e21d347 commit a0ff5eb

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

docs/Data/Maybe.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ maybe x f Nothing == x
5555
maybe x f (Just y) == f y
5656
```
5757

58+
#### `maybe'`
59+
60+
``` purescript
61+
maybe' :: forall a b. (Unit -> b) -> (a -> b) -> Maybe a -> b
62+
```
63+
64+
Similar to `maybe` but for use in cases where the default value may be
65+
expensive to compute. As PureScript is not lazy, the standard `maybe` has
66+
to evaluate the default value before returning the result, whereas here
67+
the value is only computed when the `Maybe` is known to be `Nothing`.
68+
69+
``` purescript
70+
maybe' (\_ -> x) f Nothing == x
71+
maybe' (\_ -> x) f (Just y) == f y
72+
```
73+
5874
#### `fromMaybe`
5975

6076
``` purescript
@@ -70,6 +86,22 @@ fromMaybe x Nothing == x
7086
fromMaybe x (Just y) == y
7187
```
7288

89+
#### `fromMaybe'`
90+
91+
``` purescript
92+
fromMaybe' :: forall a. (Unit -> a) -> Maybe a -> a
93+
```
94+
95+
Similar to `fromMaybe` but for use in cases where the default value may be
96+
expensive to compute. As PureScript is not lazy, the standard `fromMaybe`
97+
has to evaluate the default value before returning the result, whereas here
98+
the value is only computed when the `Maybe` is known to be `Nothing`.
99+
100+
``` purescript
101+
fromMaybe' (\_ -> x) Nothing == x
102+
fromMaybe' (\_ -> x) (Just y) == y
103+
```
104+
73105
#### `isJust`
74106

75107
``` purescript

src/Data/Maybe.purs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ maybe :: forall a b. b -> (a -> b) -> Maybe a -> b
2727
maybe b _ Nothing = b
2828
maybe _ 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
3851
fromMaybe :: forall a. a -> Maybe a -> a
3952
fromMaybe 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`.
4267
isJust :: forall a. Maybe a -> Boolean
4368
isJust = maybe false (const true)

0 commit comments

Comments
 (0)