on master help sign in
Add Rewrite rule to fromIndicesM for Serial streams.
commit7758b1198cd382fda5495ec8dec5a73198c0100b authorPranay Sashank <pranaysashank@gmail.com> dateWed 22 May 2019, 11:59 (7 years ago) parent64524bf
2 files changed+25−6
src/Streamly/Prelude.hs+10−6 src/Streamly/Streams/StreamK.hs+15
src/Streamly/Prelude.hs+10−6
@@ -680,7 +680,7 @@ yieldM = K.yieldM
680680-- @since 0.6.0
681681{-# INLINE fromIndices #-}
682682fromIndices :: (IsStream t, Monad m) => (Int -> a) -> t m a
683−fromIndices = fromStreamD . D.fromIndices
683+fromIndices = fromStreamS . S.fromIndices
684684
685685--
686686-- |
@@ -691,13 +691,17 @@ fromIndices = fromStreamD . D.fromIndices
691691-- Generate an infinite stream, whose values are the output of a monadic
692692-- function @f@ applied on the corresponding index. Index starts at 0.
693693--
694+-- /Concurrent/
695+--
694696-- @since 0.6.0
695−{-# INLINE fromIndicesM #-}
697+{-# INLINE_EARLY fromIndicesM #-}
696698fromIndicesM :: (IsStream t, MonadAsync m) => (Int -> m a) -> t m a
697−fromIndicesM gen = go 0
698− where
699− go i = K.mkStream $ \st stp sng yld -> do
700− K.foldStreamShared st stp sng yld (gen i |: go (i + 1))
699+fromIndicesM = K.fromIndicesM
700+
701+{-# RULES "fromIndicesM serial" fromIndicesM = fromIndicesMSerial #-}
702+{-# INLINE fromIndicesMSerial #-}
703+fromIndicesMSerial :: MonadAsync m => (Int -> m a) -> SerialT m a
704+fromIndicesMSerial = fromStreamS . S.fromIndicesM
701705
702706-- |
703707-- @
src/Streamly/Streams/StreamK.hs+15
@@ -63,6 +63,8 @@ module Streamly.Streams.StreamK
6363 , repeat
6464 , replicate
6565 , replicateM
66+ , fromIndices
67+ , fromIndicesM
6668
6769 -- ** Conversions
6870 , yield
@@ -283,6 +285,19 @@ replicate n a = go n
283285 where
284286 go cnt = if cnt <= 0 then nil else a `cons` go (cnt - 1)
285287
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+
286301-------------------------------------------------------------------------------
287302-- Conversions
288303-------------------------------------------------------------------------------