Skip to content

Fix inverted predicates in takeInterval and dropInterval - #3272

Merged
harendra-kumar merged 1 commit into
composewell:masterfrom
GuillaumedeVolpiano:fix-take-drop-interval
Nov 26, 2026
Merged

harendra-kumar merged 1 commit into
composewell:masterfrom
GuillaumedeVolpiano:fix-take-drop-interval

Conversation

@GuillaumedeVolpiano

@GuillaumedeVolpiano GuillaumedeVolpiano commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3271.

Problem

takeInterval yields nothing and dropInterval drops nothing. Both wrap the
stream elements in Just and interject Nothing as the timer tick, but then
test for isNothing:

  • takeWhile isNothing ends the stream at the first element.
  • dropWhile isNothing stops dropping at the first element.

Change

Use isJust in both, so that takeInterval keeps elements until the first
tick and dropInterval drops them until the first tick.

Tests

The existing tests for these functions are only built under DEVBUILD, as they
measure time. This adds four tests that are always built and do not measure
time: they use an interval either much longer or much shorter than the time
taken by the stream. They replace the dummy test.

  • takeInterval with a long interval returns the whole finite stream.
  • takeInterval with a short interval ends an endless stream and returns a prefix of it.
  • dropInterval with a long interval returns nothing from a finite stream.
  • dropInterval with a short interval returns a contiguous suffix of an endless stream.

cabal test streamly-tests:Data.Stream.Prelude passes for the new tests with
GHC 9.12.4. The DEVBUILD timing tests were not run.

🤖 Generated with the help of Claude Code

The stream elements are wrapped in Just and the timer ticks are Nothing,
so "takeWhile isNothing" ended the stream at the first element and
"dropWhile isNothing" dropped nothing. Use isJust in both.

Add tests that do not depend on time measurement, replacing the dummy
test.

Fixes composewell#3271

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_019RtUYCYCwddhe8t5hVfLiv

@harendra-kumar harendra-kumar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I have some suggested improvements but we can merge this and do that in another PR. This one will fix the bugs.

Stream.catMaybes
. Stream.dropWhile isNothing
. Stream.dropWhile isJust
. interject (return Nothing) d . fmap Just

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

interject generates an infinite stream, instead we can use a single element stream so that we do not keep generating Nothing and filtering them.

In fact we do not even need to merge concurrent streams here, we can just check the time using getTime before emitting an element, and use a hand-written state machine. That would be simplest and perhaps more efficient. It will drop the MonadAsync constraint as well.

If you are inclined to do this in this PR, I would accept the PR as is too, it fixes the bug.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion! I've opened #3274 builds on this one and addresses both your comments.

Stream.catMaybes
. Stream.takeWhile isNothing
. Stream.takeWhile isJust
. interject (return Nothing) d . fmap Just

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The current fix is good to go, but this combinator can be improved.

Instead of using interject we can do something similar to timeout from System.Timeout. We can fork a thread and use a one-shot threadDelay writing to an IORef and reading the IORef synchronously from the main stream.

@harendra-kumar
harendra-kumar merged commit 56baef4 into composewell:master Nov 26, 2026
15 of 17 checks passed
@GuillaumedeVolpiano
GuillaumedeVolpiano deleted the fix-take-drop-interval branch November 27, 2026 16:58
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.

takeInterval yields nothing, dropInterval drops nothing

2 participants