Skip to content

Commit cf6bbc0

Browse files
Fix inverted predicates in takeInterval and dropInterval
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 #3271 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019RtUYCYCwddhe8t5hVfLiv
1 parent da441b8 commit cf6bbc0

2 files changed

Lines changed: 61 additions & 6 deletions

File tree

  • src/Streamly/Internal/Data/Stream
  • test/Streamly/Test/Data/Stream/Prelude

‎src/Streamly/Internal/Data/Stream/Time.hs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ import Control.Monad.IO.Class (MonadIO(..))
6262
import Data.Heap (Entry(..))
6363
import Data.Kind (Type)
6464
import Data.Map (Map)
65-
import Data.Maybe (isNothing)
65+
import Data.Maybe (isJust, isNothing)
6666
import Data.Proxy (Proxy(..))
6767
import Streamly.Data.Fold (Fold)
6868
import Streamly.Data.Array (Unbox)
@@ -190,7 +190,7 @@ interject f n xs = parListEagerFst [xs, periodic f n]
190190
takeInterval :: MonadAsync m => Double -> Stream m a -> Stream m a
191191
takeInterval d =
192192
Stream.catMaybes
193-
. Stream.takeWhile isNothing
193+
. Stream.takeWhile isJust
194194
. interject (return Nothing) d . fmap Just
195195

196196
-- | Take time interval @i@ seconds at the end of the stream.
@@ -214,7 +214,7 @@ takeLastInterval = undefined
214214
dropInterval :: MonadAsync m => Double -> Stream m a -> Stream m a
215215
dropInterval d =
216216
Stream.catMaybes
217-
. Stream.dropWhile isNothing
217+
. Stream.dropWhile isJust
218218
. interject (return Nothing) d . fmap Just
219219

220220
-- | Drop time interval @i@ seconds at the end of the stream.

‎test/Streamly/Test/Data/Stream/Prelude/Time.hs‎

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313

1414
module Streamly.Test.Data.Stream.Prelude.Time (main) where
1515

16-
#ifdef DEVBUILD
1716
import Control.Concurrent ( threadDelay )
17+
#ifdef DEVBUILD
1818
import Control.Monad ( when)
1919
import Data.Int (Int64)
2020
import Streamly.Internal.Data.Time.Clock (Clock(Monotonic), getTime)
2121
import Streamly.Internal.Data.Time.Units
2222
(AbsTime, NanoSecond64(..), toRelTime64, diffAbsTime64)
23+
#endif
2324

2425
import qualified Streamly.Internal.Data.Fold as Fold
2526
import qualified Streamly.Internal.Data.Stream as Stream
2627
import qualified Streamly.Internal.Data.Stream.Prelude as Stream
2728

28-
#endif
2929

3030
import Test.Hspec as H
3131

@@ -76,6 +76,53 @@ testDropInterval = do
7676
checkTakeDropTime (Just t0, mt1)
7777
#endif
7878

79+
-- The tests below do not measure time, they only use intervals that are either
80+
-- much longer or much shorter than the time taken by the stream.
81+
82+
longInterval :: Double
83+
longInterval = 100
84+
85+
shortInterval :: Double
86+
shortInterval = 0.1
87+
88+
-- | An endless stream of consecutive numbers, one every millisecond.
89+
slowEnumeration :: Stream.Stream IO Int
90+
slowEnumeration =
91+
Stream.mapM (\x -> threadDelay 1000 >> return x) (Stream.enumerateFrom 1)
92+
93+
takeIntervalAll :: IO [Int]
94+
takeIntervalAll =
95+
Stream.fold Fold.toList
96+
$ Stream.takeInterval longInterval
97+
$ Stream.fromList [1..10]
98+
99+
dropIntervalAll :: IO [Int]
100+
dropIntervalAll =
101+
Stream.fold Fold.toList
102+
$ Stream.dropInterval longInterval
103+
$ Stream.fromList [1..10]
104+
105+
-- The stream is endless, so this returns only if takeInterval ends it. The
106+
-- elements taken must be the beginning of the stream.
107+
takeIntervalPrefix :: IO Bool
108+
takeIntervalPrefix = do
109+
xs <-
110+
Stream.fold Fold.toList
111+
$ Stream.takeInterval shortInterval slowEnumeration
112+
return $ not (null xs) && xs == [1..length xs]
113+
114+
-- The elements that remain must be a suffix of the stream, starting after the
115+
-- first element.
116+
dropIntervalSuffix :: IO Bool
117+
dropIntervalSuffix = do
118+
xs <-
119+
Stream.fold Fold.toList
120+
$ Stream.take 10
121+
$ Stream.dropInterval shortInterval slowEnumeration
122+
return $ case xs of
123+
x : _ -> x > 1 && xs == [x .. x + 9]
124+
[] -> False
125+
79126
moduleName :: String
80127
moduleName = "Data.Stream.Time"
81128

@@ -93,5 +140,13 @@ main = hspec
93140
it "dropInterval" (testDropInterval `shouldReturn` True)
94141
#endif
95142
#endif
96-
it "dummy" (return () `shouldReturn` ())
143+
describe "Trimming" $ do
144+
it "takeInterval takes all the elements within the interval"
145+
(takeIntervalAll `shouldReturn` [1..10])
146+
it "takeInterval ends the stream after the interval"
147+
(takeIntervalPrefix `shouldReturn` True)
148+
it "dropInterval drops all the elements within the interval"
149+
(dropIntervalAll `shouldReturn` [])
150+
it "dropInterval yields the elements after the interval"
151+
(dropIntervalSuffix `shouldReturn` True)
97152

0 commit comments

Comments
 (0)