@@ -796,44 +796,36 @@ module internal TaskSeqInternal =
796796
797797 }
798798
799- let takeWhile whileKind predicate ( source : TaskSeq < _ >) =
799+ let takeWhile isInclusive predicate ( source : TaskSeq < _ >) =
800800 checkNonNull ( nameof source) source
801801
802802 taskSeq {
803803 use e = source.GetAsyncEnumerator CancellationToken.None
804804 let! notEmpty = e.MoveNextAsync()
805- let mutable cont = notEmpty
806-
807- let inclusive =
808- match whileKind with
809- | Inclusive -> true
810- | Exclusive -> false
805+ let mutable hasMore = notEmpty
811806
812807 match predicate with
813- | Predicate predicate -> // takeWhile(Inclusive)?
814- while cont do
815- if predicate e.Current then
808+ | Predicate synchronousPredicate ->
809+ while hasMore && synchronousPredicate e.Current do
810+ yield e.Current
811+ let! cont = e.MoveNextAsync()
812+ hasMore <- cont
813+
814+ | PredicateAsync asyncPredicate ->
815+ let mutable predicateHolds = true
816+ while hasMore && predicateHolds do
817+ let! predicateIsTrue = asyncPredicate e.Current
818+ if predicateIsTrue then
816819 yield e.Current
817- let! hasMore = e.MoveNextAsync()
818- cont <- hasMore
819- else
820- if inclusive then
821- yield e.Current
822-
823- cont <- false
820+ let! cont = e.MoveNextAsync()
821+ hasMore <- cont
824822
825- | PredicateAsync predicate -> // takeWhile(Inclusive)?Async
826- while cont do
827- match ! predicate e.Current with
828- | true ->
829- yield e.Current
830- let! hasMore = e.MoveNextAsync()
831- cont <- hasMore
832- | false ->
833- if inclusive then
834- yield e.Current
823+ predicateHolds <- predicateIsTrue
835824
836- cont <- false
825+ // "inclusive" means: always return the item that we pulled, regardless of the result of applying the predicate
826+ // and only stop thereafter. The non-inclusive versions, in contrast, do not return the item under which the predicate is false.
827+ if hasMore && isInclusive then
828+ yield e.Current
837829 }
838830
839831 let skipWhile whileKind predicate ( source : TaskSeq < _ >) =
0 commit comments