Skip to content

Commit

Permalink
Collapse takeWhile
Browse files Browse the repository at this point in the history
  • Loading branch information
bartelink committed Dec 22, 2023
1 parent c24fded commit ad2f66e
Showing 1 changed file with 23 additions and 44 deletions.
67 changes: 23 additions & 44 deletions src/FSharp.Control.TaskSeq/TaskSeqInternal.fs
Original file line number Diff line number Diff line change
Expand Up @@ -732,56 +732,35 @@ module internal TaskSeqInternal =
taskSeq {
use e = source.GetAsyncEnumerator CancellationToken.None
let! notEmpty = e.MoveNextAsync()
let mutable more = notEmpty
let mutable cont = notEmpty

match whileKind, predicate with
| Exclusive, Predicate predicate -> // takeWhile
while more do
let value = e.Current
more <- predicate value

if more then
// yield ONLY if predicate is true
yield value
let! hasMore = e.MoveNextAsync()
more <- hasMore

| Inclusive, Predicate predicate -> // takeWhileInclusive
while more do
let value = e.Current
more <- predicate value

// yield regardless of result of predicate
yield value

if more then
match predicate with
| Predicate predicate -> // takeWhile(Inclusive)?
while cont do
if predicate e.Current then
yield e.Current
let! hasMore = e.MoveNextAsync()
more <- hasMore
cont <- hasMore
else
match whileKind with
| Inclusive -> yield e.Current
| Exclusive -> ()

| Exclusive, PredicateAsync predicate -> // takeWhileAsync
while more do
let value = e.Current
let! passed = predicate value
more <- passed
cont <- false

if more then
// yield ONLY if predicate is true
yield value
| PredicateAsync predicate -> // takeWhile(Inclusive)?Async
while cont do
match! predicate e.Current with
| true ->
yield e.Current
let! hasMore = e.MoveNextAsync()
more <- hasMore

| Inclusive, PredicateAsync predicate -> // takeWhileInclusiveAsync
while more do
let value = e.Current
let! passed = predicate value
more <- passed

// yield regardless of predicate
yield value
cont <- hasMore
| false ->
match whileKind with
| Inclusive -> yield e.Current
| Exclusive -> ()

if more then
let! hasMore = e.MoveNextAsync()
more <- hasMore
cont <- false
}

let skipWhile whileKind predicate (source: TaskSeq<_>) =
Expand Down

0 comments on commit ad2f66e

Please sign in to comment.