-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathCore__Option.resi
290 lines (223 loc) · 7.59 KB
/
Core__Option.resi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* Copyright (C) 2017 Authors of ReScript
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/***
We represent the existence and nonexistence of a value by wrapping it with
the `option` type. In order to make it a bit more convenient to work with
option-types, we provide utility-functions for it.
The `option` type is a part of the ReScript standard library which is defined
like this:
```rescript
type option<'a> = None | Some('a)
```
```rescript
let someString: option<string> = Some("hello")
```
*/
/**
`flat(value)` flattens a nested `option` value to a single level.
## Examples
```rescript
Option.flat(Some(Some(10))) // Some(10)
Option.flat(Some(None)) // None
```
*/
let flat: option<option<'a>> => option<'a>
/**
`filter(opt, f)` applies `f` to `opt`, if `f` returns `true`, then it returns `Some(value)`, otherwise returns `None`.
## Examples
```rescript
Option.filter(Some(10), x => x > 5) // Some(10)
Option.filter(Some(4), x => x > 5) // None
Option.filter(None, x => x > 5) // None
```
*/
let filter: (option<'a>, 'a => bool) => option<'a>
/**
`forEach(opt, f)` call `f` on `opt`. if `opt` is `Some(value)`, then if calls
`f`, otherwise returns `unit`.
## Examples
```rescript
Option.forEach(Some("thing"), x => Console.log(x)) // logs "thing"
Option.forEach(None, x => Console.log(x)) // returns ()
```
*/
let forEach: (option<'a>, 'a => unit) => unit
/**
`getExn(opt)` returns `value` if `opt` is `Some(value)`, raises `Not_found` if `None`.
```rescript
Option.getExn(Some(3)) // 3
Option.getExn(None) /* Raises an Error */
```
## Exceptions
- Raises `Not_found` if `opt` is `None`
*/
let getExn: option<'a> => 'a
/**
`expect(opt, message)` returns `value` if `Some(value)`, raises a `Failure` expection with the given message if `None`.
```rescript
Option.expect(Some(3), "should not be None") // 3
Option.expect(None, "should not be None") // Raises `Failure("should not be None")`
```
## Exceptions
- Panics if `opt` is `None`
*/
let expect: (option<'a>, string) => 'a
/**
`getUnsafe(value)` returns `value`.
## Examples
```rescript
Option.getUnsafe(Some(3)) == 3
Option.getUnsafe(None) // Raises an error
```
## Important
This is an unsafe operation, it assumes `value` is neither `None`, `Some(None))`, `Some(Some(None))` etc.
*/
external getUnsafe: option<'a> => 'a = "%identity"
/**
`mapWithDefault(opt, default, f)` returns `f(value)` if `opt` is `Some(value)`, otherwise `default`.
## Examples
```rescript
let someValue = Some(3)
someValue->Option.mapWithDefault(0, x => x + 5) // 8
let noneValue = None
noneValue->Option.mapWithDefault(0, x => x + 5) // 0
```
*/
let mapWithDefault: (option<'a>, 'b, 'a => 'b) => 'b
/**
`map(opt, f)` returns `Some(f(value))` if `opt` is `Some(value)`, otherwise `None`.
## Examples
```rescript
Option.map(Some(3), x => x * x) // Some(9)
Option.map(None, x => x * x) // None
```
*/
let map: (option<'a>, 'a => 'b) => option<'b>
/**
`flatMap(opt, f)` returns `f(value)` if `opt` is `Some(value)`, otherwise `None`.
## Examples
```rescript
let addIfAboveOne = value =>
if (value > 1) {
Some(value + 1)
} else {
None
}
Option.flatMap(Some(2), addIfAboveOne) // Some(3)
Option.flatMap(Some(-4), addIfAboveOne) // None
Option.flatMap(None, addIfAboveOne) // None
```
*/
let flatMap: (option<'a>, 'a => option<'b>) => option<'b>
/**
`getWithDefault(opt, default)` returns `value` if `opt` is `Some(value)`, otherwise `default`.
## Examples
```rescript
Option.getWithDefault(None, "Banana") // Banana
Option.getWithDefault(Some("Apple"), "Banana") // Apple
let greet = (firstName: option<string>) =>
"Greetings " ++ firstName->Option.getWithDefault("Anonymous")
Some("Jane")->greet // "Greetings Jane"
None->greet // "Greetings Anonymous"
```
*/
let getWithDefault: (option<'a>, 'a) => 'a
/**
`or(opt1, opt2)` returns `opt2` if `opt1` is `None`, otherwise `opt1`.
## Examples
```rescript
Option.or(Some(1812), Some(1066)) == Some(1812)
Option.or(None, Some(1066) == Some(1066)
Option.or(None, None) == None
```
*/
let or: (option<'a>, option<'a>) => option<'a>
/**
`orElse(opt1, opt2)` returns `opt2` if `opt1` is `None`, otherwise `opt1`.
## Examples
```rescript
Option.orElse(Some(1812), Some(1066)) == Some(1812)
Option.orElse(None, Some(1066) == Some(1066)
Option.orElse(None, None) == None
```
*/
@deprecated("Use `or` instead")
let orElse: (option<'a>, option<'a>) => option<'a>
/**
`isSome(opt)` returns `true` if `opt` is `Some(value)`, otherwise returns `false`.
## Examples
```rescript
Option.isSome(None) // false
Option.isSome(Some(1)) // true
```
*/
let isSome: option<'a> => bool
/**
`isNone(opt)` returns `true` if `opt` is `None`, false otherwise.
## Examples
```rescript
Option.isNone(None) // true
Option.isNone(Some(1)) // false
```
*/
let isNone: option<'a> => bool
/**
`eq(opt1, opt2, f)` evaluates two optional values for equality with respect to a predicate function `f`. If both `opt1` and `opt2` are `None`, returns `true`.
If one of the arguments is `Some(value)` and the other is `None`, returns
`false`.
If arguments are `Some(value1)` and `Some(value2)`, returns the result of
`f(value1, value2)`, the predicate function `f` must return a bool.
## Examples
```rescript
let clockEqual = (a, b) => mod(a, 12) == mod(b, 12)
open Option
eq(Some(3), Some(15), clockEqual) // true
eq(Some(3), None, clockEqual) // false
eq(None, Some(3), clockEqual) // false
eq(None, None, clockEqual) // true
```
*/
let eq: (option<'a>, option<'b>, ('a, 'b) => bool) => bool
/**
`cmp(opt1, opt2, f)` compares two optional values with respect to given `f`.
If both `opt1` and `opt2` are `None`, it returns `0`. If the first argument is `Some(value1)` and the second is `None`, returns `1` (something is greater than nothing).
If the first argument is `None` and the second is `Some(value2)`, returns `-1`
(nothing is less than something).
If the arguments are `Some(value1)` and `Some(value2)`, returns the result of
`f(value1, value2)`, `f` 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.
## Examples
```rescript
let clockCompare = (a, b) => compare(mod(a, 12), mod(b, 12))
open Option
cmp(Some(3), Some(15), clockCompare) // 0
cmp(Some(3), Some(14), clockCompare) // 1
cmp(Some(2), Some(15), clockCompare) // (-1)
cmp(None, Some(15), clockCompare) // (-1)
cmp(Some(14), None, clockCompare) // 1
cmp(None, None, clockCompare) // 0
```
*/
let cmp: (option<'a>, option<'b>, ('a, 'b) => int) => int