Skip to content

Commit d4efae0

Browse files
authored
Merge pull request #5424 from nkrkv/sync-belt-doc-headers-option
Sync belt doc headers for `Option`
2 parents becaf84 + 362d3a3 commit d4efae0

File tree

1 file changed

+129
-79
lines changed

1 file changed

+129
-79
lines changed

jscomp/others/belt_Option.mli

Lines changed: 129 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,20 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

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:
2631
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+
```
2839
*)
2940

3041

@@ -33,14 +44,12 @@ val keepU : 'a option -> ('a -> bool [@bs]) -> 'a option
3344

3445
val keep : 'a option -> ('a -> bool) -> 'a option
3546
(**
36-
`keep optionValue p`
47+
If `optionValue` is `Some(value)` and `p(value) = true`, it returns `Some(value)`; otherwise returns `None`
3748
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` */
4453
```
4554
*)
4655

@@ -49,45 +58,50 @@ val forEachU : 'a option -> ('a -> unit [@bs]) -> unit
4958

5059
val forEach : 'a option -> ('a -> unit) -> unit
5160
(**
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 `()`
5562
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 () */
5966
```
6067
*)
6168

6269
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.
6572
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+
```
7078
*)
7179

7280
external getUnsafe :
7381
'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(...)))`
7787
*)
7888

7989
val mapWithDefaultU : 'a option -> 'b -> ('a -> 'b [@bs]) -> 'b
8090
(** Uncurried version of `mapWithDefault` *)
8191

8292
val mapWithDefault : 'a option -> 'b -> ('a -> 'b) -> 'b
8393
(**
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)`.
8596
86-
If `optionValue` is `Some value`, returns `f value`; otherwise returns `default`
97+
If `optionValue` is `None`, the default is returned.
8798
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 */
91105
```
92106
*)
93107

@@ -96,13 +110,12 @@ val mapU : 'a option -> ('a -> 'b [@bs]) -> 'b option
96110

97111
val map : 'a option -> ('a -> 'b) -> 'b option
98112
(**
99-
`map optionValue f`
113+
If `optionValue` is `Some(value)` this returns `f(value)`, otherwise it returns `None`.
100114
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) */
102117
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 */
106119
```
107120
*)
108121

@@ -111,44 +124,66 @@ val flatMapU : 'a option -> ('a -> 'b option [@bs]) -> 'b option
111124

112125
val flatMap : 'a option -> ('a -> 'b option) -> 'b option
113126
(**
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>`.
115130
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+
}
118138
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 */
129144
```
130145
*)
131146

132147
val getWithDefault : 'a option -> 'a -> 'a
133148
(**
134-
`getWithDefault optionalValue default`
149+
If `optionalValue` is `Some(value)`, returns `value`, otherwise default.
135150
136-
If `optionalValue` is `Some value`, returns `value`, otherwise `default`
151+
```res example
152+
Belt.Option.getWithDefault(None, "Banana") /* Banana */
137153
154+
Belt.Option.getWithDefault(Some("Apple"), "Banana") /* Apple */
138155
```
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" */
141164
```
142165
*)
143166

144167
val isSome : 'a option -> bool
145168
(**
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+
```
147176
*)
148177

149178
val isNone : 'a option -> bool
150179
(**
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+
```
152187
*)
153188

154189
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
158193

159194
val eq : 'a option -> 'b option -> ('a -> 'b -> bool) -> bool
160195
(**
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`.
162200
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.
164203
165-
If both `optValue1` and `optValue2` are `None`, returns `true`.
204+
```res example
205+
let clockEqual = (a, b) => mod(a, 12) == mod(b, 12)
166206
167-
If one of the arguments is `Some value` and the other is `None`, returns `false`
207+
open Belt.Option
168208
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 */
171210
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 */
178216
```
179217
*)
180218

@@ -183,25 +221,37 @@ val cmpU : 'a option -> 'b option -> ('a -> 'b -> int [@bs]) -> int
183221

184222
val cmp : 'a option -> 'b option -> ('a -> 'b -> int) -> int
185223
(**
186-
`cmp optValue1 optvalue2 comparisonFcn`
224+
`cmp(optValue1, optValue2, comparisonFunction)` compares two optional values
225+
with respect to given `comparisonFunction`.
187226
188-
Compares two optional values with respect to a comparison function
227+
If both `optValue1` and `optValue2` are `None`, it returns `0`.
189228
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).
191231
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).
193234
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.
195239
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))
197242
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 */
206256
```
207257
*)

0 commit comments

Comments
 (0)