Skip to content

Commit f147b98

Browse files
committed
move up AsyncSeq.unfoldAsync
1 parent f822f8f commit f147b98

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

src/FSharpx.Async/AsyncSeq.fs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,15 @@ module AsyncSeq =
3535
let singleton (v:'T) : AsyncSeq<'T> =
3636
async { return Cons(v, empty) }
3737

38+
/// Generates an async sequence using the specified generator function.
39+
let rec unfoldAsync (f:'State -> Async<('T * 'State) option>) (s:'State) : AsyncSeq<'T> =
40+
f s
41+
|> Async.map (function
42+
| Some (a,s) -> Cons(a, unfoldAsync f s)
43+
| None -> Nil)
44+
3845
/// Creates an async sequence which repeats the specified value indefinitely.
39-
let rec replicate (v:'T) : AsyncSeq<'T> =
46+
let rec replicate (v:'T) : AsyncSeq<'T> =
4047
Cons(v, async.Delay <| fun() -> replicate v) |> async.Return
4148

4249
/// Yields all elements of the first asynchronous sequence and then
@@ -284,15 +291,6 @@ module AsyncSeq =
284291
let filter f (input : AsyncSeq<'T>) =
285292
filterAsync (f >> async.Return) input
286293

287-
/// Generates an async sequence using the specified generator function.
288-
let rec unfoldAsync (f:'State -> Async<('T * 'State) option>) (s:'State) : AsyncSeq<'T> = asyncSeq {
289-
let! r = f s
290-
match r with
291-
| Some (a,s) ->
292-
yield a
293-
yield! unfoldAsync f s
294-
| None -> () }
295-
296294
// --------------------------------------------------------------------------
297295
// Converting from/to synchronous sequences or IObservables
298296

0 commit comments

Comments
 (0)