-
Notifications
You must be signed in to change notification settings - Fork 786
/
option.fs
381 lines (319 loc) · 11 KB
/
option.fs
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace Microsoft.FSharp.Core
open Microsoft.FSharp.Core.Operators
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Option =
[<CompiledName("GetValue")>]
let get option =
match option with
| None -> invalidArg "option" (SR.GetString(SR.optionValueWasNone))
| Some x -> x
[<CompiledName("IsSome")>]
let inline isSome option =
match option with
| None -> false
| Some _ -> true
[<CompiledName("IsNone")>]
let inline isNone option =
match option with
| None -> true
| Some _ -> false
[<CompiledName("DefaultValue")>]
let inline defaultValue value option =
match option with
| None -> value
| Some v -> v
[<CompiledName("DefaultWith")>]
let inline defaultWith ([<InlineIfLambda>] defThunk) option =
match option with
| None -> defThunk ()
| Some v -> v
[<CompiledName("OrElse")>]
let inline orElse ifNone option =
match option with
| None -> ifNone
| Some _ -> option
[<CompiledName("OrElseWith")>]
let inline orElseWith ([<InlineIfLambda>] ifNoneThunk) option =
match option with
| None -> ifNoneThunk ()
| Some _ -> option
[<CompiledName("Count")>]
let inline count option =
match option with
| None -> 0
| Some _ -> 1
[<CompiledName("Fold")>]
let inline fold<'T, 'State> ([<InlineIfLambda>] folder) (state: 'State) (option: 'T option) =
match option with
| None -> state
| Some x -> folder state x
[<CompiledName("FoldBack")>]
let inline foldBack<'T, 'State> ([<InlineIfLambda>] folder) (option: option<'T>) (state: 'State) =
match option with
| None -> state
| Some x -> folder x state
[<CompiledName("Exists")>]
let inline exists ([<InlineIfLambda>] predicate) option =
match option with
| None -> false
| Some x -> predicate x
[<CompiledName("ForAll")>]
let inline forall ([<InlineIfLambda>] predicate) option =
match option with
| None -> true
| Some x -> predicate x
[<CompiledName("Contains")>]
let inline contains value option =
match option with
| None -> false
| Some v -> v = value
[<CompiledName("Iterate")>]
let inline iter ([<InlineIfLambda>] action) option =
match option with
| None -> ()
| Some x -> action x
[<CompiledName("Map")>]
let inline map ([<InlineIfLambda>] mapping) option =
match option with
| None -> None
| Some x -> Some(mapping x)
[<CompiledName("Map2")>]
let inline map2 ([<InlineIfLambda>] mapping) option1 option2 =
match option1, option2 with
| Some x, Some y -> Some(mapping x y)
| _ -> None
[<CompiledName("Map3")>]
let inline map3 ([<InlineIfLambda>] mapping) option1 option2 option3 =
match option1, option2, option3 with
| Some x, Some y, Some z -> Some(mapping x y z)
| _ -> None
[<CompiledName("Bind")>]
let inline bind ([<InlineIfLambda>] binder) option =
match option with
| None -> None
| Some x -> binder x
[<CompiledName("Flatten")>]
let inline flatten option =
match option with
| None -> None
| Some x -> x
[<CompiledName("Filter")>]
let inline filter ([<InlineIfLambda>] predicate) option =
match option with
| None -> None
| Some x -> if predicate x then Some x else None
[<CompiledName("ToArray")>]
let inline toArray option =
match option with
| None -> [||]
| Some x -> [| x |]
[<CompiledName("ToList")>]
let inline toList option =
match option with
| None -> []
| Some x -> [ x ]
[<CompiledName("ToNullable")>]
let inline toNullable option =
match option with
| None -> System.Nullable()
| Some v -> System.Nullable(v)
[<CompiledName("OfNullable")>]
let inline ofNullable (value: System.Nullable<'T>) =
if value.HasValue then
Some (value.GetValueOrDefault())
else
None
#if BUILDING_WITH_LKG || NO_NULLCHECKING_LIB_SUPPORT
[<CompiledName("OfObj")>]
let inline ofObj value =
match value with
| null -> None
| _ -> Some value
[<CompiledName("ToObj")>]
let inline toObj value =
match value with
| None -> null
| Some x -> x
#else
[<CompiledName("OfObj")>]
let inline ofObj (value: 'T | null) : 'T option when 'T: not struct and 'T : not null =
match value with
| null -> None
| _ -> Some value
[<CompiledName("ToObj")>]
let inline toObj (value: 'T option) : 'T | null when 'T: not struct (* and 'T : not null *) =
match value with
| None -> null
| Some x -> x
#endif
[<CompiledName("OfValueOption")>]
let inline ofValueOption (voption: 'T voption) =
match voption with
| ValueNone -> None
| ValueSome x -> Some x
[<CompiledName("ToValueOption")>]
let inline toValueOption (option: 'T option) =
match option with
| None -> ValueNone
| Some x -> ValueSome x
module ValueOption =
[<CompiledName("GetValue")>]
let get voption =
match voption with
| ValueNone -> invalidArg "option" (SR.GetString(SR.optionValueWasNone))
| ValueSome x -> x
[<CompiledName("IsSome")>]
let inline isSome voption =
match voption with
| ValueNone -> false
| ValueSome _ -> true
[<CompiledName("IsNone")>]
let inline isNone voption =
match voption with
| ValueNone -> true
| ValueSome _ -> false
[<CompiledName("DefaultValue")>]
let inline defaultValue value voption =
match voption with
| ValueNone -> value
| ValueSome v -> v
// We're deliberately not using InlineIfLambda, because benchmarked code ends up slightly slower at the time of writing (.NET 8 Preview)
[<CompiledName("DefaultWith")>]
let inline defaultWith defThunk voption =
match voption with
| ValueNone -> defThunk ()
| ValueSome v -> v
[<CompiledName("OrElse")>]
let inline orElse ifNone voption =
match voption with
| ValueNone -> ifNone
| ValueSome _ -> voption
[<CompiledName("OrElseWith")>]
let inline orElseWith ([<InlineIfLambda>] ifNoneThunk) voption =
match voption with
| ValueNone -> ifNoneThunk ()
| ValueSome _ -> voption
[<CompiledName("Count")>]
let inline count voption =
match voption with
| ValueNone -> 0
| ValueSome _ -> 1
[<CompiledName("Fold")>]
let inline fold<'T, 'State> ([<InlineIfLambda>] folder) (state: 'State) (voption: voption<'T>) =
match voption with
| ValueNone -> state
| ValueSome x -> folder state x
[<CompiledName("FoldBack")>]
let inline foldBack<'T, 'State> ([<InlineIfLambda>] folder) (voption: voption<'T>) (state: 'State) =
match voption with
| ValueNone -> state
| ValueSome x -> folder x state
[<CompiledName("Exists")>]
let inline exists ([<InlineIfLambda>] predicate) voption =
match voption with
| ValueNone -> false
| ValueSome x -> predicate x
[<CompiledName("ForAll")>]
let inline forall ([<InlineIfLambda>] predicate) voption =
match voption with
| ValueNone -> true
| ValueSome x -> predicate x
[<CompiledName("Contains")>]
let inline contains value voption =
match voption with
| ValueNone -> false
| ValueSome v -> v = value
[<CompiledName("Iterate")>]
let inline iter ([<InlineIfLambda>] action) voption =
match voption with
| ValueNone -> ()
| ValueSome x -> action x
[<CompiledName("Map")>]
let inline map ([<InlineIfLambda>] mapping) voption =
match voption with
| ValueNone -> ValueNone
| ValueSome x -> ValueSome(mapping x)
[<CompiledName("Map2")>]
let inline map2 ([<InlineIfLambda>] mapping) voption1 voption2 =
match voption1, voption2 with
| ValueSome x, ValueSome y -> ValueSome(mapping x y)
| _ -> ValueNone
[<CompiledName("Map3")>]
let inline map3 ([<InlineIfLambda>] mapping) voption1 voption2 voption3 =
match voption1, voption2, voption3 with
| ValueSome x, ValueSome y, ValueSome z -> ValueSome(mapping x y z)
| _ -> ValueNone
[<CompiledName("Bind")>]
let inline bind ([<InlineIfLambda>] binder) voption =
match voption with
| ValueNone -> ValueNone
| ValueSome x -> binder x
[<CompiledName("Flatten")>]
let inline flatten voption =
match voption with
| ValueNone -> ValueNone
| ValueSome x -> x
[<CompiledName("Filter")>]
let inline filter ([<InlineIfLambda>] predicate) voption =
match voption with
| ValueNone -> ValueNone
| ValueSome x ->
if predicate x then
ValueSome x
else
ValueNone
[<CompiledName("ToArray")>]
let inline toArray voption =
match voption with
| ValueNone -> [||]
| ValueSome x -> [| x |]
[<CompiledName("ToList")>]
let inline toList voption =
match voption with
| ValueNone -> []
| ValueSome x -> [ x ]
[<CompiledName("ToNullable")>]
let inline toNullable voption =
match voption with
| ValueNone -> System.Nullable()
| ValueSome v -> System.Nullable(v)
[<CompiledName("OfNullable")>]
let inline ofNullable (value: System.Nullable<'T>) =
if value.HasValue then
ValueSome (value.GetValueOrDefault())
else
ValueNone
#if BUILDING_WITH_LKG || NO_NULLCHECKING_LIB_SUPPORT
[<CompiledName("OfObj")>]
let inline ofObj value =
match value with
| null -> ValueNone
| _ -> ValueSome value
[<CompiledName("ToObj")>]
let inline toObj value =
match value with
| ValueNone -> null
| ValueSome x -> x
#else
[<CompiledName("OfObj")>]
let inline ofObj (value: 'T | null) : 'T voption when 'T: not struct and 'T : not null =
match value with
| null -> ValueNone
| _ -> ValueSome value
[<CompiledName("ToObj")>]
let inline toObj (value : 'T voption) : 'T | null when 'T: not struct (* and 'T : not null *) =
match value with
| ValueNone -> null
| ValueSome x -> x
#endif
[<CompiledName("OfOption")>]
let inline ofOption (option: 'T option) =
match option with
| None -> ValueNone
| Some x -> ValueSome x
[<CompiledName("ToOption")>]
let inline toOption (voption: 'T voption) =
match voption with
| ValueNone -> None
| ValueSome x -> Some x