on master help sign in
Change the signature of replicateM
6e021ceAdithya Kumar
benchmark/Streamly/Benchmark/Data/Fold.hs+2−2
@@ -277,8 +277,8 @@ unzipWithMinM = do
277unfoldMany :: Int -> Benchmarkable
277unfoldMany :: Int -> Benchmarkable
278unfoldMany val =
278unfoldMany val =
279 nfIO
279 nfIO
280− $ Stream.fold (FL.unfoldMany (Unfold.replicateM val) FL.drain)
280+ $ Stream.fold (FL.unfoldMany Unfold.replicateM FL.drain)
281− $ Stream.fromPure (randomRIO (1, 1 :: Int))
281+ $ Stream.fromPure (val, randomRIO (1, 1 :: Int))
282
282
283-------------------------------------------------------------------------------
283-------------------------------------------------------------------------------
284-- Benchmarks
284-- Benchmarks
benchmark/Streamly/Benchmark/Data/Stream/Expand.hs+4−2
@@ -8,6 +8,7 @@
8{-# LANGUAGE FlexibleContexts #-}
8{-# LANGUAGE FlexibleContexts #-}
9{-# LANGUAGE ScopedTypeVariables #-}
9{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE RankNTypes #-}
10{-# LANGUAGE RankNTypes #-}
11+{-# LANGUAGE TupleSections #-}
11
12
12#ifdef __HADDOCK_VERSION__
13#ifdef __HADDOCK_VERSION__
13#undef INSPECTION
14#undef INSPECTION
@@ -258,8 +259,9 @@ unfoldManyRepl :: Int -> Int -> Int -> IO ()
258unfoldManyRepl outer inner n =
259unfoldManyRepl outer inner n =
259 drain
260 drain
260 $ S.unfoldMany
261 $ S.unfoldMany
261− (UF.lmap return (UF.replicateM inner))
262+ UF.replicateM
262− (sourceUnfoldrM outer n)
263+ (fmap ((inner,) . return) (sourceUnfoldrM outer n))
264+
263
265
264#ifdef INSPECTION
266#ifdef INSPECTION
265inspect $ hasNoTypeClasses 'unfoldManyRepl
267inspect $ hasNoTypeClasses 'unfoldManyRepl
benchmark/Streamly/Benchmark/Data/Stream/StreamD.hs+3−2
@@ -8,6 +8,7 @@
8{-# LANGUAGE CPP #-}
8{-# LANGUAGE CPP #-}
9{-# LANGUAGE FlexibleContexts #-}
9{-# LANGUAGE FlexibleContexts #-}
10{-# LANGUAGE ScopedTypeVariables #-}
10{-# LANGUAGE ScopedTypeVariables #-}
11+{-# LANGUAGE TupleSections #-}
11
12
12#ifdef __HADDOCK_VERSION__
13#ifdef __HADDOCK_VERSION__
13#undef INSPECTION
14#undef INSPECTION
@@ -416,8 +417,8 @@ unfoldManyRepl :: Int -> Int -> Int -> IO ()
416unfoldManyRepl outer inner n =
417unfoldManyRepl outer inner n =
417 S.drain
418 S.drain
418 $ S.unfoldMany
419 $ S.unfoldMany
419− (UF.lmap return (UF.replicateM inner))
420+ UF.replicateM
420− (sourceUnfoldrMN outer n)
421+ (S.map ((inner,) . return) (sourceUnfoldrMN outer n))
421
422
422#ifdef INSPECTION
423#ifdef INSPECTION
423inspect $ hasNoTypeClasses 'unfoldManyRepl
424inspect $ hasNoTypeClasses 'unfoldManyRepl
benchmark/Streamly/Benchmark/Data/Unfold.hs+1−1
@@ -231,7 +231,7 @@ _fromProducer = undefined
231
231
232{-# INLINE replicateM #-}
232{-# INLINE replicateM #-}
233replicateM :: Monad m => Int -> Int -> m ()
233replicateM :: 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)
235
235
236{-# INLINE repeatM #-}
236{-# INLINE repeatM #-}
237repeatM :: Monad m => Int -> Int -> m ()
237repeatM :: 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)
103
103
104-- | Convert an 'Unfold' into a stream by supplying it an input seed.
104-- | Convert an 'Unfold' into a stream by supplying it an input seed.
105--
105--
106−-- >>> s = Stream.unfold (Unfold.replicateM 3) (putStrLn "hello")
106+-- >>> s = Stream.unfold Unfold.replicateM (3, putStrLn "hello")
107-- >>> Stream.fold Fold.drain s
107-- >>> Stream.fold Fold.drain s
108-- hello
108-- hello
109-- hello
109-- hello
core/src/Streamly/Internal/Data/Stream/StreamD/Generate.hs+1−1
@@ -182,7 +182,7 @@ repeat x = Stream (\_ _ -> return $ Yield x ()) ()
182{-# INLINE_NORMAL replicateM #-}
182{-# INLINE_NORMAL replicateM #-}
183replicateM :: forall m a. Monad m => Int -> m a -> Stream m a
183replicateM :: forall m a. Monad m => Int -> m a -> Stream m a
184#ifdef USE_UNFOLDS_EVERYWHERE
184#ifdef USE_UNFOLDS_EVERYWHERE
185−replicateM n = unfold (Unfold.replicateM n)
185+replicateM n p = unfold Unfold.replicateM (n, p)
186#else
186#else
187replicateM n p = Stream step n
187replicateM n p = Stream step n
188 where
188 where
core/src/Streamly/Internal/Data/Unfold.hs+8−13
@@ -657,29 +657,24 @@ fromPtr = Unfold step return
657-- Specialized Generation
657-- Specialized Generation
658------------------------------------------------------------------------------
658------------------------------------------------------------------------------
659
659
660−-- XXX If the count depends on the input e.g. when we have to resample (see
660+-- | Given a seed @(n, action)@, generates a stream replicating the @action@ @n@
661−-- resample in streamly-statistics) an array and we have to iterate as many
661+-- times.
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.
665--
662--
666−-- | Generates a stream replicating the seed @n@ times.
667−--
668−-- /Since: 0.8.0/
663+-- /Since: 0.9.0/
669--
664--
670{-# INLINE replicateM #-}
665{-# INLINE replicateM #-}
671−replicateM :: Applicative m => Int -> Unfold m (m a) a
666+replicateM :: Applicative m => Unfold m (Int, m a) a
672−replicateM n = Unfold step inject
667+replicateM = Unfold step inject
673
668
674 where
669 where
675
670
676− inject action = pure (action, n)
671+ inject seed = pure seed
677
672
678 {-# INLINE_LATE step #-}
673 {-# INLINE_LATE step #-}
679− step (action, i) =
674+ step (i, action) =
680 if i <= 0
675 if i <= 0
681 then pure Stop
676 then pure Stop
682− else (\x -> Yield x (action, i - 1)) <$> action
677+ else (\x -> Yield x (i - 1, action)) <$> action
683
678
684-- | Generates an infinite stream repeating the seed.
679-- | Generates an infinite stream repeating the seed.
685--
680--
docs/User/ProjectRelated/Changelog.md+1
@@ -24,6 +24,7 @@
24 `streamly-statistics` package instead.
24 `streamly-statistics` package instead.
25* `Streamly.Data.Array.Foreign` is deprecated. Use `Streamly.Data.Array.Unboxed`
25* `Streamly.Data.Array.Foreign` is deprecated. Use `Streamly.Data.Array.Unboxed`
26 instead.
26 instead.
27+* The type of `replicateM` in `Streamly.Data.Unfold` is changed.
27
28
28## 0.8.2 (Mar 2022)
29## 0.8.2 (Mar 2022)
29
30
src/Streamly/Internal/Data/Stream/IsStream/Generate.hs+1−1
@@ -141,7 +141,7 @@ import Prelude hiding (iterate, replicate, repeat)
141
141
142-- | Convert an 'Unfold' into a stream by supplying it an input seed.
142-- | Convert an 'Unfold' into a stream by supplying it an input seed.
143--
143--
144−-- >>> Stream.drain $ Stream.unfold (Unfold.replicateM 3) (putStrLn "hello")
144+-- >>> Stream.drain $ Stream.unfold Unfold.replicateM (3, putStrLn "hello")
145-- hello
145-- hello
146-- hello
146-- hello
147-- hello
147-- hello
test/Streamly/Test/Data/Unfold.hs+5−5
@@ -168,8 +168,8 @@ replicateM =
168 property
168 property
169 $ \i ->
169 $ \i ->
170 let ns = max 0 i
170 let ns = max 0 i
171− seed = modify (+ 1) >> get
171+ seed = (i, modify (+ 1) >> get)
172− in testUnfoldMD (UF.replicateM i) seed 0 ns [1 .. i]
172+ in testUnfoldMD UF.replicateM seed 0 ns [1 .. i]
173
173
174repeatM :: Bool
174repeatM :: Bool
175repeatM =
175repeatM =
@@ -547,8 +547,8 @@ zipWithM =
547
547
548concat :: Bool
548concat :: Bool
549concat =
549concat =
550− let unfIn = UF.replicateM 10
550+ let unfIn = UF.replicateM
551− unfOut = UF.map return UF.enumerateFromToIntegral
551+ unfOut = UF.map ((10,) . return) UF.enumerateFromToIntegral
552 unf = UF.many unfIn unfOut
552 unf = UF.many unfIn unfOut
553 lst = Prelude.concat $ Prelude.map (Prelude.replicate 10) [1 .. 10]
553 lst = Prelude.concat $ Prelude.map (Prelude.replicate 10) [1 .. 10]
554 in testUnfoldD unf (1, 10) lst
554 in testUnfoldD unf (1, 10) lst
@@ -568,7 +568,7 @@ outerProduct =
568concatMapM :: Bool
568concatMapM :: Bool
569concatMapM =
569concatMapM =
570 let inner b =
570 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
572 in modify (+ 1) >> return u
572 in modify (+ 1) >> return u
573 unf = UF.concatMapM inner UF.enumerateFromToIntegral
573 unf = UF.concatMapM inner UF.enumerateFromToIntegral
574 list = List.concatMap (replicate 10) [1 .. 10]
574 list = List.concatMap (replicate 10) [1 .. 10]