Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions src/FSharp.Control.AsyncSeq/AsyncSeq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,11 @@ module AsyncSeq =
let interleave (source1:AsyncSeq<'T>) (source2:AsyncSeq<'T>) : AsyncSeq<'T> =
interleaveChoice source1 source2 |> map (function Choice1Of2 x -> x | Choice2Of2 x -> x)

let interleaveMany (xs : #seq<AsyncSeq<'T>>) : AsyncSeq<'T> =
let mutable result = empty
for x in xs do
result <- interleave result x
result

let bufferByCount (bufferSize:int) (source:AsyncSeq<'T>) : AsyncSeq<'T[]> =
if (bufferSize < 1) then invalidArg "bufferSize" "must be positive"
Expand Down
6 changes: 5 additions & 1 deletion src/FSharp.Control.AsyncSeq/AsyncSeq.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,15 @@ module AsyncSeq =
/// large or infinite sequences.
val sortByDescending : projection:('T -> 'Key) -> source:AsyncSeq<'T> -> array<'T> when 'Key : comparison
#endif

/// Interleaves two async sequences of the same type into a resulting sequence. The provided
/// sequences are consumed in lock-step.
val interleave : source1:AsyncSeq<'T> -> source2:AsyncSeq<'T> -> AsyncSeq<'T>

/// Interleaves a sequence of async sequences into a resulting async sequence. The provided
/// sequences are consumed in lock-step.
val interleaveMany : source:#seq<AsyncSeq<'T>> -> AsyncSeq<'T>

/// Interleaves two async sequences into a resulting sequence. The provided
/// sequences are consumed in lock-step.
val interleaveChoice : source1:AsyncSeq<'T1> -> source2:AsyncSeq<'T2> -> AsyncSeq<Choice<'T1,'T2>>
Expand Down
19 changes: 19 additions & 0 deletions tests/FSharp.Control.AsyncSeq.Tests/AsyncSeqTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,25 @@ let ``AsyncSeq.interleave first empty``() =
let merged = AsyncSeq.interleave s1 s2 |> AsyncSeq.toListSynchronously
Assert.True([1 ; 2 ; 3] = merged)

[<Test>]
let ``AsyncSeq.interleaveMany empty``() =
let merged = AsyncSeq.interleaveMany [] |> AsyncSeq.toListSynchronously
Assert.True(List.isEmpty merged)

[<Test>]
let ``AsyncSeq.interleaveMany 1``() =
let s1 = AsyncSeq.ofSeq ["a";"b";"c"]
let merged = AsyncSeq.interleaveMany [s1] |> AsyncSeq.toListSynchronously
Assert.True(["a" ; "b" ; "c" ] = merged)

[<Test>]
let ``AsyncSeq.interleaveMany 3``() =
let s1 = AsyncSeq.ofSeq ["a";"b"]
let s2 = AsyncSeq.ofSeq ["i";"j";"k";"l"]
let s3 = AsyncSeq.ofSeq ["x";"y";"z"]
let merged = AsyncSeq.interleaveMany [s1;s2;s3] |> AsyncSeq.toListSynchronously
Assert.True(["a"; "x"; "i"; "y"; "b"; "z"; "j"; "k"; "l"] = merged)


[<Test>]
let ``AsyncSeq.bufferByCount``() =
Expand Down