Repository navigation
Fix inverted predicates in takeInterval and dropInterval - #3272
harendra-kumar merged 1 commit into
Conversation
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
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
Fixes #3271.
Problem
takeIntervalyields nothing anddropIntervaldrops nothing. Both wrap thestream elements in
Justand interjectNothingas the timer tick, but thentest for
isNothing:takeWhile isNothingends the stream at the first element.dropWhile isNothingstops dropping at the first element.Change
Use
isJustin both, so thattakeIntervalkeeps elements until the firsttick and
dropIntervaldrops them until the first tick.Tests
The existing tests for these functions are only built under
DEVBUILD, as theymeasure 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.
takeIntervalwith a long interval returns the whole finite stream.takeIntervalwith a short interval ends an endless stream and returns a prefix of it.dropIntervalwith a long interval returns nothing from a finite stream.dropIntervalwith a short interval returns a contiguous suffix of an endless stream.cabal test streamly-tests:Data.Stream.Preludepasses for the new tests withGHC 9.12.4. The
DEVBUILDtiming tests were not run.🤖 Generated with the help of Claude Code