Skip to content

Commit 163d61b

Browse files
committed
Fix TaskSeq.truncate reading one too many from the input
1 parent d28f5f5 commit 163d61b

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/FSharp.Control.TaskSeq/TaskSeqInternal.fs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ module internal TaskSeqInternal =
6767
let inline raiseCannotBeNegative name = invalidArg name "The value must be non-negative"
6868

6969
let inline raiseInsufficient () =
70+
// this is correct, it is NOT an InvalidOperationException (see Seq.fs in F# Core)
71+
// but instead, it's an ArgumentException... FWIW lol
7072
invalidArg "source" "The input task sequence was has an insufficient number of elements."
7173

7274
let inline raiseNotFound () =
@@ -136,15 +138,15 @@ module internal TaskSeqInternal =
136138
i <- i + 1 // update before moving: we are counting, not indexing
137139
go <- step
138140

139-
| Some(Predicate predicate) ->
141+
| Some (Predicate predicate) ->
140142
while go do
141143
if predicate e.Current then
142144
i <- i + 1
143145

144146
let! step = e.MoveNextAsync()
145147
go <- step
146148

147-
| Some(PredicateAsync predicate) ->
149+
| Some (PredicateAsync predicate) ->
148150
while go do
149151
match! predicate e.Current with
150152
| true -> i <- i + 1
@@ -213,7 +215,7 @@ module internal TaskSeqInternal =
213215
// multiple threads access the same item through the same enumerator (which is
214216
// bad practice, but hey, who're we to judge).
215217
if isNull value then
216-
value <- Lazy<_>.Create(fun () -> init i)
218+
value <- Lazy<_>.Create (fun () -> init i)
217219

218220
yield value.Force()
219221
value <- Unchecked.defaultof<_>
@@ -720,7 +722,7 @@ module internal TaskSeqInternal =
720722
yield e.Current
721723
pos <- pos + 1
722724
let! moveNext = e.MoveNextAsync()
723-
cont <- moveNext && pos <= count
725+
cont <- moveNext && pos < count
724726

725727
}
726728

0 commit comments

Comments
 (0)