Skip to content

Commit 8bdbb33

Browse files
committed
AsyncSeq.unfoldAsync
1 parent eea942c commit 8bdbb33

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

src/FSharpx.Async/AssemblyInfo.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ open System.Reflection
44
[<assembly: AssemblyTitleAttribute("FSharpx.Async")>]
55
[<assembly: AssemblyProductAttribute("FSharpx.Async")>]
66
[<assembly: AssemblyDescriptionAttribute("Async extensions for F#")>]
7-
[<assembly: AssemblyVersionAttribute("1.9.8")>]
8-
[<assembly: AssemblyFileVersionAttribute("1.9.8")>]
7+
[<assembly: AssemblyVersionAttribute("1.9.9")>]
8+
[<assembly: AssemblyFileVersionAttribute("1.9.9")>]
99
do ()
1010

1111
module internal AssemblyVersionInformation =
12-
let [<Literal>] Version = "1.9.8"
12+
let [<Literal>] Version = "1.9.9"

src/FSharpx.Async/AsyncSeq.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,14 @@ module AsyncSeq =
487487
let toList (input:AsyncSeq<'T>) : Async<'T list> =
488488
input |> fold (fun arr a -> a::arr) []
489489

490+
/// Generates an async sequence using the specified generator function.
491+
let rec unfoldAsync (f:'State -> Async<('T * 'State) option>) (s:'State) : AsyncSeq<'T> = asyncSeq {
492+
let! r = f s
493+
match r with
494+
| Some (a,s) ->
495+
yield a
496+
yield! unfoldAsync f s
497+
| None -> () }
490498

491499
[<AutoOpen>]
492500
module AsyncSeqExtensions =

tests/FSharpx.Async.Tests/AsyncSeqTests.fs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ let ``toArray should collect the results into an array``() =
2828

2929
Assert.True(([1;2;3] = a))
3030

31+
3132
[<Test>]
3233
let ``toList should collect the results into an array``() =
3334

@@ -41,5 +42,19 @@ let ``toList should collect the results into an array``() =
4142

4243
Assert.True(([1;2;3] = a))
4344

44-
45+
46+
[<Test>]
47+
let ``unfoldAsync should generate a sequence``() =
48+
49+
let gen s =
50+
if s < 3 then (s,s + 1) |> Some |> async.Return
51+
else None |> async.Return
52+
53+
let s =
54+
AsyncSeq.unfoldAsync gen 0
55+
|> AsyncSeq.toList
56+
|> Async.RunSynchronously
57+
58+
Assert.True(([0;1;2] = s))
59+
4560

0 commit comments

Comments
 (0)