-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement For on the built-in TaskBuilder #75
Comments
Thanks for this suggestion! You mean on I certainly see the usefulness of this addition. For the time being, you can do this, which may not be ideal. The let mySeq =
taskSeq {
for i in 0..9 do yield i
}
let x = task {
let! l = mySeq |> TaskSeq.toListAsync
for x in l do
let y = x + 1
printfn $"{y}"
} Do note that this can only ever work for executing side effects: the |
Here's the equivalent in the AsyncSeq library: https://github.com/fsprojects/FSharp.Control.AsyncSeq/blob/1023d8a203acb7dd3f395bf25223bbe8a79d9e80/src/FSharp.Control.AsyncSeq/AsyncSeq.fs#L1819 So yes we should an equivalent add to TaskBuilder |
This turns out to be a bit trickier than I thought. This has to do with the First I figured I could use member inline this.For(tasksq: IAsyncEnumerable<'T>, body: 'T -> TaskCode<'TOverall, unit>) : TaskCode<'TOverall, unit> =
TaskCode<'TOverall, unit>(fun sm ->
this
.Using(
tasksq.GetAsyncEnumerator(CancellationToken()),
(fun e -> this.While((fun () -> e.MoveNextAsync()), (fun sm -> (body e.Current).Invoke(&sm))))
)
.Invoke(&sm)) It's probably not so hard in the end, just need to give it another look. PS, for member x.For(tasksq: IAsyncEnumerable<'T>, action: 'T -> Async<unit>) =
tasksq
|> TaskSeq.iterAsync (action >> Async.StartAsTask)
|> Async.AwaitTask |
@Tarmil thanks for making this request. The changes have been made in #93 (and #99, with help from @TheAngryByrd) and are merged. They'll be part of the upcoming v0.3.0, which I'll publish later today. The solution uses resumable code for |
It would be useful to add an extension method
For
onTaskBuilder
to be able to dofor
loops overIAsyncEnumerable
in tasks.The text was updated successfully, but these errors were encountered: