on master help sign in
Make fromIndicesM concurrent, and add tests for fromIndicesM.
commit64524bf4f6290b3fd41eecaec73914c48bf5bfd9 authorPranay Sashank <pranaysashank@gmail.com> dateWed 22 May 2019, 02:01 (7 years ago) parente6699ec
2 files changed+32−3
src/Streamly/Prelude.hs+5−3 test/Prop.hs+27
src/Streamly/Prelude.hs+5−3
@@ -682,7 +682,6 @@ yieldM = K.yieldM
682682fromIndices :: (IsStream t, Monad m) => (Int -> a) -> t m a
683683fromIndices = fromStreamD . D.fromIndices
684684
685−-- XXX this needs to be concurrent
686685--
687686-- |
688687-- @
@@ -694,8 +693,11 @@ fromIndices = fromStreamD . D.fromIndices
694693--
695694-- @since 0.6.0
696695{-# INLINE fromIndicesM #-}
697−fromIndicesM :: (IsStream t, Monad m) => (Int -> m a) -> t m a
698−fromIndicesM = fromStreamD . D.fromIndicesM
696+fromIndicesM :: (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))
699701
700702-- |
701703-- @
test/Prop.hs+27
@@ -173,6 +173,29 @@ constructWithIterateM op len =
173173 streamEffect <- run $ readIORef mvl
174174 listEquals (==) streamEffect list
175175
176+constructWithFromIndices ::
177+ IsStream t => (t IO Int -> SerialT IO Int) -> Word8 -> Property
178+constructWithFromIndices op len =
179+ withMaxSuccess maxTestCount $
180+ monadicIO $ do
181+ stream <-
182+ run $ (S.toList . op . S.take (fromIntegral len)) (S.fromIndices id)
183+ let list = take (fromIntegral len) (iterate (+ 1) 0)
184+ listEquals (==) stream list
185+
186+constructWithFromIndicesM ::
187+ IsStream t => (t IO Int -> SerialT IO Int) -> Word8 -> Property
188+constructWithFromIndicesM op len =
189+ withMaxSuccess maxTestCount $
190+ monadicIO $ do
191+ mvl <- run (newIORef [] :: IO (IORef [Int]))
192+ let addIndex mv i = modifyIORef' mv (++ [i]) >> return i
193+ list = take (fromIntegral len) (iterate (+ 1) 0)
194+ run $
195+ S.drain . op $
196+ S.take (fromIntegral len) $ S.fromIndicesM (addIndex mvl)
197+ streamEffect <- run $ readIORef mvl
198+ listEquals (==) streamEffect list
176199
177200-------------------------------------------------------------------------------
178201-- Concurrent generation
@@ -1017,6 +1040,10 @@ main = hspec
10171040 -- parallelOps $ prop "parallely iterateM" parallely . onstructWithIterate
10181041 -- XXX add tests for fromIndices
10191042
1043+ serialOps $ prop "serially fromIndices" . constructWithFromIndices
1044+
1045+ serialOps $ prop "serially fromIndicesM" . constructWithFromIndicesM
1046+
10201047 describe "Functor operations" $ do
10211048 serialOps $ functorOps S.fromFoldable "serially" (==)
10221049 serialOps $ functorOps folded "serially folded" (==)