Skip to content
Draft
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
4 changes: 4 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 4.9.0

* Added `AsyncSeq.transpose` β€” transposes a sequence of sequences so each element of the result is an array of the i-th elements of all inner sequences; all inner sequences must have the same length and the entire source is buffered, mirroring `Seq.transpose`.

### 4.8.0

* Added `AsyncSeq.mapFoldAsync` β€” maps each element using an asynchronous folder that also threads an accumulator state, returning both the array of results and the final state; mirrors `Seq.mapFold`.
Expand Down
11 changes: 11 additions & 0 deletions src/FSharp.Control.AsyncSeq/AsyncSeq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2035,6 +2035,17 @@
let! arr = toArrayAsync source
for i in arr.Length - 1 .. -1 .. 0 do
yield arr.[i] }

let transpose (source: AsyncSeq<#seq<'T>>) : AsyncSeq<'T[]> = asyncSeq {
let! rows = toArrayAsync source
if rows.Length > 0 then
let rowArrays = rows |> Array.map (fun r -> (r :> seq<'T>) |> Seq.toArray)
let colCount = rowArrays.[0].Length
for row in rowArrays do
if row.Length <> colCount then
invalidArg "source" "All inner sequences must have the same length."
for c in 0 .. colCount - 1 do
yield [| for row in rowArrays -> row.[c] |] }
#endif

#if !FABLE_COMPILER
Expand Down Expand Up @@ -2397,7 +2408,7 @@

[<CompilerMessage("The result of groupBy must be consumed with a parallel combinator such as AsyncSeq.mapAsyncParallel. Sequential consumption will deadlock because sub-sequence completion depends on other sub-sequences being consumed concurrently.", 9999)>]
let groupBy (p:'a -> 'k) (s:AsyncSeq<'a>) : AsyncSeq<'k * AsyncSeq<'a>> =
groupByAsync (p >> async.Return) s

Check warning on line 2411 in src/FSharp.Control.AsyncSeq/AsyncSeq.fs

View workflow job for this annotation

GitHub Actions / build

The result of groupByAsync must be consumed with a parallel combinator such as AsyncSeq.mapAsyncParallel. Sequential consumption will deadlock because sub-sequence completion depends on other sub-sequences being consumed concurrently.

Check warning on line 2411 in src/FSharp.Control.AsyncSeq/AsyncSeq.fs

View workflow job for this annotation

GitHub Actions / build

The result of groupByAsync must be consumed with a parallel combinator such as AsyncSeq.mapAsyncParallel. Sequential consumption will deadlock because sub-sequence completion depends on other sub-sequences being consumed concurrently.
#endif
#endif

Expand Down
6 changes: 6 additions & 0 deletions src/FSharp.Control.AsyncSeq/AsyncSeq.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,12 @@ module AsyncSeq =
/// sequence is buffered before yielding any elements, mirroring Seq.rev.
/// This function should not be used with large or infinite sequences.
val rev : source:AsyncSeq<'T> -> AsyncSeq<'T>

/// Transposes a sequence of sequences: each element of the result is an array
/// of the i-th elements of all inner sequences, mirroring Seq.transpose.
/// All inner sequences must have the same length; the entire source is buffered.
/// This function should not be used with large or infinite sequences.
val transpose : source:AsyncSeq<#seq<'T>> -> AsyncSeq<'T[]>
#endif

/// Interleaves two async sequences of the same type into a resulting sequence. The provided
Expand Down
41 changes: 41 additions & 0 deletions tests/FSharp.Control.AsyncSeq.Tests/AsyncSeqTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2000,7 +2000,7 @@
let actual =
ls
|> AsyncSeq.ofSeq
|> AsyncSeq.groupBy p

Check warning on line 2003 in tests/FSharp.Control.AsyncSeq.Tests/AsyncSeqTests.fs

View workflow job for this annotation

GitHub Actions / build

The result of groupBy must be consumed with a parallel combinator such as AsyncSeq.mapAsyncParallel. Sequential consumption will deadlock because sub-sequence completion depends on other sub-sequences being consumed concurrently.
|> AsyncSeq.mapAsyncParallel (snd >> AsyncSeq.toListAsync)
Assert.AreEqual(expected, actual)

Expand All @@ -2009,7 +2009,7 @@
let expected = asyncSeq { raise (exn("test")) }
let actual =
asyncSeq { raise (exn("test")) }
|> AsyncSeq.groupBy (fun i -> i % 3)

Check warning on line 2012 in tests/FSharp.Control.AsyncSeq.Tests/AsyncSeqTests.fs

View workflow job for this annotation

GitHub Actions / build

The result of groupBy must be consumed with a parallel combinator such as AsyncSeq.mapAsyncParallel. Sequential consumption will deadlock because sub-sequence completion depends on other sub-sequences being consumed concurrently.
|> AsyncSeq.mapAsyncParallel (snd >> AsyncSeq.toListAsync)
Assert.AreEqual(expected, actual)

Expand Down Expand Up @@ -3662,3 +3662,44 @@
|> AsyncSeq.toArrayAsync
|> Async.RunSynchronously |> ignore)
|> ignore

// ── AsyncSeq.transpose ────────────────────────────────────────────────────────

[<Test>]
let ``AsyncSeq.transpose transposes rows and columns`` () =
let source = asyncSeq { yield [1; 2; 3]; yield [4; 5; 6] }
let result = AsyncSeq.transpose source |> AsyncSeq.toArrayAsync |> Async.RunSynchronously
Assert.AreEqual([| [|1;4|]; [|2;5|]; [|3;6|] |], result)

[<Test>]
let ``AsyncSeq.transpose single row`` () =
let source = asyncSeq { yield [10; 20; 30] }
let result = AsyncSeq.transpose source |> AsyncSeq.toArrayAsync |> Async.RunSynchronously
Assert.AreEqual([| [|10|]; [|20|]; [|30|] |], result)

[<Test>]
let ``AsyncSeq.transpose empty source returns empty`` () =
let result = AsyncSeq.transpose AsyncSeq.empty<int[]> |> AsyncSeq.toArrayAsync |> Async.RunSynchronously
Assert.AreEqual([||], result)

[<Test>]
let ``AsyncSeq.transpose single column`` () =
let source = asyncSeq { yield [1]; yield [2]; yield [3] }
let result = AsyncSeq.transpose source |> AsyncSeq.toArrayAsync |> Async.RunSynchronously
Assert.AreEqual([| [|1;2;3|] |], result)

[<Test>]
let ``AsyncSeq.transpose square matrix`` () =
let source = asyncSeq { yield [1; 2]; yield [3; 4] }
let result = AsyncSeq.transpose source |> AsyncSeq.toArrayAsync |> Async.RunSynchronously
Assert.AreEqual([| [|1;3|]; [|2;4|] |], result)

[<Test>]
let ``AsyncSeq.transpose raises on mismatched row lengths`` () =
Assert.Throws<System.ArgumentException>(fun () ->
asyncSeq { yield [1; 2]; yield [3] }
|> AsyncSeq.transpose
|> AsyncSeq.toArrayAsync
|> Async.RunSynchronously
|> ignore)
|> ignore
Loading