Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/FSharp.Control.AsyncSeq/AsyncSeq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,27 @@ type AsyncSeqOp<'T> () =
[<AutoOpen>]
module AsyncSeqOp =

// Optimized enumerator for unfoldAsync with reduced allocations
[<Sealed>]
type OptimizedUnfoldEnumerator<'S, 'T> (f:'S -> Async<('T * 'S) option>, init:'S) =
let mutable currentState = init
let mutable disposed = false

interface IAsyncEnumerator<'T> with
member __.MoveNext () : Async<'T option> =
if disposed then async.Return None
else async {
let! result = f currentState
match result with
| None ->
return None
| Some (value, nextState) ->
currentState <- nextState
return Some value
}
member __.Dispose () =
disposed <- true

type UnfoldAsyncEnumerator<'S, 'T> (f:'S -> Async<('T * 'S) option>, init:'S) =
inherit AsyncSeqOp<'T> ()
override x.IterAsync g = async {
Expand Down Expand Up @@ -337,17 +358,7 @@ module AsyncSeqOp =
new UnfoldAsyncEnumerator<'S, 'U> (h, init) :> _
interface IAsyncEnumerable<'T> with
member __.GetEnumerator () =
let s = ref init
{ new IAsyncEnumerator<'T> with
member __.MoveNext () : Async<'T option> = async {
let! next = f !s
match next with
| None ->
return None
| Some (a,s') ->
s := s'
return Some a }
member __.Dispose () = () }
new OptimizedUnfoldEnumerator<'S, 'T>(f, init) :> IAsyncEnumerator<'T>



Expand Down