on master help sign in
Change the signature of replicateM
commit6e021ceb186d219c7ee39cae9f9120aed05fb6c9 authorAdithya Kumar <adithya@composewell.com> dateTue 4 Oct 2022, 18:56 (4 years ago) parentfa417eb
10 files changed+27−28
benchmark/Streamly/Benchmark/Data/Fold.hs+2−2 benchmark/Streamly/Benchmark/Data/Stream/Expand.hs+4−2 benchmark/Streamly/Benchmark/Data/Stream/StreamD.hs+3−2 benchmark/Streamly/Benchmark/Data/Unfold.hs+1−1 core/src/Streamly/Internal/Data/Stream/Generate.hs+1−1 core/src/Streamly/Internal/Data/Stream/StreamD/Generate.hs+1−1 core/src/Streamly/Internal/Data/Unfold.hs+8−13 docs/User/ProjectRelated/Changelog.md+1 src/Streamly/Internal/Data/Stream/IsStream/Generate.hs+1−1 test/Streamly/Test/Data/Unfold.hs+5−5
benchmark/Streamly/Benchmark/Data/Fold.hs+2−2
@@ -277,8 +277,8 @@ unzipWithMinM = do
277277unfoldMany :: Int -> Benchmarkable
278278unfoldMany val =
279279 nfIO
280− $ Stream.fold (FL.unfoldMany (Unfold.replicateM val) FL.drain)
281− $ Stream.fromPure (randomRIO (1, 1 :: Int))
280+ $ Stream.fold (FL.unfoldMany Unfold.replicateM FL.drain)
281+ $ Stream.fromPure (val, randomRIO (1, 1 :: Int))
282282
283283-------------------------------------------------------------------------------
284284-- Benchmarks
benchmark/Streamly/Benchmark/Data/Stream/Expand.hs+4−2
@@ -8,6 +8,7 @@
88{-# LANGUAGE FlexibleContexts #-}
99{-# LANGUAGE ScopedTypeVariables #-}
1010{-# LANGUAGE RankNTypes #-}
11+{-# LANGUAGE TupleSections #-}
1112
1213#ifdef __HADDOCK_VERSION__
1314#undef INSPECTION
@@ -258,8 +259,9 @@ unfoldManyRepl :: Int -> Int -> Int -> IO ()
258259unfoldManyRepl outer inner n =
259260 drain
260261 $ S.unfoldMany
261− (UF.lmap return (UF.replicateM inner))
262− (sourceUnfoldrM outer n)
262+ UF.replicateM
263+ (fmap ((inner,) . return) (sourceUnfoldrM outer n))
264+
263265
264266#ifdef INSPECTION
265267inspect $ hasNoTypeClasses 'unfoldManyRepl
benchmark/Streamly/Benchmark/Data/Stream/StreamD.hs+3−2
@@ -8,6 +8,7 @@
88{-# LANGUAGE CPP #-}
99{-# LANGUAGE FlexibleContexts #-}
1010{-# LANGUAGE ScopedTypeVariables #-}
11+{-# LANGUAGE TupleSections #-}
1112
1213#ifdef __HADDOCK_VERSION__
1314#undef INSPECTION
@@ -416,8 +417,8 @@ unfoldManyRepl :: Int -> Int -> Int -> IO ()
416417unfoldManyRepl outer inner n =
417418 S.drain
418419 $ S.unfoldMany
419− (UF.lmap return (UF.replicateM inner))
420− (sourceUnfoldrMN outer n)
420+ UF.replicateM
421+ (S.map ((inner,) . return) (sourceUnfoldrMN outer n))
421422
422423#ifdef INSPECTION
423424inspect $ hasNoTypeClasses 'unfoldManyRepl
benchmark/Streamly/Benchmark/Data/Unfold.hs+1−1
@@ -231,7 +231,7 @@ _fromProducer = undefined
231231
232232{-# INLINE replicateM #-}
233233replicateM :: Monad m => Int -> Int -> m ()
234−replicateM size start = drainGeneration (UF.replicateM size) (return start)
234+replicateM size start = drainGeneration UF.replicateM (size, return start)
235235
236236{-# INLINE repeatM #-}
237237repeatM :: Monad m => Int -> Int -> m ()
core/src/Streamly/Internal/Data/Stream/Generate.hs+1−1
@@ -103,7 +103,7 @@ import Prelude hiding (iterate, replicate, repeat)
103103
104104-- | Convert an 'Unfold' into a stream by supplying it an input seed.
105105--
106−-- >>> s = Stream.unfold (Unfold.replicateM 3) (putStrLn "hello")
106+-- >>> s = Stream.unfold Unfold.replicateM (3, putStrLn "hello")
107107-- >>> Stream.fold Fold.drain s
108108-- hello
109109-- hello
core/src/Streamly/Internal/Data/Stream/StreamD/Generate.hs+1−1
@@ -182,7 +182,7 @@ repeat x = Stream (\_ _ -> return $ Yield x ()) ()
182182{-# INLINE_NORMAL replicateM #-}
183183replicateM :: forall m a. Monad m => Int -> m a -> Stream m a
184184#ifdef USE_UNFOLDS_EVERYWHERE
185−replicateM n = unfold (Unfold.replicateM n)
185+replicateM n p = unfold Unfold.replicateM (n, p)
186186#else
187187replicateM n p = Stream step n
188188 where
core/src/Streamly/Internal/Data/Unfold.hs+8−13
@@ -657,29 +657,24 @@ fromPtr = Unfold step return
657657-- Specialized Generation
658658------------------------------------------------------------------------------
659659
660−-- XXX If the count depends on the input e.g. when we have to resample (see
661−-- resample in streamly-statistics) an array and we have to iterate as many
662−-- times as the array then we need the count to be passed via unfold i.e. it
663−-- should be Unfold m (Int, m a) a. We can use concatMap to pass the count
664−-- argument to replicateM but that won't perform well.
660+-- | Given a seed @(n, action)@, generates a stream replicating the @action@ @n@
661+-- times.
665662--
666−-- | Generates a stream replicating the seed @n@ times.
667−--
668−-- /Since: 0.8.0/
663+-- /Since: 0.9.0/
669664--
670665{-# INLINE replicateM #-}
671−replicateM :: Applicative m => Int -> Unfold m (m a) a
672−replicateM n = Unfold step inject
666+replicateM :: Applicative m => Unfold m (Int, m a) a
667+replicateM = Unfold step inject
673668
674669 where
675670
676− inject action = pure (action, n)
671+ inject seed = pure seed
677672
678673 {-# INLINE_LATE step #-}
679− step (action, i) =
674+ step (i, action) =
680675 if i <= 0
681676 then pure Stop
682− else (\x -> Yield x (action, i - 1)) <$> action
677+ else (\x -> Yield x (i - 1, action)) <$> action
683678
684679-- | Generates an infinite stream repeating the seed.
685680--
docs/User/ProjectRelated/Changelog.md+1
@@ -24,6 +24,7 @@
2424 `streamly-statistics` package instead.
2525* `Streamly.Data.Array.Foreign` is deprecated. Use `Streamly.Data.Array.Unboxed`
2626 instead.
27+* The type of `replicateM` in `Streamly.Data.Unfold` is changed.
2728
2829## 0.8.2 (Mar 2022)
2930
src/Streamly/Internal/Data/Stream/IsStream/Generate.hs+1−1
@@ -141,7 +141,7 @@ import Prelude hiding (iterate, replicate, repeat)
141141
142142-- | Convert an 'Unfold' into a stream by supplying it an input seed.
143143--
144−-- >>> Stream.drain $ Stream.unfold (Unfold.replicateM 3) (putStrLn "hello")
144+-- >>> Stream.drain $ Stream.unfold Unfold.replicateM (3, putStrLn "hello")
145145-- hello
146146-- hello
147147-- hello
test/Streamly/Test/Data/Unfold.hs+5−5
@@ -168,8 +168,8 @@ replicateM =
168168 property
169169 $ \i ->
170170 let ns = max 0 i
171− seed = modify (+ 1) >> get
172− in testUnfoldMD (UF.replicateM i) seed 0 ns [1 .. i]
171+ seed = (i, modify (+ 1) >> get)
172+ in testUnfoldMD UF.replicateM seed 0 ns [1 .. i]
173173
174174repeatM :: Bool
175175repeatM =
@@ -547,8 +547,8 @@ zipWithM =
547547
548548concat :: Bool
549549concat =
550− let unfIn = UF.replicateM 10
551− unfOut = UF.map return UF.enumerateFromToIntegral
550+ let unfIn = UF.replicateM
551+ unfOut = UF.map ((10,) . return) UF.enumerateFromToIntegral
552552 unf = UF.many unfIn unfOut
553553 lst = Prelude.concat $ Prelude.map (Prelude.replicate 10) [1 .. 10]
554554 in testUnfoldD unf (1, 10) lst
@@ -568,7 +568,7 @@ outerProduct =
568568concatMapM :: Bool
569569concatMapM =
570570 let inner b =
571− let u = UF.lmap (\_ -> modify (+ 1) >> return b) (UF.replicateM 10)
571+ let u = UF.lmap (\_ -> (10, modify (+ 1) >> return b)) UF.replicateM
572572 in modify (+ 1) >> return u
573573 unf = UF.concatMapM inner UF.enumerateFromToIntegral
574574 list = List.concatMap (replicate 10) [1 .. 10]