@@ -21,6 +21,13 @@ module Streamly.Internal.Data.Stream.Generate
21 , Stream.fromPure
21 , Stream.fromPure
22 , Stream.fromEffect
22 , Stream.fromEffect
24+ -- * Time Enumeration
24 -- * Cyclic Elements
31 -- * Cyclic Elements
@@ -40,7 +47,10 @@ import Control.Monad.IO.Class (MonadIO)
40import Data.Word (Word8)
47import Data.Word (Word8)
41import Foreign.Storable (Storable)
48import Foreign.Storable (Storable)
42import GHC.Exts (Addr#, Ptr (Ptr))
49import GHC.Exts (Addr#, Ptr (Ptr))
50+import Streamly.Internal.Data.Stream.Bottom
51+ (absTimesWith, relTimesWith, timesWith)
43import Streamly.Internal.Data.Stream.Type (Stream, fromStreamK, toStreamK)
52import Streamly.Internal.Data.Stream.Type (Stream, fromStreamK, toStreamK)
53+import Streamly.Internal.Data.Time.Units (AbsTime, RelTime64, addToAbsTime64)
44import Streamly.Internal.Data.Unfold.Type (Unfold)
54import Streamly.Internal.Data.Unfold.Type (Unfold)
46import qualified Streamly.Internal.Data.Stream.StreamD as D
56import qualified Streamly.Internal.Data.Stream.StreamD as D
@@ -49,12 +59,14 @@ import qualified Streamly.Internal.Data.Stream.Type as Stream
62+-- >>> import Control.Concurrent (threadDelay)
63+-- >>> import Data.Function (fix)
64+-- >>> import Prelude hiding (take)
52-- >>> import qualified Streamly.Data.Fold as Fold
65-- >>> import qualified Streamly.Data.Fold as Fold
53-- >>> import qualified Streamly.Data.Unfold as Unfold
66-- >>> import qualified Streamly.Data.Unfold as Unfold
54-- >>> import qualified Streamly.Internal.Data.Stream as Stream
67-- >>> import qualified Streamly.Internal.Data.Stream as Stream
55-- >>> import GHC.Exts (Ptr (Ptr))
68-- >>> import GHC.Exts (Ptr (Ptr))
58------------------------------------------------------------------------------
70------------------------------------------------------------------------------
59-- From Unfold
71-- From Unfold
60------------------------------------------------------------------------------
72------------------------------------------------------------------------------
@@ -72,6 +84,60 @@ import qualified Streamly.Internal.Data.Stream.Type as Stream
72unfold :: Monad m => Unfold m a b -> a -> Stream m b
84unfold :: Monad m => Unfold m a b -> a -> Stream m b
73unfold unf = Stream.fromStreamD . D.unfold unf
85unfold unf = Stream.fromStreamD . D.unfold unf
87+------------------------------------------------------------------------------
89+------------------------------------------------------------------------------
91+-- | @times@ returns a stream of time value tuples with clock of 10 ms
92+-- granularity. The first component of the tuple is an absolute time reference
93+-- (epoch) denoting the start of the stream and the second component is a time
94+-- relative to the reference.
96+-- >>> Stream.mapM_ (\x -> print x >> threadDelay 1000000) $ Stream.take 3 $ Stream.times
97+-- (AbsTime (TimeSpec {sec = ..., nsec = ...}),RelTime64 (NanoSecond64 ...))
98+-- (AbsTime (TimeSpec {sec = ..., nsec = ...}),RelTime64 (NanoSecond64 ...))
99+-- (AbsTime (TimeSpec {sec = ..., nsec = ...}),RelTime64 (NanoSecond64 ...))
101+-- Note: This API is not safe on 32-bit machines.
106+times :: MonadIO m => Stream m (AbsTime, RelTime64)
107+times = timesWith 0.01
109+-- | @absTimes@ returns a stream of absolute timestamps using a clock of 10 ms
112+-- >>> Stream.mapM_ print $ Stream.delayPre 1 $ Stream.take 3 $ Stream.absTimes
113+-- AbsTime (TimeSpec {sec = ..., nsec = ...})
114+-- AbsTime (TimeSpec {sec = ..., nsec = ...})
115+-- AbsTime (TimeSpec {sec = ..., nsec = ...})
117+-- Note: This API is not safe on 32-bit machines.
121+{-# INLINE absTimes #-}
122+absTimes :: MonadIO m => Stream m AbsTime
123+absTimes = fmap (uncurry addToAbsTime64) times
125+-- | @relTimes@ returns a stream of relative time values starting from 0,
126+-- using a clock of granularity 10 ms.
128+-- >>> Stream.mapM_ print $ Stream.delayPre 1 $ Stream.take 3 $ Stream.relTimes
129+-- RelTime64 (NanoSecond64 ...)
130+-- RelTime64 (NanoSecond64 ...)
131+-- RelTime64 (NanoSecond64 ...)
133+-- Note: This API is not safe on 32-bit machines.
137+{-# INLINE relTimes #-}
138+relTimes :: MonadIO m => Stream m RelTime64
139+relTimes = fmap snd times
75-- | We can define cyclic structures using @let@:
141-- | We can define cyclic structures using @let@:
77-- >>> let (a, b) = ([1, b], head a) in (a, b)
143-- >>> let (a, b) = ([1, b], head a) in (a, b)