Skip to content

Commit d98a5e1

Browse files
committed
Add documentation to Belt.Option.
1 parent 0dba4a6 commit d98a5e1

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

jscomp/others/belt_Option.mli

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,141 @@
2828
*)
2929

3030
val getExn : 'a option -> 'a
31+
(** [getExn optionalValue]
32+
Returns [value] if [optionalValue] is [Some value], otherwise raises [getExn]
33+
34+
@example {[
35+
getExn (Some 3) = 3;;
36+
getExn None (* Raises getExn error *)
37+
]}
38+
*)
39+
3140
val mapWithDefaultU : 'a option -> 'b -> ('a -> 'b [@bs]) -> 'b
41+
(** Uncurried version of [mapWithDefault] *)
42+
3243
val mapWithDefault : 'a option -> 'b -> ('a -> 'b) -> 'b
44+
(**
45+
[mapWithDefault optionValue default f]
46+
47+
If [optionValue] is [Some value], returns [f value]; otherwise returns [default]
48+
49+
@example {[
50+
mapWithDefault (Some 3) 0 (fun x -> x + 5) = 8;;
51+
mapWithDefault None 0 (fun x -> x + 5) = 0;;
52+
]}
53+
*)
54+
3355
val mapU : 'a option -> ('a -> 'b [@bs]) -> 'b option
56+
(** Uncurried version of [map] *)
57+
3458
val map : 'a option -> ('a -> 'b) -> 'b option
59+
(**
60+
[map optionValue f]
61+
62+
If [optionValue] is [Some value], returns [Some (f value)]; otherwise returns [None]
63+
64+
@example {[
65+
map (Some 3) (fun x -> x * x) = (Some 9);;
66+
map None (fun x -> x * x) = None;;
67+
]}
68+
*)
69+
3570
val flatMapU : 'a option -> ('a -> 'b option [@bs]) -> 'b option
71+
(** Uncurried version of [flatMap] *)
72+
3673
val flatMap : 'a option -> ('a -> 'b option) -> 'b option
74+
(**
75+
[flatMap optionValue f]
76+
77+
If [optionValue] is [Some value], returns [f value]; otherwise returns [None]
78+
The function [f] must have a return type of ['a option]
79+
80+
@example {[
81+
let f (x : float =
82+
if x >= 0.0 then
83+
Some (sqrt x)
84+
else
85+
None;;
86+
87+
flatMap (Some 4.0) f = Some 2.0;;
88+
flatMap (Some (-4.0)) f = None;;
89+
flatMap None f = None;;
90+
]}
91+
*)
92+
3793
val getWithDefault : 'a option -> 'a -> 'a
94+
(**
95+
[getWithDefault optionalValue default]
96+
97+
If [optionalValue] is [Some value], returns [value], otherwise [default]
98+
99+
@example {[
100+
getWithDefault (Some 1812) 1066 = 1812;;
101+
getWithDefault None 1066 = 1066;;
102+
]}
103+
*)
104+
38105
val isSome : 'a option -> bool
106+
(**
107+
Returns [true] if the argument is [Some value], [false] otherwise
108+
*)
109+
39110
val isNone : 'a option -> bool
111+
(**
112+
Returns [true] if the argument is [None], [false] otherwise
113+
*)
114+
40115
val eqU : 'a option -> 'b option -> ('a -> 'b -> bool [@bs]) -> bool
116+
(**
117+
Uncurried version of [eq]
118+
*)
119+
41120
val eq : 'a option -> 'b option -> ('a -> 'b -> bool) -> bool
121+
(**
122+
[eq optValue1 optvalue2 predicate]
123+
124+
Evaluates two optional values for equality with respect to a predicate function.
125+
126+
If both [optValue1] and [optValue2] are [None], returns [true].
127+
128+
If one of the arguments is [Some value] and the other is [None], returns [false]
129+
130+
If arguments are [Some value1] and [Some value2], returns the result of [predicate value1 value2];
131+
the [predicate] function must return a [bool]
132+
133+
@example {[
134+
let clockEqual = (fun a b -> a mod 12 = b mod 12);;
135+
eq (Some 3) (Some 15) clockEqual = true;;
136+
eq (Some 3) None clockEqual = false;;
137+
eq None (Some 3) clockEqual = false;;
138+
eq None None clockEqual = true;;
139+
]}
140+
*)
141+
42142
val cmpU : 'a option -> 'b option -> ('a -> 'b -> int [@bs]) -> int
143+
(** Uncurried version of [cmp] *)
144+
43145
val cmp : 'a option -> 'b option -> ('a -> 'b -> int) -> int
146+
(**
147+
[cmp optValue1 optvalue2 comparisonFcn]
148+
149+
Compares two optional values with respect to a comparison function
150+
151+
If both [optValue1] and [optValue2] are [None], returns 0.
152+
153+
If the first argument is [Some value1] and the second is [None], returns 1 (something is greater than nothing)
154+
155+
If the first argument is [None] and the second is [Some value2], returns -1 (nothing is less than something)
156+
157+
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.
158+
159+
@example {[
160+
let clockCompare = fun a b -> compare (a mod 12) (b mod 12);;
161+
cmp (Some 3) (Some 15) clockCompare = 0;;
162+
cmp (Some 3) (Some 14) clockCompare = 1;;
163+
cmp (Some 2) (Some 15) clockCompare = -1;;
164+
cmp None (Some 15) clockCompare = -1;;
165+
cmp (Some 14) None clockCompare = 1;;
166+
cmp None None clockCompare = 0;;
167+
]}
168+
*)

0 commit comments

Comments
 (0)