Skip to content

Commit 1f87331

Browse files
committed
fix AsyncSeq.scanAsync #11
1 parent a785225 commit 1f87331

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

src/FSharp.Control.AsyncSeq/AsyncSeq.fs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff 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).

tests/FSharp.Control.AsyncSeq.Tests/AsyncSeqTests.fs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)