Skip to content

Commit eb68b52

Browse files
committed
AsyncSeq.concatSeq
1 parent 8bdbb33 commit eb68b52

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

src/FSharpx.Async/AsyncSeq.fs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,16 @@ module AsyncSeq =
496496
yield! unfoldAsync f s
497497
| None -> () }
498498

499+
/// Flattens an AsyncSeq of sequences.
500+
let rec concatSeq (input:AsyncSeq<#seq<'T>>) : AsyncSeq<'T> = asyncSeq {
501+
let! v = input
502+
match v with
503+
| Nil -> ()
504+
| Cons (hd, tl) ->
505+
for item in hd do
506+
yield item
507+
yield! concatSeq tl }
508+
499509
[<AutoOpen>]
500510
module AsyncSeqExtensions =
501511
/// Builds an asynchronou sequence using the computation builder syntax

tests/FSharpx.Async.Tests/AsyncSeqTests.fs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ let ``toList should collect the results into an array``() =
4343
Assert.True(([1;2;3] = a))
4444

4545

46+
[<Test>]
47+
let ``concatSeq should flatten a sequence``() =
48+
49+
let s = asyncSeq {
50+
yield [1;2]
51+
yield [3;4]
52+
}
53+
54+
let s =
55+
s
56+
|> AsyncSeq.concatSeq
57+
|> AsyncSeq.toList
58+
|> Async.RunSynchronously
59+
60+
Assert.True(([1;2;3;4] = s))
61+
62+
63+
4664
[<Test>]
4765
let ``unfoldAsync should generate a sequence``() =
4866

0 commit comments

Comments
 (0)