@@ -518,6 +518,18 @@ module AsyncSeq =
518518 else return Nil
519519 | Nil -> return Nil }
520520
521+ /// Returns elements from the argument async sequence until the specified signal completes or
522+ /// the sequences completes.
523+ let rec takeUntil ( signal : Async < unit >) ( s : AsyncSeq < 'a >) : AsyncSeq < 'a > =
524+ Async.chooseBoth ( signal |> Async.map Choice1Of2) ( s |> Async.map Choice2Of2)
525+ |> Async.map ( fun ( first , second ) ->
526+ match first with
527+ | Choice1Of2 _ -> Nil
528+ | Choice2Of2 Nil -> Nil
529+ | Choice2Of2 ( Cons( a, tl)) ->
530+ let signal = second |> Async.map ( function Choice1Of2 x -> x | _ -> failwith " unexpected state" )
531+ Cons( a, takeUntil signal tl))
532+
521533 /// Skips elements from an asynchronous sequence while the specified
522534 /// predicate holds and then returns the rest of the sequence. The
523535 /// predicate is evaluated asynchronously.
@@ -530,10 +542,21 @@ module AsyncSeq =
530542 else return v
531543 | Nil -> return Nil }
532544
545+ /// Skips elements from an async sequence until the specified signal completes.
546+ let rec skipUntil ( signal : Async < unit >) ( s : AsyncSeq < 'a >) : AsyncSeq < 'a > =
547+ Async.chooseBoth ( signal |> Async.map Choice1Of2) ( s |> Async.map Choice2Of2)
548+ |> Async.bind ( fun ( first , second ) ->
549+ match first with
550+ | Choice1Of2 _ -> second |> Async.map ( function Choice2Of2 tl -> tl | _ -> failwith " unexpected state" )
551+ | Choice2Of2 Nil -> Nil |> async.Return
552+ | Choice2Of2 ( Cons(_, tl)) ->
553+ let signal = second |> Async.map ( function Choice1Of2 x -> x | _ -> failwith " unexpected state" )
554+ skipUntil signal tl)
555+
533556 /// Returns elements from an asynchronous sequence while the specified
534557 /// predicate holds. The predicate is evaluated synchronously.
535558 let rec takeWhile p ( input : AsyncSeq < 'T >) =
536- takeWhileAsync ( p >> async.Return) input
559+ takeWhileAsync ( p >> async.Return) input
537560
538561 /// Skips elements from an asynchronous sequence while the specified
539562 /// predicate holds and then returns the rest of the sequence. The
0 commit comments