Skip to content

Commit 4ed6ba5

Browse files
committed
Simplify takeWhile and takeWhileInclusive, easier to follow
1 parent 624e07b commit 4ed6ba5

File tree

2 files changed

+24
-32
lines changed

2 files changed

+24
-32
lines changed

src/FSharp.Control.TaskSeq/TaskSeq.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,10 @@ type TaskSeq private () =
299299
static member take count source = Internal.skipOrTake Take count source
300300
static member truncate count source = Internal.skipOrTake Truncate count source
301301

302-
static member takeWhile predicate source = Internal.takeWhile Exclusive (Predicate predicate) source
303-
static member takeWhileAsync predicate source = Internal.takeWhile Exclusive (PredicateAsync predicate) source
304-
static member takeWhileInclusive predicate source = Internal.takeWhile Inclusive (Predicate predicate) source
305-
static member takeWhileInclusiveAsync predicate source = Internal.takeWhile Inclusive (PredicateAsync predicate) source
302+
static member takeWhile predicate source = Internal.takeWhile false (Predicate predicate) source
303+
static member takeWhileAsync predicate source = Internal.takeWhile false (PredicateAsync predicate) source
304+
static member takeWhileInclusive predicate source = Internal.takeWhile true (Predicate predicate) source
305+
static member takeWhileInclusiveAsync predicate source = Internal.takeWhile true (PredicateAsync predicate) source
306306
static member skipWhile predicate source = Internal.skipWhile Exclusive (Predicate predicate) source
307307
static member skipWhileAsync predicate source = Internal.skipWhile Exclusive (PredicateAsync predicate) source
308308
static member skipWhileInclusive predicate source = Internal.skipWhile Inclusive (Predicate predicate) source

src/FSharp.Control.TaskSeq/TaskSeqInternal.fs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)