File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments