@@ -5,135 +5,107 @@ open System
5
5
open Hopac
6
6
7
7
[<AutoOpen>]
8
- module JobOptionCE =
9
-
10
- type JobOptionBuilder () =
11
-
12
- member __.Return ( value : 'T ) : Job < _ option > =
13
- job.Return <| option.Return value
14
-
15
- member __.ReturnFrom
16
- ( jobResult : Job < _ option >)
17
- : Job < _ option > =
18
- jobResult
19
-
20
- member __.Zero () : Job < _ option > =
21
- job.Return <| option.Zero ()
22
-
23
- member __.Bind
24
- ( jobResult : Job < _ option >,
25
- binder : 'T -> Job < _ option >)
26
- : Job < _ option > =
27
- job {
28
- let! result = jobResult
29
- match result with
30
- | Some x -> return ! binder x
31
- | None -> return None
32
- }
33
-
34
- member this.Bind
35
- ( taskResult : unit -> Task < _ option >,
36
- binder : 'T -> Job < _ option >)
37
- : Job < _ option > =
38
- this.Bind( Job.fromTask taskResult, binder)
39
-
40
- member __.Delay
41
- ( generator : unit -> Job < _ option >)
42
- : Job < _ option > =
43
- Job.delay generator
44
-
45
- member this.Combine
46
- ( computation1 : Job < _ option >,
47
- computation2 : Job < _ option >)
48
- : Job < _ option > =
49
- this.Bind( computation1, fun () -> computation2)
50
-
51
- member __.TryWith
52
- ( computation : Job < _ option >,
53
- handler : System.Exception -> Job < _ option >)
54
- : Job < _ option > =
55
- Job.tryWith computation handler
56
-
57
- member __.TryWith
58
- ( computation : unit -> Job < _ option >,
59
- handler : System.Exception -> Job < _ option >)
60
- : Job < _ option > =
61
- Job.tryWithDelay computation handler
62
-
63
- member __.TryFinally
64
- ( computation : Job < _ option >,
65
- compensation : unit -> unit )
66
- : Job < _ option > =
67
- Job.tryFinallyFun computation compensation
68
-
69
- member __.TryFinally
70
- ( computation : unit -> Job < _ option >,
71
- compensation : unit -> unit )
72
- : Job < _ option > =
73
- Job.tryFinallyFunDelay computation compensation
74
-
75
- member __.Using
76
- ( resource : 'T when 'T :> IDisposable ,
77
- binder : 'T -> Job < _ option >)
78
- : Job < _ option > =
79
- job.Using( resource, binder)
80
-
81
- member this.While
82
- ( guard : unit -> bool , computation : Job < _ option >)
83
- : Job < _ option > =
84
- if not <| guard () then this.Zero ()
85
- else this.Bind( computation, fun () -> this.While ( guard, computation))
86
-
87
- member this.For
88
- ( sequence : #seq<'T> , binder : 'T -> Job < _ option >)
89
- : Job < _ option > =
90
- this.Using( sequence.GetEnumerator (), fun enum ->
91
- this.While( enum .MoveNext,
92
- this.Delay( fun () -> binder enum .Current)))
8
+ module JobOptionCE =
9
+
10
+ type JobOptionBuilder () =
11
+
12
+ member __.Return ( value : 'T ) : Job < _ option > = job.Return <| option.Return value
13
+
14
+ member __.ReturnFrom ( jobResult : Job < _ option >) : Job < _ option > = jobResult
15
+
16
+ member __.Zero () : Job < _ option > = job.Return <| option.Zero()
17
+
18
+ member __.Bind ( jobResult : Job < _ option >, binder : 'T -> Job < _ option >) : Job < _ option > =
19
+ job {
20
+ let! result = jobResult
21
+
22
+ match result with
23
+ | Some x -> return ! binder x
24
+ | None -> return None
25
+ }
26
+
27
+ member this.Bind ( taskResult : unit -> Task < _ option >, binder : 'T -> Job < _ option >) : Job < _ option > =
28
+ this.Bind( Job.fromTask taskResult, binder)
29
+
30
+ member __.Delay ( generator : unit -> Job < _ option >) : Job < _ option > = Job.delay generator
31
+
32
+ member this.Combine ( computation1 : Job < _ option >, computation2 : Job < _ option >) : Job < _ option > =
33
+ this.Bind( computation1, ( fun () -> computation2))
34
+
35
+ member __.TryWith ( computation : Job < _ option >, handler : System.Exception -> Job < _ option >) : Job < _ option > =
36
+ Job.tryWith computation handler
37
+
38
+ member __.TryWith
39
+ (
40
+ computation : unit -> Job < _ option >,
41
+ handler : System.Exception -> Job < _ option >
42
+ ) : Job < _ option > =
43
+ Job.tryWithDelay computation handler
44
+
45
+ member __.TryFinally ( computation : Job < _ option >, compensation : unit -> unit ) : Job < _ option > =
46
+ Job.tryFinallyFun computation compensation
47
+
48
+ member __.TryFinally ( computation : unit -> Job < _ option >, compensation : unit -> unit ) : Job < _ option > =
49
+ Job.tryFinallyFunDelay computation compensation
50
+
51
+ member __.Using ( resource : 'T :> IDisposable , binder : 'T -> Job < _ option >) : Job < _ option > =
52
+ job.Using( resource, binder)
53
+
54
+ member this.While ( guard : unit -> bool , computation : Job < _ option >) : Job < _ option > =
55
+ if not <| guard () then
56
+ this.Zero()
57
+ else
58
+ this.Bind( computation, ( fun () -> this.While( guard, computation)))
59
+
60
+ member this.For ( sequence : #seq<'T> , binder : 'T -> Job < _ option >) : Job < _ option > =
61
+ this.Using(
62
+ sequence.GetEnumerator(),
63
+ fun enum -> this.While( enum .MoveNext, this.Delay( fun () -> binder enum .Current))
64
+ )
93
65
94
66
/// <summary>
95
67
/// Method lets us transform data types into our internal representation. This is the identity method to recognize the self type.
96
68
///
97
69
/// See https://stackoverflow.com/questions/35286541/why-would-you-use-builder-source-in-a-custom-computation-expression-builder
98
70
/// </summary>
99
- member inline _.Source ( job : Job < _ option >) : Job < _ option > = job
71
+ member inline _.Source ( job : Job < _ option >) : Job < _ option > = job
100
72
101
73
/// <summary>
102
- /// Method lets us transform data types into our internal representation.
74
+ /// Method lets us transform data types into our internal representation.
103
75
/// </summary>
104
- member inline _.Source ( async : Async < _ option >) : Job < _ option > = async |> Job.fromAsync
76
+ member inline _.Source ( async : Async < _ option >) : Job < _ option > = async |> Job.fromAsync
105
77
106
78
/// <summary>
107
- /// Method lets us transform data types into our internal representation.
79
+ /// Method lets us transform data types into our internal representation.
108
80
/// </summary>
109
- member inline _.Source ( task : Task < _ option >) : Job < _ option > = task |> Job.awaitTask
81
+ member inline _.Source ( task : Task < _ option >) : Job < _ option > = task |> Job.awaitTask
110
82
111
- let jobOption = JobOptionBuilder()
83
+ let jobOption = JobOptionBuilder()
112
84
113
85
[<AutoOpen>]
114
86
// Having members as extensions gives them lower priority in
115
87
// overload resolution and allows skipping more type annotations.
116
88
module JobOptionCEExtensions =
117
89
118
- type JobOptionBuilder with
119
- /// <summary>
120
- /// Needed to allow `for..in` and `for..do` functionality
121
- /// </summary>
122
- member inline __.Source ( s : #seq<_> ) = s
123
-
124
- /// <summary>
125
- /// Method lets us transform data types into our internal representation.
126
- /// </summary>
127
- member inline __.Source ( r : 't option ) = Job.singleton r
128
- /// <summary>
129
- /// Method lets us transform data types into our internal representation.
130
- /// </summary>
131
- member inline __.Source ( a : Job < 't >) = a |> Job.map Some
132
- /// <summary>
133
- /// Method lets us transform data types into our internal representation.
134
- /// </summary>
135
- member inline __.Source ( a : Async < 't >) = a |> Job.fromAsync |> Job.map Some
136
- /// <summary>
137
- /// Method lets us transform data types into our internal representation.
138
- /// </summary>
139
- member inline __.Source ( a : Task < 't >) = a |> Job.awaitTask |> Job.map Some
90
+ type JobOptionBuilder with
91
+ /// <summary>
92
+ /// Needed to allow `for..in` and `for..do` functionality
93
+ /// </summary>
94
+ member inline __.Source ( s : #seq<_> ) = s
95
+
96
+ /// <summary>
97
+ /// Method lets us transform data types into our internal representation.
98
+ /// </summary>
99
+ member inline __.Source ( r : 't option ) = Job.singleton r
100
+ /// <summary>
101
+ /// Method lets us transform data types into our internal representation.
102
+ /// </summary>
103
+ member inline __.Source ( a : Job < 't >) = a |> Job.map Some
104
+ /// <summary>
105
+ /// Method lets us transform data types into our internal representation.
106
+ /// </summary>
107
+ member inline __.Source ( a : Async < 't >) = a |> Job.fromAsync |> Job.map Some
108
+ /// <summary>
109
+ /// Method lets us transform data types into our internal representation.
110
+ /// </summary>
111
+ member inline __.Source ( a : Task < 't >) = a |> Job.awaitTask |> Job.map Some
0 commit comments