Skip to content

Commit 1e511be

Browse files
bartelinkabelbraaksma
authored andcommitted
refactor(skip+takeWhile): Make impls consistent
1 parent 641c284 commit 1e511be

File tree

1 file changed

+56
-105
lines changed

1 file changed

+56
-105
lines changed

src/FSharp.Control.TaskSeq/TaskSeqInternal.fs

+56-105
Original file line numberDiff line numberDiff line change
@@ -802,134 +802,85 @@ module internal TaskSeqInternal =
802802
taskSeq {
803803
use e = source.GetAsyncEnumerator CancellationToken.None
804804
let! notEmpty = e.MoveNextAsync()
805-
let mutable more = notEmpty
805+
let mutable cont = notEmpty
806806

807-
match whileKind, predicate with
808-
| Exclusive, Predicate predicate -> // takeWhile
809-
while more do
810-
let value = e.Current
811-
more <- predicate value
807+
let inclusive =
808+
match whileKind with
809+
| Inclusive -> true
810+
| Exclusive -> false
812811

813-
if more then
814-
// yield ONLY if predicate is true
815-
yield value
816-
let! hasMore = e.MoveNextAsync()
817-
more <- hasMore
818-
819-
| Inclusive, Predicate predicate -> // takeWhileInclusive
820-
while more do
821-
let value = e.Current
822-
more <- predicate value
823-
824-
// yield regardless of result of predicate
825-
yield value
826-
827-
if more then
812+
match predicate with
813+
| Predicate predicate -> // takeWhile(Inclusive)?
814+
while cont do
815+
if predicate e.Current then
816+
yield e.Current
828817
let! hasMore = e.MoveNextAsync()
829-
more <- hasMore
818+
cont <- hasMore
819+
else
820+
if inclusive then
821+
yield e.Current
830822

831-
| Exclusive, PredicateAsync predicate -> // takeWhileAsync
832-
while more do
833-
let value = e.Current
834-
let! passed = predicate value
835-
more <- passed
823+
cont <- false
836824

837-
if more then
838-
// yield ONLY if predicate is true
839-
yield value
825+
| PredicateAsync predicate -> // takeWhile(Inclusive)?Async
826+
while cont do
827+
match! predicate e.Current with
828+
| true ->
829+
yield e.Current
840830
let! hasMore = e.MoveNextAsync()
841-
more <- hasMore
842-
843-
| Inclusive, PredicateAsync predicate -> // takeWhileInclusiveAsync
844-
while more do
845-
let value = e.Current
846-
let! passed = predicate value
847-
more <- passed
848-
849-
// yield regardless of predicate
850-
yield value
831+
cont <- hasMore
832+
| false ->
833+
if inclusive then
834+
yield e.Current
851835

852-
if more then
853-
let! hasMore = e.MoveNextAsync()
854-
more <- hasMore
836+
cont <- false
855837
}
856838

857839
let skipWhile whileKind predicate (source: TaskSeq<_>) =
858840
checkNonNull (nameof source) source
859841

860842
taskSeq {
861843
use e = source.GetAsyncEnumerator CancellationToken.None
862-
let! moveFirst = e.MoveNextAsync()
863-
let mutable more = moveFirst
864-
865-
match whileKind, predicate with
866-
| Exclusive, Predicate predicate -> // skipWhile
867-
while more && predicate e.Current do
868-
let! hasMore = e.MoveNextAsync()
869-
more <- hasMore
870-
871-
if more then
872-
// yield the last one where the predicate was false
873-
// (this ensures we skip 0 or more)
874-
yield e.Current
875-
876-
while! e.MoveNextAsync() do // get the rest
877-
yield e.Current
878-
879-
| Inclusive, Predicate predicate -> // skipWhileInclusive
880-
while more && predicate e.Current do
881-
let! hasMore = e.MoveNextAsync()
882-
more <- hasMore
883-
884-
if more then
885-
// yield the rest (this ensures we skip 1 or more)
886-
while! e.MoveNextAsync() do
887-
yield e.Current
888-
889-
| Exclusive, PredicateAsync predicate -> // skipWhileAsync
890-
let mutable cont = true
891-
892-
if more then
893-
let! hasMore = predicate e.Current
894-
cont <- hasMore
895-
896-
while more && cont do
897-
let! moveNext = e.MoveNextAsync()
898-
899-
if moveNext then
900-
let! hasMore = predicate e.Current
901-
cont <- hasMore
902844

903-
more <- moveNext
904-
905-
if more then
906-
// yield the last one where the predicate was false
907-
// (this ensures we skip 0 or more)
908-
yield e.Current
845+
match! e.MoveNextAsync() with
846+
| false -> () // Nothing further to do, no matter what the rules are
847+
| true ->
909848

910-
while! e.MoveNextAsync() do // get the rest
911-
yield e.Current
849+
let exclusive =
850+
match whileKind with
851+
| Exclusive -> true
852+
| Inclusive -> false
912853

913-
| Inclusive, PredicateAsync predicate -> // skipWhileInclusiveAsync
914854
let mutable cont = true
915855

916-
if more then
917-
let! hasMore = predicate e.Current
918-
cont <- hasMore
856+
match predicate with
857+
| Predicate predicate -> // skipWhile(Inclusive)?
858+
while cont do
859+
if predicate e.Current then // spam -> skip
860+
let! hasAnother = e.MoveNextAsync()
861+
cont <- hasAnother
862+
else // Starting the ham
863+
if exclusive then
864+
yield e.Current // return the item as it does not meet the condition for skipping
919865

920-
while more && cont do
921-
let! moveNext = e.MoveNextAsync()
866+
while! e.MoveNextAsync() do // propagate the rest
867+
yield e.Current
922868

923-
if moveNext then
924-
let! hasMore = predicate e.Current
925-
cont <- hasMore
869+
cont <- false
870+
| PredicateAsync predicate -> // skipWhile(Inclusive)?Async
871+
while cont do
872+
match! predicate e.Current with
873+
| true ->
874+
let! hasAnother = e.MoveNextAsync()
875+
cont <- hasAnother
876+
| false -> // We're starting the ham
877+
if exclusive then
878+
yield e.Current // return the item as it does not meet the condition for skipping
926879

927-
more <- moveNext
880+
while! e.MoveNextAsync() do // propagate the rest
881+
yield e.Current
928882

929-
if more then
930-
// get the rest, this gives 1 or more semantics
931-
while! e.MoveNextAsync() do
932-
yield e.Current
883+
cont <- false
933884
}
934885

935886
// Consider turning using an F# version of this instead?

0 commit comments

Comments
 (0)