on master help sign in
Add Rewrite rule to fromIndicesM for Serial streams.
7758b11Pranay Sashank
src/Streamly/Prelude.hs+10−6
@@ -680,7 +680,7 @@ yieldM = K.yieldM
680-- @since 0.6.0
680-- @since 0.6.0
681{-# INLINE fromIndices #-}
681{-# INLINE fromIndices #-}
682fromIndices :: (IsStream t, Monad m) => (Int -> a) -> t m a
682fromIndices :: (IsStream t, Monad m) => (Int -> a) -> t m a
683−fromIndices = fromStreamD . D.fromIndices
683+fromIndices = fromStreamS . S.fromIndices
684
684
685--
685--
686-- |
686-- |
@@ -691,13 +691,17 @@ fromIndices = fromStreamD . D.fromIndices
691-- Generate an infinite stream, whose values are the output of a monadic
691-- Generate an infinite stream, whose values are the output of a monadic
692-- function @f@ applied on the corresponding index. Index starts at 0.
692-- function @f@ applied on the corresponding index. Index starts at 0.
693--
693--
694+-- /Concurrent/
695+--
694-- @since 0.6.0
696-- @since 0.6.0
695−{-# INLINE fromIndicesM #-}
697+{-# INLINE_EARLY fromIndicesM #-}
696fromIndicesM :: (IsStream t, MonadAsync m) => (Int -> m a) -> t m a
698fromIndicesM :: (IsStream t, MonadAsync m) => (Int -> m a) -> t m a
697−fromIndicesM gen = go 0
699+fromIndicesM = K.fromIndicesM
698− where
700+
699− go i = K.mkStream $ \st stp sng yld -> do
701+{-# RULES "fromIndicesM serial" fromIndicesM = fromIndicesMSerial #-}
700− K.foldStreamShared st stp sng yld (gen i |: go (i + 1))
702+{-# INLINE fromIndicesMSerial #-}
703+fromIndicesMSerial :: MonadAsync m => (Int -> m a) -> SerialT m a
704+fromIndicesMSerial = fromStreamS . S.fromIndicesM
701
705
702-- |
706-- |
703-- @
707-- @
src/Streamly/Streams/StreamK.hs+15
@@ -63,6 +63,8 @@ module Streamly.Streams.StreamK
63 , repeat
63 , repeat
64 , replicate
64 , replicate
65 , replicateM
65 , replicateM
66+ , fromIndices
67+ , fromIndicesM
66
68
67 -- ** Conversions
69 -- ** Conversions
68 , yield
70 , yield
@@ -283,6 +285,19 @@ replicate n a = go n
283 where
285 where
284 go cnt = if cnt <= 0 then nil else a `cons` go (cnt - 1)
286 go cnt = if cnt <= 0 then nil else a `cons` go (cnt - 1)
285
287
288+{-# INLINE fromIndicesM #-}
289+fromIndicesM :: (IsStream t, MonadAsync m) => (Int -> m a) -> t m a
290+fromIndicesM gen = go 0
291+ where
292+ go i = mkStream $ \st stp sng yld -> do
293+ foldStreamShared st stp sng yld (gen i |: go (i + 1))
294+
295+{-# INLINE fromIndices #-}
296+fromIndices :: IsStream t => (Int -> a) -> t m a
297+fromIndices gen = go 0
298+ where
299+ go n = (gen n) `cons` go (n + 1)
300+
286-------------------------------------------------------------------------------
301-------------------------------------------------------------------------------
287-- Conversions
302-- Conversions
288-------------------------------------------------------------------------------
303-------------------------------------------------------------------------------