-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathValueTaskValueOption.fs
More file actions
88 lines (75 loc) · 3.35 KB
/
Copy pathValueTaskValueOption.fs
File metadata and controls
88 lines (75 loc) · 3.35 KB
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
namespace FsToolkit.ErrorHandling
open System.Threading.Tasks
open IcedTasks
[<RequireQualifiedAccess>]
module ValueTaskValueOption =
let inline map ([<InlineIfLambda>] f) (ar: ValueTask<_ voption>) =
valueTask {
let! opt = ar
return ValueOption.map f opt
}
let inline bind ([<InlineIfLambda>] f) (ar: ValueTask<_ voption>) =
valueTask {
let! opt = ar
match opt with
| ValueSome x -> return! f x
| ValueNone -> return ValueNone
}
let inline valueSome x = ValueTask<_ voption>(ValueSome x)
let inline apply f x =
bind (fun f' -> bind (fun x' -> valueSome (f' x')) x) f
let inline zip (x1: ValueTask<'a voption>) (x2: ValueTask<'b voption>) =
valueTask {
let! r1 = x1
let! r2 = x2
return ValueOption.zip r1 r2
}
/// <summary>
/// Returns result of running <paramref name="onValueSome"/> if it is <c>ValueSome</c>, otherwise returns result of running <paramref name="onValueNone"/>
/// </summary>
/// <param name="onValueSome">The function to run if <paramref name="input"/> is <c>ValueSome</c></param>
/// <param name="onValueNone">The function to run if <paramref name="input"/> is <c>ValueNone</c></param>
/// <param name="input">The input voption.</param>
/// <returns>
/// The result of running <paramref name="onValueSome"/> if the input is <c>ValueSome</c>, else returns result of running <paramref name="onValueNone"/>.
/// </returns>
let inline either
([<InlineIfLambda>] onValueSome: 'input -> ValueTask<'output>)
([<InlineIfLambda>] onValueNone: unit -> ValueTask<'output>)
(input: ValueTask<'input voption>)
: ValueTask<'output> =
valueTask {
let! opt = input
match opt with
| ValueSome v -> return! onValueSome v
| ValueNone -> return! onValueNone ()
}
/// <summary>
/// Gets the value of the voption if the voption is <c>ValueSome</c>, otherwise returns the specified default value.
/// </summary>
/// <param name="value">The specified default value.</param>
/// <param name="valueTaskValueOption">The input voption.</param>
/// <returns>
/// The voption if the voption is <c>ValueSome</c>, else the default value.
/// </returns>
let inline defaultValue (value: 'value) (valueTaskValueOption: ValueTask<'value voption>) =
valueTask {
let! opt = valueTaskValueOption
return ValueOption.defaultValue value opt
}
/// <summary>
/// Gets the value of the voption if the voption is <c>ValueSome</c>, otherwise evaluates <paramref name="defThunk"/> and returns the result.
/// </summary>
/// <param name="defThunk">A thunk that provides a default value when evaluated.</param>
/// <param name="valueTaskValueOption">The input voption.</param>
/// <returns>
/// The voption if the voption is <c>ValueSome</c>, else the result of evaluating <paramref name="defThunk"/>.
/// </returns>
let inline defaultWith
([<InlineIfLambda>] defThunk: unit -> 'value)
(valueTaskValueOption: ValueTask<'value voption>)
: ValueTask<'value> =
valueTask {
let! opt = valueTaskValueOption
return ValueOption.defaultWith defThunk opt
}