File tree Expand file tree Collapse file tree
tests/FSharpx.Async.Tests Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -477,6 +477,17 @@ module AsyncSeq =
477477 | Nil -> return Nil
478478 else return ! input }
479479
480+ /// Creates an async computation which iterates the AsyncSeq and collects the output into an array.
481+ let toArray ( input : AsyncSeq < 'T >) : Async < 'T []> =
482+ input
483+ |> fold ( fun ( arr :ResizeArray < _ >) a -> arr.Add( a) ; arr) ( new ResizeArray<_>())
484+ |> Async.map ( fun arr -> arr.ToArray())
485+
486+ /// Creates an async computation which iterates the AsyncSeq and collects the output into a list.
487+ let toList ( input : AsyncSeq < 'T >) : Async < 'T list > =
488+ input |> fold ( fun arr a -> a:: arr) []
489+
490+
480491[<AutoOpen>]
481492module AsyncSeqExtensions =
482493 /// Builds an asynchronou sequence using the computation builder syntax
Original file line number Diff line number Diff line change @@ -13,3 +13,33 @@ let ``skipping should return all elements after the first non-match``() =
1313 |> AsyncSeq.toBlockingSeq
1414 |> Seq.toList
1515 Assert.AreEqual( expected, result)
16+
17+
18+ [<Test>]
19+ let ``toArray should collect the results into an array`` () =
20+
21+ let s = asyncSeq {
22+ yield 1
23+ yield 2
24+ yield 3
25+ }
26+
27+ let a = s |> AsyncSeq.toArray |> Async.RunSynchronously |> Array.toList
28+
29+ Assert.True(([ 1 ; 2 ; 3 ] = a))
30+
31+ [<Test>]
32+ let ``toList should collect the results into an array`` () =
33+
34+ let s = asyncSeq {
35+ yield 1
36+ yield 2
37+ yield 3
38+ }
39+
40+ let a = s |> AsyncSeq.toList |> Async.RunSynchronously
41+
42+ Assert.True(([ 1 ; 2 ; 3 ] = a))
43+
44+
45+
You can’t perform that action at this time.
0 commit comments