-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathTask.fs
66 lines (52 loc) · 1.58 KB
/
Task.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
namespace FsToolkit.ErrorHandling
open System.Threading.Tasks
#if NETSTANDARD2_0
open FSharp.Control.Tasks.Affine
#endif
[<RequireQualifiedAccess>]
module Task =
let inline singleton value =
value
|> Task.FromResult
let inline bind ([<InlineIfLambda>] f: 'a -> Task<'b>) (x: Task<'a>) = task {
let! x = x
return! f x
}
let inline bindV ([<InlineIfLambda>] f: 'a -> Task<'b>) (x: ValueTask<'a>) = task {
let! x = x
return! f x
}
let inline apply f x =
bind (fun f' -> bind (fun x' -> singleton (f' x')) x) f
let inline map ([<InlineIfLambda>] f) x =
x
|> bind (
f
>> singleton
)
let inline mapV ([<InlineIfLambda>] f) x =
x
|> bindV (
f
>> singleton
)
let inline map2 ([<InlineIfLambda>] f) x y = (apply (apply (singleton f) x) y)
let inline map3 ([<InlineIfLambda>] f) x y z = apply (map2 f x y) z
/// Takes two tasks and returns a tuple of the pair
let zip (a1: Task<_>) (a2: Task<_>) = task {
let! r1 = a1
let! r2 = a2
return r1, r2
}
let ofUnit (t: Task) = task { return! t }
/// Creates a `Task` that attempts to execute the provided task,
/// returning `Choice1Of2` with the result if the task completes
/// without exceptions, or `Choice2Of2` with the exception if an
/// exception is thrown.
let catch (x: Task<_>) = task {
try
let! r = x
return Choice1Of2 r
with e ->
return Choice2Of2 e
}