on master help sign in
Add replicate.
b2c86b1Pranay Sashank
src/Streamly/Prelude.hs+9−1
@@ -84,6 +84,7 @@ module Streamly.Prelude
84 -- number of steps can run concurrently. All of these can be expressed in
84 -- number of steps can run concurrently. All of these can be expressed in
85 -- terms of 'K.cons' and 'K.nil' primitives.
85 -- terms of 'K.cons' and 'K.nil' primitives.
86 , replicateM
86 , replicateM
87+ , replicate
87 , K.repeat
88 , K.repeat
88 , repeatM
89 , repeatM
89
90
@@ -257,7 +258,7 @@ import Prelude
257 foldl, mapM, mapM_, sequence, all, any, sum, product, elem,
258 foldl, mapM, mapM_, sequence, all, any, sum, product, elem,
258 notElem, maximum, minimum, head, last, tail, length, null,
259 notElem, maximum, minimum, head, last, tail, length, null,
259 reverse, iterate, init, and, or, lookup, foldr1, (!!),
260 reverse, iterate, init, and, or, lookup, foldr1, (!!),
260− splitAt, scanl, scanl1)
261+ splitAt, scanl, scanl1, replicate)
261import qualified Prelude
262import qualified Prelude
262import qualified System.IO as IO
263import qualified System.IO as IO
263
264
@@ -419,6 +420,13 @@ replicateM n m = go n
419 where
420 where
420 go cnt = if cnt <= 0 then K.nil else m |: go (cnt - 1)
421 go cnt = if cnt <= 0 then K.nil else m |: go (cnt - 1)
421
422
423+-- | Generate a stream by replicating the value @n@ times.
424+--
425+replicate :: IsStream t => Int -> a -> t m a
426+replicate n a = go n
427+ where
428+ go cnt = if cnt <= 0 then K.nil else a `K.cons` go (cnt - 1)
429+
422-- | Generate a stream by repeatedly executing a monadic action forever. Can be
430-- | Generate a stream by repeatedly executing a monadic action forever. Can be
423-- expressed as @cycle1 . yieldM@.
431-- expressed as @cycle1 . yieldM@.
424--
432--
test/Prop.hs+15
@@ -86,6 +86,18 @@ constructWithReplicateM op len = withMaxSuccess maxTestCount $
86 list <- run $ replicateM (fromIntegral len) x
86 list <- run $ replicateM (fromIntegral len) x
87 listEquals (==) stream list
87 listEquals (==) stream list
88
88
89+constructWithReplicate
90+ :: IsStream t
91+ => (t IO Int -> SerialT IO Int)
92+ -> Word8
93+ -> Property
94+constructWithReplicate op len = withMaxSuccess maxTestCount $
95+ monadicIO $ do
96+ let x = (1 :: Int)
97+ stream <- run $ (S.toList . op) (S.replicate (fromIntegral len) x)
98+ let list = replicate (fromIntegral len) x
99+ listEquals (==) stream list
100+
89transformFromList
101transformFromList
90 :: (Eq b, Show b) =>
102 :: (Eq b, Show b) =>
91 ([a] -> t IO a)
103 ([a] -> t IO a)
@@ -838,6 +850,9 @@ main = hspec
838 -- XXX test for all types of streams
850 -- XXX test for all types of streams
839 constructWithIterate serially
851 constructWithIterate serially
840
852
853+ describe "Construction with replicate" $ do
854+ serialOps $ prop "serially replicate" . constructWithReplicate
855+
841 describe "Functor operations" $ do
856 describe "Functor operations" $ do
842 serialOps $ functorOps S.fromFoldable "serially" (==)
857 serialOps $ functorOps S.fromFoldable "serially" (==)
843 serialOps $ functorOps folded "serially folded" (==)
858 serialOps $ functorOps folded "serially folded" (==)