22
22
* along with this program; if not, write to the Free Software
23
23
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24
24
25
- (* * [`Belt.Option`]()
25
+ (* *
26
+ In Belt we represent the existence and nonexistence of a value by wrapping it
27
+ with the `option` type. In order to make it a bit more convenient to work with
28
+ option-types, Belt provides utility-functions for it.
29
+
30
+ The `option` type is a part of the ReScript standard library which is defined like this:
26
31
27
- Utilities for option data type
32
+ ```res sig
33
+ type option<'a> = None | Some('a)
34
+ ```
35
+
36
+ ```res example
37
+ let someString: option<string> = Some("hello")
38
+ ```
28
39
*)
29
40
30
41
@@ -33,14 +44,12 @@ val keepU : 'a option -> ('a -> bool [@bs]) -> 'a option
33
44
34
45
val keep : 'a option -> ('a -> bool ) -> 'a option
35
46
(* *
36
- `keep optionValue p `
47
+ If ` optionValue` is `Some(value)` and `p(value) = true`, it returns `Some(value)`; otherwise returns `None `
37
48
38
- If `optionValue` is `Some value` and `p value = true`, it returns `Some value`; otherwise returns `None`
39
-
40
- ```
41
- keep (Some 10)(fun x -> x > 5);; (* returns `Some 10` *)
42
- keep (Some 4)(fun x -> x > 5);; (* returns `None` *)
43
- keep None (fun x -> x > 5);; (* returns `None` *)
49
+ ```res example
50
+ Belt.Option.keep(Some(10), x => x > 5) /* returns `Some(10)` */
51
+ Belt.Option.keep(Some(4), x => x > 5) /* returns `None` */
52
+ Belt.Option.keep(None, x => x > 5) /* returns `None` */
44
53
```
45
54
*)
46
55
@@ -49,45 +58,50 @@ val forEachU : 'a option -> ('a -> unit [@bs]) -> unit
49
58
50
59
val forEach : 'a option -> ('a -> unit ) -> unit
51
60
(* *
52
- `forEach optionValue f`
53
-
54
- If `optionValue` is `Some value`, it calls `f value`; otherwise returns `()`
61
+ If `optionValue` is `Some(value`), it calls `f(value)`; otherwise returns `()`
55
62
56
- ```
57
- forEach (Some "thing")(fun x - > Js.log x);; ( * logs "thing" *)
58
- forEach None (fun x - > Js.log x);; ( * returns () *)
63
+ ```res example
64
+ Belt.Option. forEach(Some( "thing"), x = > Js.log(x)) / * logs "thing" */
65
+ Belt.Option. forEach( None, x = > Js.log(x)) / * returns () */
59
66
```
60
67
*)
61
68
62
69
val getExn : 'a option -> 'a
63
- (* * `getExn optionalValue`
64
- Returns `value` if `optionalValue ` is `Some value`, otherwise raises `getExn`
70
+ (* *
71
+ Raises an Error in case `None ` is provided. Use with care.
65
72
66
- ```
67
- getExn (Some 3) = 3;;
68
- getExn None (* Raises getExn error *)
69
- ```
73
+ ```res example
74
+ Belt.Option.getExn(Some(3)) /* 3 */
75
+
76
+ Belt.Option.getExn(None) /* Raises an Error */
77
+ ```
70
78
*)
71
79
72
80
external getUnsafe :
73
81
'a option -> 'a = " %identity"
74
- (* * `getUnsafe x` returns x
75
- This is an unsafe operation, it assumes x is neither not None
76
- or (Some (None .. ))
82
+ (* *
83
+ `getUnsafe(x)` returns `x`
84
+
85
+ This is an unsafe operation, it assumes `x` is neither `None`
86
+ nor `Some(None(...)))`
77
87
*)
78
88
79
89
val mapWithDefaultU : 'a option -> 'b -> ('a -> 'b [@ bs]) -> 'b
80
90
(* * Uncurried version of `mapWithDefault` *)
81
91
82
92
val mapWithDefault : 'a option -> 'b -> ('a -> 'b ) -> 'b
83
93
(* *
84
- `mapWithDefault optionValue default f`
94
+ If `optionValue` is of `Some(value)`,
95
+ this function returns that value applied with `f`, in other words `f(value)`.
85
96
86
- If `optionValue` is `Some value `, returns `f value`; otherwise returns `default`
97
+ If `optionValue` is `None `, the default is returned.
87
98
88
- ```
89
- mapWithDefault (Some 3) 0 (fun x -> x + 5) = 8;;
90
- mapWithDefault None 0 (fun x -> x + 5) = 0;;
99
+ ```res example
100
+ let someValue = Some(3)
101
+ someValue->Belt.Option.mapWithDefault(0, x => x + 5) /* 8 */
102
+
103
+ let noneValue = None
104
+ noneValue->Belt.Option.mapWithDefault(0, x => x + 5) /* 0 */
91
105
```
92
106
*)
93
107
@@ -96,13 +110,12 @@ val mapU : 'a option -> ('a -> 'b [@bs]) -> 'b option
96
110
97
111
val map : 'a option -> ('a -> 'b ) -> 'b option
98
112
(* *
99
- `map optionValue f`
113
+ If ` optionValue` is `Some(value)` this returns `f(value)`, otherwise it returns `None`.
100
114
101
- If `optionValue` is `Some value`, returns `Some (f value)`; otherwise returns `None`
115
+ ```res example
116
+ Belt.Option.map(Some(3), x => x * x) /* Some(9) */
102
117
103
- ```
104
- map (Some 3) (fun x -> x * x) = (Some 9);;
105
- map None (fun x -> x * x) = None;;
118
+ Belt.Option.map(None, x => x * x) /* None */
106
119
```
107
120
*)
108
121
@@ -111,44 +124,66 @@ val flatMapU : 'a option -> ('a -> 'b option [@bs]) -> 'b option
111
124
112
125
val flatMap : 'a option -> ('a -> 'b option ) -> 'b option
113
126
(* *
114
- `flatMap optionValue f`
127
+ If `optionValue` is `Some(value)`, returns `f(value)`, otherwise returns
128
+ `None`.<br/>
129
+ The function `f` must have a return type of `option<'b>`.
115
130
116
- If `optionValue` is `Some value`, returns `f value`; otherwise returns `None`
117
- The function `f` must have a return type of `'a option`
131
+ ```res example
132
+ let addIfAboveOne = value =>
133
+ if (value > 1) {
134
+ Some(value + 1)
135
+ } else {
136
+ None
137
+ }
118
138
119
- ```
120
- let f (x : float) =
121
- if x >= 0.0 then
122
- Some (sqrt x)
123
- else
124
- None;;
125
-
126
- flatMap (Some 4.0) f = Some 2.0;;
127
- flatMap (Some (-4.0)) f = None;;
128
- flatMap None f = None;;
139
+ Belt.Option.flatMap(Some(2), addIfAboveOne) /* Some(3) */
140
+
141
+ Belt.Option.flatMap(Some(-4), addIfAboveOne) /* None */
142
+
143
+ Belt.Option.flatMap(None, addIfAboveOne) /* None */
129
144
```
130
145
*)
131
146
132
147
val getWithDefault : 'a option -> 'a -> 'a
133
148
(* *
134
- `getWithDefault optionalValue default`
149
+ If ` optionalValue` is `Some(value)`, returns `value`, otherwise default.
135
150
136
- If `optionalValue` is `Some value`, returns `value`, otherwise `default`
151
+ ```res example
152
+ Belt.Option.getWithDefault(None, "Banana") /* Banana */
137
153
154
+ Belt.Option.getWithDefault(Some("Apple"), "Banana") /* Apple */
138
155
```
139
- getWithDefault (Some 1812) 1066 = 1812;;
140
- getWithDefault None 1066 = 1066;;
156
+
157
+ ```res example
158
+ let greet = (firstName: option<string>) =>
159
+ "Greetings " ++ firstName->Belt.Option.getWithDefault("Anonymous")
160
+
161
+ Some("Jane")->greet /* "Greetings Jane" */
162
+
163
+ None->greet /* "Greetings Anonymous" */
141
164
```
142
165
*)
143
166
144
167
val isSome : 'a option -> bool
145
168
(* *
146
- Returns `true` if the argument is `Some value`, `false` otherwise
169
+ Returns `true` if the argument is `Some(value)`, `false` otherwise.
170
+
171
+ ```res example
172
+ Belt.Option.isSome(None) /* false */
173
+
174
+ Belt.Option.isSome(Some(1)) /* true */
175
+ ```
147
176
*)
148
177
149
178
val isNone : 'a option -> bool
150
179
(* *
151
- Returns `true` if the argument is `None`, `false` otherwise
180
+ Returns `true` if the argument is `None`, `false` otherwise.
181
+
182
+ ```res example
183
+ Belt.Option.isNone(None) /* true */
184
+
185
+ Belt.Option.isNone(Some(1)) /* false */
186
+ ```
152
187
*)
153
188
154
189
val eqU : 'a option -> 'b option -> ('a -> 'b -> bool [@ bs]) -> bool
@@ -158,23 +193,26 @@ val eqU : 'a option -> 'b option -> ('a -> 'b -> bool [@bs]) -> bool
158
193
159
194
val eq : 'a option -> 'b option -> ('a -> 'b -> bool ) -> bool
160
195
(* *
161
- `eq optValue1 optvalue2 predicate`
196
+ Evaluates two optional values for equality with respect to a predicate
197
+ function. If both `optValue1` and `optValue2` are `None`, returns `true`.
198
+ If one of the arguments is `Some(value)` and the other is `None`, returns
199
+ `false`.
162
200
163
- Evaluates two optional values for equality with respect to a predicate function.
201
+ If arguments are `Some(value1)` and `Some(value2)`, returns the result of
202
+ `predicate(value1, value2)`; the predicate function must return a bool.
164
203
165
- If both `optValue1` and `optValue2` are `None`, returns `true`.
204
+ ```res example
205
+ let clockEqual = (a, b) => mod(a, 12) == mod(b, 12)
166
206
167
- If one of the arguments is `Some value` and the other is `None`, returns `false`
207
+ open Belt.Option
168
208
169
- If arguments are `Some value1` and `Some value2`, returns the result of `predicate value1 value2`;
170
- the `predicate` function must return a `bool`
209
+ eq(Some(3), Some(15), clockEqual) /* true */
171
210
172
- ```
173
- let clockEqual = (fun a b -> a mod 12 = b mod 12);;
174
- eq (Some 3) (Some 15) clockEqual = true;;
175
- eq (Some 3) None clockEqual = false;;
176
- eq None (Some 3) clockEqual = false;;
177
- eq None None clockEqual = true;;
211
+ eq(Some(3), None, clockEqual) /* false */
212
+
213
+ eq(None, Some(3), clockEqual) /* false */
214
+
215
+ eq(None, None, clockEqual) /* true */
178
216
```
179
217
*)
180
218
@@ -183,25 +221,37 @@ val cmpU : 'a option -> 'b option -> ('a -> 'b -> int [@bs]) -> int
183
221
184
222
val cmp : 'a option -> 'b option -> ('a -> 'b -> int ) -> int
185
223
(* *
186
- `cmp optValue1 optvalue2 comparisonFcn`
224
+ `cmp(optValue1, optValue2, comparisonFunction)` compares two optional values
225
+ with respect to given `comparisonFunction`.
187
226
188
- Compares two optional values with respect to a comparison function
227
+ If both `optValue1` and `optValue2` are `None`, it returns `0`.
189
228
190
- If both `optValue1` and `optValue2` are `None`, returns 0.
229
+ If the first argument is `Some(value1)` and the second is `None`, returns `1`
230
+ (something is greater than nothing).
191
231
192
- If the first argument is `Some value1` and the second is `None`, returns 1 (something is greater than nothing)
232
+ If the first argument is `None` and the second is `Some(value2)`, returns `-1`
233
+ (nothing is less than something).
193
234
194
- If the first argument is `None` and the second is `Some value2`, returns -1 (nothing is less than something)
235
+ If the arguments are `Some(value1)` and `Some(value2)`, returns the result of
236
+ `comparisonFunction(value1, value2)`; comparisonFunction takes two arguments
237
+ and returns `-1` if the first argument is less than the second, `0` if the
238
+ arguments are equal, and `1` if the first argument is greater than the second.
195
239
196
- If the arguments are `Some value1` and `Some value2`, returns the result of `comparisonFcn value1 value2`; `comparisonFcn` takes two arguments and returns -1 if the first argument is less than the second, 0 if the arguments are equal, and 1 if the first argument is greater than the second.
240
+ ```res example
241
+ let clockCompare = (a, b) => compare(mod(a, 12), mod(b, 12))
197
242
198
- ```
199
- let clockCompare = fun a b -> compare (a mod 12) (b mod 12);;
200
- cmp (Some 3) (Some 15) clockCompare = 0;;
201
- cmp (Some 3) (Some 14) clockCompare = 1;;
202
- cmp (Some 2) (Some 15) clockCompare = -1;;
203
- cmp None (Some 15) clockCompare = -1;;
204
- cmp (Some 14) None clockCompare = 1;;
205
- cmp None None clockCompare = 0;;
243
+ open Belt.Option
244
+
245
+ cmp(Some(3), Some(15), clockCompare) /* 0 */
246
+
247
+ cmp(Some(3), Some(14), clockCompare) /* 1 */
248
+
249
+ cmp(Some(2), Some(15), clockCompare) /* (-1) */
250
+
251
+ cmp(None, Some(15), clockCompare) /* (-1) */
252
+
253
+ cmp(Some(14), None, clockCompare) /* 1 */
254
+
255
+ cmp(None, None, clockCompare) /* 0 */
206
256
```
207
257
*)
0 commit comments