You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The first argument to `map()` is an uncurried function `f()` that takes a
133
+
plain value and returns a plain result. The second argument is an `option`
134
+
value. If it is of the form `Some(v)`, `map()` returns `Some(f(v))`; if it is
135
+
`None`, the return value is `None`, and function `f()` is not called.
136
+
137
+
```res example
138
+
let square = (. x) => x * x
139
+
Js.Option.map(square, Some(3)) == Some(9)
140
+
Js.Option.map(square, None) == None
141
+
```
142
+
*)
62
143
letmapfx=
63
144
match x with
64
145
|None -> None
65
146
|Somex -> Some (f x [@bs])
66
147
148
+
(**
149
+
The first argument to `getWithDefault()` is a default value. If the second
150
+
argument is of the form `Some(v)`, `getWithDefault()` returns `v`; if the
151
+
second argument is `None`, the return value is the default value.
152
+
153
+
```res example
154
+
Js.Option.getWithDefault(1066, Some(15)) == 15
155
+
Js.Option.getWithDefault(1066, None) == 1066
156
+
```
157
+
*)
67
158
letgetWithDefaultax=
68
159
match x with
69
160
|None -> a
70
161
|Somex -> x
71
162
163
+
(** **See:** [getWithDefault](#getWithDefault) *)
72
164
let default = getWithDefault
73
165
166
+
(**
167
+
The first argument to `filter()` is an uncurried function that takes a plain
168
+
value and returns a boolean. The second argument is an `option` value.
169
+
170
+
If the second argument is of the form `Some(v)` and `f(v)` is `true`,
171
+
the return value is `Some(v)`. Otherwise, the return value is `None`.
172
+
173
+
```res example
174
+
let isEven = (. x) => mod(x, 2) == 0
175
+
Js.Option.filter(isEven, Some(2)) == Some(2)
176
+
Js.Option.filter(isEven, Some(3)) == None
177
+
Js.Option.filter(isEven, None) == None
178
+
```
179
+
*)
74
180
letfilterfx=
75
181
match x with
76
182
|None -> None
@@ -80,6 +186,16 @@ let filter f x =
80
186
else
81
187
None
82
188
189
+
(**
190
+
The `firstSome()` function takes two `option` values; if the first is of the form `Some(v1)`, that is the return value. Otherwise, `firstSome()` returns the second value.
0 commit comments