4
4
5
5
#### ` Maybe `
6
6
7
+ ``` purescript
8
+ data Maybe a
9
+ = Nothing
10
+ | Just a
11
+ ```
12
+
7
13
The ` Maybe ` type is used to represent optional values and can be seen as
8
14
something like a type-safe ` null ` , where ` Nothing ` is ` null ` and ` Just x `
9
15
is the non-null value ` x ` .
10
16
11
- data Maybe a
12
- = Nothing
13
- | Just a
14
-
15
17
#### ` maybe `
16
18
19
+ ``` purescript
20
+ maybe :: forall a b. b -> (a -> b) -> Maybe a -> b
21
+ ```
22
+
17
23
Takes a default value, a function, and a ` Maybe ` value. If the ` Maybe `
18
24
value is ` Nothing ` the default value is returned, otherwise the function
19
25
is applied to the value inside the ` Just ` and the result is returned.
@@ -23,10 +29,12 @@ maybe default f Nothing == default
23
29
maybe default f (Just x) == f x
24
30
```
25
31
26
- maybe :: forall a b. b -> (a -> b) -> Maybe a -> b
27
-
28
32
#### ` fromMaybe `
29
33
34
+ ``` purescript
35
+ fromMaybe :: forall a. a -> Maybe a -> a
36
+ ```
37
+
30
38
Takes a default value, and a ` Maybe ` value. If the ` Maybe ` value is
31
39
` Nothing ` the default value is returned, otherwise the value inside the
32
40
` Just ` is returned.
@@ -36,22 +44,28 @@ fromMaybe default Nothing == default
36
44
fromMaybe default (Just x) == x
37
45
```
38
46
39
- fromMaybe :: forall a. a -> Maybe a -> a
40
-
41
47
#### ` isJust `
42
48
43
- Returns ` true ` when the ` Maybe ` value was constructed with ` Just ` .
49
+ ``` purescript
50
+ isJust :: forall a. Maybe a -> Boolean
51
+ ```
44
52
45
- isJust :: forall a. Maybe a -> Boolean
53
+ Returns ` true ` when the ` Maybe ` value was constructed with ` Just ` .
46
54
47
55
#### ` isNothing `
48
56
49
- Returns ` true ` when the ` Maybe ` value is ` Nothing ` .
57
+ ``` purescript
58
+ isNothing :: forall a. Maybe a -> Boolean
59
+ ```
50
60
51
- isNothing :: forall a. Maybe a -> Boolean
61
+ Returns ` true ` when the ` Maybe ` value is ` Nothing ` .
52
62
53
63
#### ` functorMaybe `
54
64
65
+ ``` purescript
66
+ instance functorMaybe :: Functor Maybe
67
+ ```
68
+
55
69
The ` Functor ` instance allows functions to transform the contents of a
56
70
` Just ` with the ` <$> ` operator:
57
71
@@ -65,10 +79,12 @@ f <$> Just x == Just (f x)
65
79
f <$> Nothing == Nothing
66
80
```
67
81
68
- instance functorMaybe :: Functor Maybe
69
-
70
82
#### ` applyMaybe `
71
83
84
+ ``` purescript
85
+ instance applyMaybe :: Apply Maybe
86
+ ```
87
+
72
88
The ` Apply ` instance allows functions contained within a ` Just ` to
73
89
transform a value contained within a ` Just ` using the ` (<*>) ` operator:
74
90
@@ -101,10 +117,12 @@ f <$> Just x <*> Nothing == Nothing
101
117
f <$> Nothing <*> Nothing == Nothing
102
118
```
103
119
104
- instance applyMaybe :: Apply Maybe
105
-
106
120
#### ` applicativeMaybe `
107
121
122
+ ``` purescript
123
+ instance applicativeMaybe :: Applicative Maybe
124
+ ```
125
+
108
126
The ` Applicative ` instance enables lifting of values into ` Maybe ` with the
109
127
` pure ` or ` return ` function (` return ` is an alias for ` pure ` ):
110
128
@@ -126,10 +144,12 @@ Even though `pure = Just` it is recommended to use `pure` in situations
126
144
like this as it allows the choice of ` Applicative ` to be changed later
127
145
without having to go through and replace ` Just ` with a new constructor.
128
146
129
- instance applicativeMaybe :: Applicative Maybe
130
-
131
147
#### ` altMaybe `
132
148
149
+ ``` purescript
150
+ instance altMaybe :: Alt Maybe
151
+ ```
152
+
133
153
The ` Alt ` instance allows for a choice to be made between two ` Maybe `
134
154
values with the ` <|> ` operator, where the first ` Just ` encountered
135
155
is taken.
@@ -140,27 +160,33 @@ Nothing <|> Just y == Just y
140
160
Nothing <|> Nothing == Nothing
141
161
```
142
162
143
- instance altMaybe :: Alt Maybe
144
-
145
163
#### ` plusMaybe `
146
164
165
+ ``` purescript
166
+ instance plusMaybe :: Plus Maybe
167
+ ```
168
+
147
169
The ` Plus ` instance provides a default ` Maybe ` value:
148
170
149
171
``` purescript
150
172
empty :: Maybe _ == Nothing
151
173
```
152
174
153
- instance plusMaybe :: Plus Maybe
154
-
155
175
#### ` alternativeMaybe `
156
176
177
+ ``` purescript
178
+ instance alternativeMaybe :: Alternative Maybe
179
+ ```
180
+
157
181
The ` Alternative ` instance guarantees that there are both ` Applicative ` and
158
182
` Plus ` instances for ` Maybe ` .
159
183
160
- instance alternativeMaybe :: Alternative Maybe
161
-
162
184
#### ` bindMaybe `
163
185
186
+ ``` purescript
187
+ instance bindMaybe :: Bind Maybe
188
+ ```
189
+
164
190
The ` Bind ` instance allows sequencing of ` Maybe ` values and functions that
165
191
return a ` Maybe ` by using the ` >>= ` operator:
166
192
@@ -169,10 +195,12 @@ Just x >>= f = f x
169
195
Nothing >>= f = Nothing
170
196
```
171
197
172
- instance bindMaybe :: Bind Maybe
173
-
174
198
#### ` monadMaybe `
175
199
200
+ ``` purescript
201
+ instance monadMaybe :: Monad Maybe
202
+ ```
203
+
176
204
The ` Monad ` instance guarantees that there are both ` Applicative ` and
177
205
` Bind ` instances for ` Maybe ` . This also enables the ` do ` syntactic sugar:
178
206
@@ -189,17 +217,21 @@ Which is equivalent to:
189
217
x >>= (\x' -> y >>= (\y' -> pure (f x' y')))
190
218
```
191
219
192
- instance monadMaybe :: Monad Maybe
193
-
194
220
#### ` monadPlusMaybe `
195
221
222
+ ``` purescript
223
+ instance monadPlusMaybe :: MonadPlus Maybe
224
+ ```
225
+
196
226
The ` MonadPlus ` instance guarantees that there are both ` Monad ` and
197
227
` Alternative ` instances for ` Maybe ` .
198
228
199
- instance monadPlusMaybe :: MonadPlus Maybe
200
-
201
229
#### ` extendMaybe `
202
230
231
+ ``` purescript
232
+ instance extendMaybe :: Extend Maybe
233
+ ```
234
+
203
235
The ` Extend ` instance allows sequencing of ` Maybe ` values and functions
204
236
that accept a ` Maybe a ` and return a non-` Maybe ` result using the
205
237
` <<= ` operator.
@@ -209,10 +241,12 @@ f <<= Nothing = Nothing
209
241
f <<= Just x = Just (f x)
210
242
```
211
243
212
- instance extendMaybe :: Extend Maybe
213
-
214
244
#### ` semigroupMaybe `
215
245
246
+ ``` purescript
247
+ instance semigroupMaybe :: (Semigroup a) => Semigroup (Maybe a)
248
+ ```
249
+
216
250
The ` Semigroup ` instance enables use of the operator ` <> ` on ` Maybe ` values
217
251
whenever there is a ` Semigroup ` instance for the type the ` Maybe ` contains.
218
252
The exact behaviour of ` <> ` depends on the "inner" ` Semigroup ` instance,
@@ -225,41 +259,47 @@ Nothing <> Just y = Just y
225
259
Nothing <> Nothing = Nothing
226
260
```
227
261
228
- instance semigroupMaybe :: (Semigroup a) => Semigroup (Maybe a)
229
-
230
262
#### ` showMaybe `
231
263
264
+ ``` purescript
265
+ instance showMaybe :: (Show a) => Show (Maybe a)
266
+ ```
267
+
232
268
The ` Show ` instance allows ` Maybe ` values to be rendered as a string with
233
269
` show ` whenever there is an ` Show ` instance for the type the ` Maybe `
234
270
contains.
235
271
236
- instance showMaybe :: (Show a) => Show (Maybe a)
237
-
238
272
#### ` eqMaybe `
239
273
274
+ ``` purescript
275
+ instance eqMaybe :: (Eq a) => Eq (Maybe a)
276
+ ```
277
+
240
278
The ` Eq ` instance allows ` Maybe ` values to be checked for equality with
241
279
` == ` and inequality with ` /= ` whenever there is an ` Eq ` instance for the
242
280
type the ` Maybe ` contains.
243
281
244
- instance eqMaybe :: (Eq a) => Eq (Maybe a)
245
-
246
282
#### ` ordMaybe `
247
283
284
+ ``` purescript
285
+ instance ordMaybe :: (Ord a) => Ord (Maybe a)
286
+ ```
287
+
248
288
The ` Ord ` instance allows ` Maybe ` values to be compared with
249
289
` compare ` , ` > ` , ` >= ` , ` < ` and ` <= ` whenever there is an ` Ord ` instance for
250
290
the type the ` Maybe ` contains.
251
291
252
292
` Nothing ` is considered to be less than any ` Just ` value.
253
293
254
- instance ordMaybe :: (Ord a) => Ord (Maybe a)
255
-
256
294
257
295
## Module Data.Maybe.Unsafe
258
296
259
297
#### ` fromJust `
260
298
299
+ ``` purescript
300
+ fromJust :: forall a. Maybe a -> a
301
+ ```
302
+
261
303
A partial function that extracts the value from the ` Just ` data
262
304
constructor. Passing ` Nothing ` to ` fromJust ` will throw an error at
263
- runtime.
264
-
265
- fromJust :: forall a. Maybe a -> a
305
+ runtime.
0 commit comments