2
2
3
3
## Module Data.Maybe
4
4
5
- ### Types
6
-
7
5
#### ` Maybe `
8
6
9
7
The ` Maybe ` type is used to represent optional values and can be seen as
@@ -14,54 +12,60 @@ is the non-null value `x`.
14
12
= Nothing
15
13
| Just a
16
14
15
+ #### ` maybe `
17
16
18
- ### Type Class Instances
17
+ Takes a default value, a function, and a ` Maybe ` value. If the ` Maybe `
18
+ value is ` Nothing ` the default value is returned, otherwise the function
19
+ is applied to the value inside the ` Just ` and the result is returned.
19
20
20
- #### ` altMaybe `
21
+ ``` purescript
22
+ maybe default f Nothing == default
23
+ maybe default f (Just x) == f x
24
+ ```
21
25
22
- The ` Alt ` instance allows for a choice to be made between two ` Maybe `
23
- values with the ` <|> ` operator, where the first ` Just ` encountered
24
- is taken.
26
+ maybe :: forall a b. b -> (a -> b) -> Maybe a -> b
27
+
28
+ #### ` fromMaybe `
29
+
30
+ Takes a default value, and a ` Maybe ` value. If the ` Maybe ` value is
31
+ ` Nothing ` the default value is returned, otherwise the value inside the
32
+ ` Just ` is returned.
25
33
26
34
``` purescript
27
- Just x <|> Just y == Just x
28
- Nothing <|> Just y == Just y
29
- Nothing <|> Nothing == Nothing
35
+ fromMaybe default Nothing == default
36
+ fromMaybe default (Just x) == x
30
37
```
31
38
32
- instance altMaybe :: Alt Maybe
39
+ fromMaybe :: forall a. a -> Maybe a -> a
33
40
34
- #### ` alternativeMaybe `
41
+ #### ` isJust `
35
42
36
- The ` Alternative ` instance guarantees that there are both ` Applicative ` and
37
- ` Plus ` instances for ` Maybe ` .
43
+ Returns ` true ` when the ` Maybe ` value was constructed with ` Just ` .
38
44
39
- instance alternativeMaybe :: Alternative Maybe
45
+ isJust :: forall a. Maybe a -> Boolean
40
46
41
- #### ` applicativeMaybe `
47
+ #### ` isNothing `
42
48
43
- The ` Applicative ` instance enables lifting of values into ` Maybe ` with the
44
- ` pure ` or ` return ` function (` return ` is an alias for ` pure ` ):
49
+ Returns ` true ` when the ` Maybe ` value is ` Nothing ` .
50
+
51
+ isNothing :: forall a. Maybe a -> Boolean
52
+
53
+ #### ` functorMaybe `
54
+
55
+ The ` Functor ` instance allows functions to transform the contents of a
56
+ ` Just ` with the ` <$> ` operator:
45
57
46
58
``` purescript
47
- pure x :: Maybe _ == Just x
48
- return x :: Maybe _ == Just x
59
+ f <$> Just x == Just (f x)
49
60
```
50
61
51
- Combining ` Functor ` 's' ` <$> ` with ` Apply ` 's ` <*> ` and ` Applicative ` 's
52
- ` pure ` can be used to pass a mixture of ` Maybe ` and non-` Maybe ` typed
53
- values to a function that does not usually expect them, by using ` pure `
54
- for any value that is not already ` Maybe ` typed:
62
+ ` Nothing ` values are left untouched:
55
63
56
64
``` purescript
57
- f <$> Just x <*> pure y == Just (f x y)
65
+ f <$> Nothing == Nothing
58
66
```
59
67
60
- Even though ` pure = Just ` it is recommended to use ` pure ` in situations
61
- like this as it allows the choice of ` Applicative ` to be changed later
62
- without having to go through and replace ` Just ` with a new constructor.
63
-
64
- instance applicativeMaybe :: Applicative Maybe
68
+ instance functorMaybe :: Functor Maybe
65
69
66
70
#### ` applyMaybe `
67
71
@@ -99,55 +103,73 @@ f <$> Nothing <*> Nothing == Nothing
99
103
100
104
instance applyMaybe :: Apply Maybe
101
105
102
- #### ` bindMaybe `
106
+ #### ` applicativeMaybe `
103
107
104
- The ` Bind ` instance allows sequencing of ` Maybe ` values and functions that
105
- return a ` Maybe ` by using the ` >>= ` operator :
108
+ The ` Applicative ` instance enables lifting of values into ` Maybe ` with the
109
+ ` pure ` or ` return ` function ( ` return ` is an alias for ` pure ` ) :
106
110
107
111
``` purescript
108
- Just x >>= f = f x
109
- Nothing >>= f = Nothing
112
+ pure x :: Maybe _ == Just x
113
+ return x :: Maybe _ == Just x
110
114
```
111
115
112
- instance bindMaybe :: Bind Maybe
116
+ Combining ` Functor ` 's' ` <$> ` with ` Apply ` 's ` <*> ` and ` Applicative ` 's
117
+ ` pure ` can be used to pass a mixture of ` Maybe ` and non-` Maybe ` typed
118
+ values to a function that does not usually expect them, by using ` pure `
119
+ for any value that is not already ` Maybe ` typed:
113
120
114
- #### ` eqMaybe `
121
+ ``` purescript
122
+ f <$> Just x <*> pure y == Just (f x y)
123
+ ```
115
124
116
- The ` Eq ` instance allows ` Maybe ` values to be checked for equality with
117
- ` == ` and inequality with ` /= ` whenever there is an ` Eq ` instance for the
118
- type the ` Maybe ` contains .
125
+ Even though ` pure = Just ` it is recommended to use ` pure ` in situations
126
+ like this as it allows the choice of ` Applicative ` to be changed later
127
+ without having to go through and replace ` Just ` with a new constructor .
119
128
120
- instance eqMaybe :: (Eq a) => Eq ( Maybe a)
129
+ instance applicativeMaybe :: Applicative Maybe
121
130
122
- #### ` extendMaybe `
131
+ #### ` altMaybe `
123
132
124
- The ` Extend ` instance allows sequencing of ` Maybe ` values and functions
125
- that accept a ` Maybe a ` and return a non- ` Maybe ` result using the
126
- ` <<= ` operator .
133
+ The ` Alt ` instance allows for a choice to be made between two ` Maybe `
134
+ values with the ` <|> ` operator, where the first ` Just ` encountered
135
+ is taken .
127
136
128
137
``` purescript
129
- f <<= Nothing = Nothing
130
- f <<= Just x = Just (f x)
138
+ Just x <|> Just y == Just x
139
+ Nothing <|> Just y == Just y
140
+ Nothing <|> Nothing == Nothing
131
141
```
132
142
133
- instance extendMaybe :: Extend Maybe
143
+ instance altMaybe :: Alt Maybe
134
144
135
- #### ` functorMaybe `
145
+ #### ` plusMaybe `
136
146
137
- The ` Functor ` instance allows functions to transform the contents of a
138
- ` Just ` with the ` <$> ` operator:
147
+ The ` Plus ` instance provides a default ` Maybe ` value:
139
148
140
149
``` purescript
141
- f <$> Just x == Just (f x)
150
+ empty :: Maybe _ == Nothing
142
151
```
143
152
144
- ` Nothing ` values are left untouched:
153
+ instance plusMaybe :: Plus Maybe
154
+
155
+ #### ` alternativeMaybe `
156
+
157
+ The ` Alternative ` instance guarantees that there are both ` Applicative ` and
158
+ ` Plus ` instances for ` Maybe ` .
159
+
160
+ instance alternativeMaybe :: Alternative Maybe
161
+
162
+ #### ` bindMaybe `
163
+
164
+ The ` Bind ` instance allows sequencing of ` Maybe ` values and functions that
165
+ return a ` Maybe ` by using the ` >>= ` operator:
145
166
146
167
``` purescript
147
- f <$> Nothing == Nothing
168
+ Just x >>= f = f x
169
+ Nothing >>= f = Nothing
148
170
```
149
171
150
- instance functorMaybe :: Functor Maybe
172
+ instance bindMaybe :: Bind Maybe
151
173
152
174
#### ` monadMaybe `
153
175
@@ -176,25 +198,18 @@ The `MonadPlus` instance guarantees that there are both `Monad` and
176
198
177
199
instance monadPlusMaybe :: MonadPlus Maybe
178
200
179
- #### ` ordMaybe `
180
-
181
- The ` Ord ` instance allows ` Maybe ` values to be compared with
182
- ` compare ` , ` > ` , ` >= ` , ` < ` and ` <= ` whenever there is an ` Ord ` instance for
183
- the type the ` Maybe ` contains.
184
-
185
- ` Nothing ` is considered to be less than any ` Just ` value.
186
-
187
- instance ordMaybe :: (Ord a) => Ord (Maybe a)
188
-
189
- #### ` plusMaybe `
201
+ #### ` extendMaybe `
190
202
191
- The ` Plus ` instance provides a default ` Maybe ` value:
203
+ The ` Extend ` instance allows sequencing of ` Maybe ` values and functions
204
+ that accept a ` Maybe a ` and return a non-` Maybe ` result using the
205
+ ` <<= ` operator.
192
206
193
207
``` purescript
194
- empty :: Maybe _ == Nothing
208
+ f <<= Nothing = Nothing
209
+ f <<= Just x = Just (f x)
195
210
```
196
211
197
- instance plusMaybe :: Plus Maybe
212
+ instance extendMaybe :: Extend Maybe
198
213
199
214
#### ` semigroupMaybe `
200
215
@@ -220,52 +235,27 @@ contains.
220
235
221
236
instance showMaybe :: (Show a) => Show (Maybe a)
222
237
238
+ #### ` eqMaybe `
223
239
224
- ### Values
225
-
226
- #### ` fromMaybe `
227
-
228
- Takes a default value, and a ` Maybe ` value. If the ` Maybe ` value is
229
- ` Nothing ` the default value is returned, otherwise the value inside the
230
- ` Just ` is returned.
231
-
232
- ``` purescript
233
- fromMaybe default Nothing == default
234
- fromMaybe default (Just x) == x
235
- ```
236
-
237
- fromMaybe :: forall a. a -> Maybe a -> a
238
-
239
- #### ` isJust `
240
-
241
- Returns ` true ` when the ` Maybe ` value was constructed with ` Just ` .
242
-
243
- isJust :: forall a. Maybe a -> Boolean
244
-
245
- #### ` isNothing `
246
-
247
- Returns ` true ` when the ` Maybe ` value is ` Nothing ` .
240
+ The ` Eq ` instance allows ` Maybe ` values to be checked for equality with
241
+ ` == ` and inequality with ` /= ` whenever there is an ` Eq ` instance for the
242
+ type the ` Maybe ` contains.
248
243
249
- isNothing :: forall a. Maybe a -> Boolean
244
+ instance eqMaybe :: (Eq a) => Eq (Maybe a)
250
245
251
- #### ` maybe `
246
+ #### ` ordMaybe `
252
247
253
- Takes a default value, a function, and a ` Maybe ` value. If the ` Maybe `
254
- value is ` Nothing ` the default value is returned, otherwise the function
255
- is applied to the value inside the ` Just ` and the result is returned .
248
+ The ` Ord ` instance allows ` Maybe ` values to be compared with
249
+ ` compare ` , ` > ` , ` >= ` , ` < ` and ` <= ` whenever there is an ` Ord ` instance for
250
+ the type the ` Maybe ` contains .
256
251
257
- ``` purescript
258
- maybe default f Nothing == default
259
- maybe default f (Just x) == f x
260
- ```
252
+ ` Nothing ` is considered to be less than any ` Just ` value.
261
253
262
- maybe :: forall a b. b -> (a -> b) -> Maybe a -> b
254
+ instance ordMaybe :: (Ord a) => Ord ( Maybe a)
263
255
264
256
265
257
## Module Data.Maybe.Unsafe
266
258
267
- ### Values
268
-
269
259
#### ` fromJust `
270
260
271
261
A partial function that extracts the value from the ` Just ` data
0 commit comments