Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Streamly/Internal/Data/Stream/Time.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import Control.Monad.IO.Class (MonadIO(..))
import Data.Heap (Entry(..))
import Data.Kind (Type)
import Data.Map (Map)
import Data.Maybe (isNothing)
import Data.Maybe (isJust, isNothing)
import Data.Proxy (Proxy(..))
import Streamly.Data.Fold (Fold)
import Streamly.Data.Array (Unbox)
Expand Down Expand Up @@ -190,7 +190,7 @@ interject f n xs = parListEagerFst [xs, periodic f n]
takeInterval :: MonadAsync m => Double -> Stream m a -> Stream m a
takeInterval d =
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.


-- | Take time interval @i@ seconds at the end of the stream.
Expand All @@ -214,7 +214,7 @@ takeLastInterval = undefined
dropInterval :: MonadAsync m => Double -> Stream m a -> Stream m a
dropInterval d =
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.


-- | Drop time interval @i@ seconds at the end of the stream.
Expand Down
61 changes: 58 additions & 3 deletions test/Streamly/Test/Data/Stream/Prelude/Time.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@

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

#ifdef DEVBUILD
import Control.Concurrent ( threadDelay )
#ifdef DEVBUILD
import Control.Monad ( when)
import Data.Int (Int64)
import Streamly.Internal.Data.Time.Clock (Clock(Monotonic), getTime)
import Streamly.Internal.Data.Time.Units
(AbsTime, NanoSecond64(..), toRelTime64, diffAbsTime64)
#endif

import qualified Streamly.Internal.Data.Fold as Fold
import qualified Streamly.Internal.Data.Stream as Stream
import qualified Streamly.Internal.Data.Stream.Prelude as Stream

#endif

import Test.Hspec as H

Expand Down Expand Up @@ -76,6 +76,53 @@ testDropInterval = do
checkTakeDropTime (Just t0, mt1)
#endif

-- The tests below do not measure time, they only use intervals that are either
-- much longer or much shorter than the time taken by the stream.

longInterval :: Double
longInterval = 100

shortInterval :: Double
shortInterval = 0.1

-- | An endless stream of consecutive numbers, one every millisecond.
slowEnumeration :: Stream.Stream IO Int
slowEnumeration =
Stream.mapM (\x -> threadDelay 1000 >> return x) (Stream.enumerateFrom 1)

takeIntervalAll :: IO [Int]
takeIntervalAll =
Stream.fold Fold.toList
$ Stream.takeInterval longInterval
$ Stream.fromList [1..10]

dropIntervalAll :: IO [Int]
dropIntervalAll =
Stream.fold Fold.toList
$ Stream.dropInterval longInterval
$ Stream.fromList [1..10]

-- The stream is endless, so this returns only if takeInterval ends it. The
-- elements taken must be the beginning of the stream.
takeIntervalPrefix :: IO Bool
takeIntervalPrefix = do
xs <-
Stream.fold Fold.toList
$ Stream.takeInterval shortInterval slowEnumeration
return $ not (null xs) && xs == [1..length xs]

-- The elements that remain must be a suffix of the stream, starting after the
-- first element.
dropIntervalSuffix :: IO Bool
dropIntervalSuffix = do
xs <-
Stream.fold Fold.toList
$ Stream.take 10
$ Stream.dropInterval shortInterval slowEnumeration
return $ case xs of
x : _ -> x > 1 && xs == [x .. x + 9]
[] -> False

moduleName :: String
moduleName = "Data.Stream.Time"

Expand All @@ -93,5 +140,13 @@ main = hspec
it "dropInterval" (testDropInterval `shouldReturn` True)
#endif
#endif
it "dummy" (return () `shouldReturn` ())
describe "Trimming" $ do
it "takeInterval takes all the elements within the interval"
(takeIntervalAll `shouldReturn` [1..10])
it "takeInterval ends the stream after the interval"
(takeIntervalPrefix `shouldReturn` True)
it "dropInterval drops all the elements within the interval"
(dropIntervalAll `shouldReturn` [])
it "dropInterval yields the elements after the interval"
(dropIntervalSuffix `shouldReturn` True)

Loading