-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathTaskResultCE.fs
137 lines (110 loc) · 4.33 KB
/
TaskResultCE.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
namespace FsToolkit.ErrorHandling.Benchmarks
open System
open BenchmarkDotNet
open BenchmarkDotNet.Attributes
open System.Threading.Tasks
open FsToolkit.ErrorHandling
module TaskResultCE =
type TaskResultCEInlinedLambdaBuilder() =
member inline _.Return(value: 'T) : TaskCode<Result<'T, 'TError>, _> = task.Return(Ok value)
member inline _.ReturnFrom
([<InlineIfLambda>] asyncResult: TaskCode<Result<'T, 'TError>, _>)
: TaskCode<Result<'T, 'TError>, _> =
asyncResult
member _.Zero() : TaskCode<Result<unit, 'TError>, _> =
task.Return
<| Ok()
member inline _.Bind
(
asyncResult: Task<Result<'T, 'TError>>,
[<InlineIfLambda>] binder: 'T -> TaskCode<Result<'U, 'TError>, _>
) : TaskCode<Result<'U, 'TError>, _> =
let inline binder' r =
match r with
| Ok x -> binder x
| Error x ->
task.Return
<| Error x
task.Bind(asyncResult, binder')
member inline _.Delay
([<InlineIfLambda>] generator: unit -> TaskCode<Result<'T, 'TError>, _>)
: TaskCode<Result<'T, 'TError>, _> =
task.Delay generator
member inline _.Run([<InlineIfLambda>] foo) = task.Run foo
/// <summary>
/// Method lets us transform data types into our internal representation. This is the identity method to recognize the self type.
///
/// See https://stackoverflow.com/questions/35286541/why-would-you-use-builder-source-in-a-custom-computation-expression-builder
/// </summary>
member inline _.Source(result: Task<Result<_, _>>) : Task<Result<_, _>> = result
/// <summary>
/// Method lets us transform data types into our internal representation. This is the identity method to recognize the self type.
///
/// See https://stackoverflow.com/questions/35286541/why-would-you-use-builder-source-in-a-custom-computation-expression-builder
/// </summary>
member inline _.Source
([<InlineIfLambda>] result: TaskCode<Result<_, _>, _>)
: TaskCode<Result<_, _>, _> =
result
[<AutoOpen>]
// Having members as extensions gives them lower priority in
// overload resolution and allows skipping more type annotations.
module TaskResultCEExtensions =
open TaskResultCE
type TaskResultCEInlinedLambdaBuilder with
/// <summary>
/// Needed to allow `for..in` and `for..do` functionality
/// </summary>
member inline _.Source(s: #seq<_>) = s
/// <summary>
/// Method lets us transform data types into our internal representation.
/// </summary>R
member inline _.Source(result: Result<_, _>) : Task<Result<_, _>> = Task.FromResult result
let rec fib n =
if n < 2L then
n
else
fib (n - 1L)
+ fib (n - 2L)
let rec afib (n, level) =
task {
if n < 0L then
return Error "No"
elif n < 2L then
return Ok n
elif n < level then
return Ok(fib n)
else
let n2a = afib (n - 2L, level)
let! n1 = afib (n - 1L, level)
let! n2 = n2a
match n1, n2 with
| Ok n1, Ok n2 -> return Ok(n2 + n1)
| Error e, _
| _, Error e -> return Error e
}
let taskResultInlinedIfLambda = TaskResultCEInlinedLambdaBuilder()
[<MemoryDiagnoser>]
type TaskResult_BindCEBenchmarks() =
[<Benchmark(Baseline = true)>]
member this.afib() = afib (10, 5)
[<Benchmark>]
member this.Result_Normal_Bind_CE() =
let action () =
taskResult {
let! a = Ok 10
let! b = Ok 5
let! c = afib (a, b)
return c
}
action ()
[<Benchmark>]
member this.Result_Alt_Inlined_Bind_CE() =
let action () =
taskResultInlinedIfLambda {
let! a = Ok 10
let! b = Ok 5
let! c = afib (a, b)
return c
}
action ()