Last active
March 3, 2022 11:50
-
-
Save akhansari/d88812b742aa6be1c35b4f46bd9f8532 to your computer and use it in GitHub Desktop.
Revisions
-
akhansari revised this gist
Mar 3, 2022 . 1 changed file with 28 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,10 @@ module AsyncSeq = open System.Collections.Generic open System.Threading.Tasks let cancelled (cancellationToken: CancellationToken) = Task.FromCanceled<bool> cancellationToken |> ValueTask<bool> let ofSeq (sq: Task<'T> seq) = { new IAsyncEnumerable<'T> with @@ -14,15 +18,18 @@ module AsyncSeq = enumerator.Dispose() ValueTask.CompletedTask member _.MoveNextAsync() = if cancellationToken.IsCancellationRequested then cancelled cancellationToken else task { if enumerator.MoveNext() then let! res = enumerator.Current current <- res return true else return false } |> ValueTask<bool> } } @@ -38,15 +45,18 @@ module AsyncSeq = enumerator.Dispose() ValueTask.CompletedTask member _.MoveNextAsync() = if cancellationToken.IsCancellationRequested then cancelled cancellationToken else task { if enumerator.MoveNext() then let! res = mapping enumerator.Current current <- res return true else return false } |> ValueTask<bool> } } -
akhansari revised this gist
Feb 16, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -56,7 +56,7 @@ module AsyncSeq = let! go = enumerator.MoveNextAsync() if go then do! action enumerator.Current return! iter enumerator } task { use enumerator = asyncSeq.GetAsyncEnumerator() -
akhansari renamed this gist
Feb 16, 2022 . 1 changed file with 14 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ module AsyncSeq = open System.Collections.Generic open System.Threading.Tasks @@ -48,4 +48,17 @@ module AsyncEnumerable = }, cancellationToken) |> ValueTask<bool> } } let iter action (asyncSeq: IAsyncEnumerable<'T>) = let rec iter (enumerator: IAsyncEnumerator<'T>) = task { let! go = enumerator.MoveNextAsync() if go then do! action enumerator.Current do! iter enumerator } task { use enumerator = asyncSeq.GetAsyncEnumerator() do! iter enumerator } -
akhansari revised this gist
Feb 15, 2022 . 1 changed file with 2 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,6 @@ module AsyncEnumerable = open System.Collections.Generic open System.Threading.Tasks let ofSeq (sq: Task<'T> seq) = { new IAsyncEnumerable<'T> with -
akhansari revised this gist
Feb 15, 2022 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ open System.Collections.Generic open System.Threading.Tasks -
akhansari created this gist
Feb 15, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,53 @@ open System open System.Collections.Generic open System.Threading.Tasks module AsyncEnumerable = let ofSeq (sq: Task<'T> seq) = { new IAsyncEnumerable<'T> with member _.GetAsyncEnumerator cancellationToken = let enumerator = sq.GetEnumerator() let mutable current = Unchecked.defaultof<_> { new IAsyncEnumerator<'T> with member _.Current = current member _.DisposeAsync() = enumerator.Dispose() ValueTask.CompletedTask member _.MoveNextAsync() = Task.Run<bool>(fun () -> task { if enumerator.MoveNext() then let! res = enumerator.Current current <- res return true else return false }, cancellationToken) |> ValueTask<bool> } } let ofSeqMap mapping (sq: 'T seq) = { new IAsyncEnumerable<'U> with member _.GetAsyncEnumerator cancellationToken = let enumerator = sq.GetEnumerator() let mutable current = Unchecked.defaultof<_> { new IAsyncEnumerator<'U> with member _.Current = current member _.DisposeAsync() = enumerator.Dispose() ValueTask.CompletedTask member _.MoveNextAsync() = Task.Run<bool>(fun () -> task { if enumerator.MoveNext() then let! res = mapping enumerator.Current current <- res return true else return false }, cancellationToken) |> ValueTask<bool> } }