File tree Expand file tree Collapse file tree
src/FSharp.Control.AsyncSeq
tests/FSharp.Control.AsyncSeq.Tests Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -306,14 +306,17 @@ module AsyncSeq =
306306 ///
307307 /// The aggregation function is asynchronous (and the input sequence will
308308 /// be asked for the next element after the processing of an element completes).
309- let rec scanAsync f ( state : 'TState ) ( input : AsyncSeq < 'T >) = asyncSeq {
310- let! v = input
311- match v with
312- | Nil -> ()
313- | Cons( h, t) ->
309+ let scanAsync f ( state : 'TState ) ( input : AsyncSeq < 'T >) =
310+ let rec go f state s = asyncSeq {
311+ let! v = s
312+ match v with
313+ | Nil -> ()
314+ | Cons( h, t) ->
314315 let! v = f state h
315316 yield v
316- yield ! t |> scanAsync f v }
317+ yield ! t |> go f v }
318+ Cons( state, go f state input) |> async.Return
319+
317320
318321 /// Iterates over the input sequence and calls the specified function for
319322 /// every value (to perform some side-effect asynchronously).
Original file line number Diff line number Diff line change @@ -153,7 +153,16 @@ let ``AsyncSeq.scanAsync``() =
153153 let f i a = i + a
154154 let z = 0
155155 let actual = ls |> AsyncSeq.ofSeq |> AsyncSeq.scanAsync ( fun i a -> f i a |> async.Return) z
156- let expected = ls |> Seq.scan f z |> Seq.skip 1 |> AsyncSeq.ofSeq
156+ let expected = ls |> List.scan f z |> AsyncSeq.ofSeq
157+ Assert.True( EQ expected actual)
158+
159+ [<Test>]
160+ let ``AsyncSeq.scanAsync on empty should emit initial state`` () =
161+ let ls = List.empty
162+ let f i a = i + a
163+ let z = 0
164+ let actual = ls |> AsyncSeq.ofSeq |> AsyncSeq.scanAsync ( fun i a -> f i a |> async.Return) z
165+ let expected = ls |> List.scan f z |> AsyncSeq.ofSeq
157166 Assert.True( EQ expected actual)
158167
159168
You can’t perform that action at this time.
0 commit comments