on master help sign in
Add replicate.
commitb2c86b10942cfe9856cf42ad3e1865258314a80c authorPranay Sashank <pranaysashank@gmail.com> dateSun 18 Nov 2018, 11:14 (8 years ago) parent9ccee33
2 files changed+24−1
src/Streamly/Prelude.hs+9−1 test/Prop.hs+15
src/Streamly/Prelude.hs+9−1
@@ -84,6 +84,7 @@ module Streamly.Prelude
8484 -- number of steps can run concurrently. All of these can be expressed in
8585 -- terms of 'K.cons' and 'K.nil' primitives.
8686 , replicateM
87+ , replicate
8788 , K.repeat
8889 , repeatM
8990
@@ -257,7 +258,7 @@ import Prelude
257258 foldl, mapM, mapM_, sequence, all, any, sum, product, elem,
258259 notElem, maximum, minimum, head, last, tail, length, null,
259260 reverse, iterate, init, and, or, lookup, foldr1, (!!),
260− splitAt, scanl, scanl1)
261+ splitAt, scanl, scanl1, replicate)
261262import qualified Prelude
262263import qualified System.IO as IO
263264
@@ -419,6 +420,13 @@ replicateM n m = go n
419420 where
420421 go cnt = if cnt <= 0 then K.nil else m |: go (cnt - 1)
421422
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+
422430-- | Generate a stream by repeatedly executing a monadic action forever. Can be
423431-- expressed as @cycle1 . yieldM@.
424432--
test/Prop.hs+15
@@ -86,6 +86,18 @@ constructWithReplicateM op len = withMaxSuccess maxTestCount $
8686 list <- run $ replicateM (fromIntegral len) x
8787 listEquals (==) stream list
8888
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+
89101transformFromList
90102 :: (Eq b, Show b) =>
91103 ([a] -> t IO a)
@@ -838,6 +850,9 @@ main = hspec
838850 -- XXX test for all types of streams
839851 constructWithIterate serially
840852
853+ describe "Construction with replicate" $ do
854+ serialOps $ prop "serially replicate" . constructWithReplicate
855+
841856 describe "Functor operations" $ do
842857 serialOps $ functorOps S.fromFoldable "serially" (==)
843858 serialOps $ functorOps folded "serially folded" (==)