Skip to content

Seq(tryHeadTail): fix bug with non-rewinding streams - #276

Open
knocte wants to merge 2 commits into
fsprojects:masterfrom
knocte:fixSeqTryHeadTailBug
Open

knocte wants to merge 2 commits into
fsprojects:masterfrom
knocte:fixSeqTryHeadTailBug

Conversation

@knocte

@knocte knocte commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

No description provided.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The tail cannot be safely traversed repeatedly, and source enumerators can leak when the initial read throws or the tail is discarded.

3 open issues
What changed in this PR

This PR updates Seq.tryHeadTail to preserve remaining elements when reading non-rewinding streams in the core collections library.

Changes:

  • Uses one enumerator to read the head and lazily produce the tail.
  • Adds a regression test verifying that a byte-stream tail includes all remaining bytes.
File Description
tests/​FSharpx.Collections.Tests/​SeqTests.fs Adds byte-stream coverage for tryHeadTail.
src/​FSharpx.Collections/​Collections.fs Replaces separate head and tail enumeration with a shared enumerator.

Review effort: Balanced

Comment on lines +78 to +79
if e.MoveNext() then
let head = e.Current

while e.MoveNext() do
yield e.Current
}
yield e.Current
}

Some(head, tail)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants