We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
I propose adding two functions:
AsyncSeq.toChannel : ChannelWriter<'a> -> AsyncSeq<'a> -> Async<unit> AsyncSeq.fromChannel : ChannelReader<'a> -> AsyncSeq<'a>
AsyncSeq.toChannel would be used to fill a ChannelWriter from an AsyncSeq
AsyncSeq.toChannel
ChannelWriter
AsyncSeq
AsyncSeq.fromChannel would be used to empty a ChannelReader into an AsyncSeq
AsyncSeq.fromChannel
ChannelReader
Here is an initial implementation:
#r "nuget: FSharp.Control.AsyncSeq, 2.0.23" open System.Threading.Channels open FSharp.Control [<RequireQualifiedAccess>] module AsyncSeq = let toChannel (writer : ChannelWriter<'a>) (xs : AsyncSeq<'a>) : Async<unit> = async { try for x in xs do if not (writer.TryWrite(x)) then let! ct = Async.CancellationToken do! writer.WriteAsync(x, ct).AsTask() |> Async.AwaitTask writer.Complete() with exn -> writer.Complete(error = exn) } let fromChannel (reader : ChannelReader<'a>) : AsyncSeq<'a> = asyncSeq { let mutable keepGoing = true while keepGoing do let mutable item = Unchecked.defaultof<'a> if reader.TryRead(&item) then yield item else let! ct = Async.CancellationToken let! hasMoreData = reader.WaitToReadAsync(ct).AsTask() |> Async.AwaitTask if not hasMoreData then keepGoing <- false }
// Demo async { let xs = [ 0 .. 9] |> AsyncSeq.ofSeq let channel = Channel.CreateBounded(4) do! Async.Parallel [| AsyncSeq.toChannel channel.Writer xs async { for x in AsyncSeq.fromChannel channel.Reader do printfn $"%i{x}" } |] |> Async.Ignore<unit array> } |> Async.RunSynchronously
Would a PR be accepted?
Thanks!
The text was updated successfully, but these errors were encountered:
Hi @njlr!
Yes, a PR with good testing would be accepted
You might need to update the engineering in this repository a bit - I haven't checked lately but I assume we should move to latest .NET etc.
Sorry, something went wrong.
Oh also make sure you bump the version numbers too (via CHANGELOG)
No branches or pull requests
I propose adding two functions:
AsyncSeq.toChannel
would be used to fill aChannelWriter
from anAsyncSeq
AsyncSeq.fromChannel
would be used to empty aChannelReader
into anAsyncSeq
Here is an initial implementation:
Would a PR be accepted?
Thanks!
The text was updated successfully, but these errors were encountered: