Skip to content

Commit 5bd3813

Browse files
committed
Renames, cleanup and overall fixup of task-for and async-for CE code
1 parent c5b24ec commit 5bd3813

File tree

4 files changed

+15
-85
lines changed

4 files changed

+15
-85
lines changed
Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,14 @@
11
namespace FSharp.Control
22

3-
open System.Collections.Generic
4-
open System.Threading
5-
open System.Threading.Tasks
6-
7-
#nowarn "57"
8-
#nowarn "1204"
9-
#nowarn "3513"
10-
3+
open FSharp.Control.TaskSeqBuilders
114

125
[<AutoOpen>]
136
module AsyncExtensions =
147

158
// Add asynchronous for loop to the 'async' computation builder
169
type Microsoft.FSharp.Control.AsyncBuilder with
1710

18-
member x.For(tasksq: IAsyncEnumerable<'T>, action: 'T -> Async<unit>) =
19-
tasksq
11+
member _.For(source: taskSeq<'T>, action: 'T -> Async<unit>) =
12+
source
2013
|> TaskSeq.iterAsync (action >> Async.StartAsTask)
2114
|> Async.AwaitTask
22-
23-
24-
// temp example
25-
let foo () = async {
26-
let mutable sum = 0
27-
28-
let xs = taskSeq {
29-
1
30-
2
31-
3
32-
}
33-
34-
for x in xs do
35-
sum <- sum + x
36-
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
namespace FSharp.Control
22

3-
#nowarn "1204"
4-
53
[<AutoOpen>]
64
module AsyncExtensions =
5+
open FSharp.Control.TaskSeqBuilders
76

87
type AsyncBuilder with
98

10-
member For: tasksq: System.Collections.Generic.IAsyncEnumerable<'T> * action: ('T -> Async<unit>) -> Async<unit>
9+
/// Iterate over all values of a taskSeq.
10+
member For: source: taskSeq<'T> * action: ('T -> Async<unit>) -> Async<unit>

src/FSharp.Control.TaskSeq/TaskExtensions.fs

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ open System.Collections.Generic
44
open System.Threading
55
open System.Threading.Tasks
66

7+
open Microsoft.FSharp.Core.CompilerServices
8+
open Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicOperators
9+
10+
open FSharp.Control.TaskSeqBuilders
11+
712
#nowarn "57"
813
#nowarn "1204"
914
#nowarn "3513"
1015

11-
1216
[<AutoOpen>]
1317
module TaskExtensions =
14-
open Microsoft.FSharp.Core.CompilerServices
15-
open Microsoft.FSharp.Core.CompilerServices.StateMachineHelpers
16-
open Microsoft.FSharp.Core.LanguagePrimitives.IntrinsicOperators
1718

1819
let rec WhileDynamic
1920
(
@@ -104,42 +105,11 @@ module TaskExtensions =
104105
false)
105106
)
106107

107-
member inline this.For(tasksq: IAsyncEnumerable<'T>, body: 'T -> TaskCode<_, unit>) : TaskCode<_, unit> =
108-
// tasksq
109-
// |> TaskSeq.iterAsync (body >> task.Run)
110-
// |> task.ReturnFrom
111-
112-
// task.ReturnFrom <|
113-
// task {
114-
// let mutable continueWhile = true
115-
// use e = tasksq.GetAsyncEnumerator()
116-
// while continueWhile do
117-
// let! next = e.MoveNextAsync()
118-
// if next then
119-
// do! task.Run(body e.Current)
120-
// else
121-
// continueWhile <- false
122-
// }
123-
108+
member inline this.For(source: taskSeq<'T>, body: 'T -> TaskCode<_, unit>) : TaskCode<_, unit> =
124109
TaskCode<'TOverall, unit>(fun sm ->
125-
126110
this
127111
.Using(
128-
tasksq.GetAsyncEnumerator(CancellationToken()),
112+
source.GetAsyncEnumerator(CancellationToken()),
129113
(fun e -> this.WhileAsync(e.MoveNextAsync, (fun sm -> (body e.Current).Invoke(&sm))))
130114
)
131115
.Invoke(&sm))
132-
133-
// temp example
134-
let foo () = task {
135-
let mutable sum = 0
136-
137-
let xs = taskSeq {
138-
1
139-
2
140-
3
141-
}
142-
143-
for x in xs do
144-
sum <- sum + x
145-
}

src/FSharp.Control.TaskSeq/TaskExtensions.fsi

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,8 @@ namespace FSharp.Control
44

55
[<AutoOpen>]
66
module TaskExtensions =
7-
8-
val WhileDynamic:
9-
sm: byref<TaskStateMachine<'Data>> *
10-
condition: (unit -> System.Threading.Tasks.ValueTask<bool>) *
11-
body: TaskCode<'Data, unit> ->
12-
bool
13-
14-
val WhileBodyDynamicAux:
15-
sm: byref<TaskStateMachine<'Data>> *
16-
condition: (unit -> System.Threading.Tasks.ValueTask<bool>) *
17-
body: TaskCode<'Data, unit> *
18-
rf: TaskResumptionFunc<'Data> ->
19-
bool
7+
open FSharp.Control.TaskSeqBuilders
208

219
type TaskBuilder with
2210

23-
member inline WhileAsync:
24-
condition: (unit -> System.Threading.Tasks.ValueTask<bool>) * body: TaskCode<'TOverall, unit> ->
25-
TaskCode<'TOverall, unit>
26-
27-
member inline For:
28-
tasksq: System.Collections.Generic.IAsyncEnumerable<'T> * body: ('T -> TaskCode<'TOverall, unit>) ->
29-
TaskCode<'TOverall, unit>
11+
member inline For: source: taskSeq<'T> * body: ('T -> TaskCode<'TOverall, unit>) -> TaskCode<'TOverall, unit>

0 commit comments

Comments
 (0)