on master help sign in
Add docs & change the signature of replicateM and repeatM

This commit also include some other minor changes

commit5004f124094a2f6960e3ac27f4d1e3c7120f3773 authoradithyaov <adi.obilisetty@gmail.com> dateWed 7 Oct 2020, 10:34 (6 years ago) parent07d0609
6 files changed+69−46
benchmark/Streamly/Benchmark/Data/Stream/StreamD.hs+4−1 benchmark/Streamly/Benchmark/Data/Unfold.hs+8−9 benchmark/Streamly/Benchmark/Prelude/Serial.hs+4−1 benchmark/Streamly/Benchmark/Prelude/WSerial.hs+2−2 src/Streamly/Internal/Data/Unfold.hs+45−29 test/Streamly/Test/Internal/Data/Unfold.hs+6−4
benchmark/Streamly/Benchmark/Data/Stream/StreamD.hs+4−1
@@ -407,7 +407,10 @@ inspect $ 'concatMapRepl `hasNoType` ''SPEC
407407{-# INLINE concatUnfoldRepl #-}
408408concatUnfoldRepl :: Int -> Int -> Int -> IO ()
409409concatUnfoldRepl outer inner n =
410− S.drain $ S.concatMapU (UF.replicateM inner) (sourceUnfoldrMN outer n)
410+ S.drain
411+ $ S.concatMapU
412+ (UF.lmap return (UF.replicateM inner))
413+ (sourceUnfoldrMN outer n)
411414
412415#ifdef INSPECTION
413416inspect $ hasNoTypeClasses 'concatUnfoldRepl
benchmark/Streamly/Benchmark/Data/Unfold.hs+8−9
@@ -36,11 +36,11 @@ fromListM value n =
3636
3737{-# INLINE replicateM #-}
3838replicateM :: Monad m => Int -> Int -> m ()
39−replicateM value n = S.drain $ S.unfold (UF.replicateM value) n
39+replicateM value n = S.drain $ S.unfold (UF.replicateM value) (return n)
4040
4141{-# INLINE repeatM #-}
4242repeatM :: Monad m => Int -> Int -> m ()
43−repeatM value n = S.drain $ S.take value $ S.unfold UF.repeatM n
43+repeatM value n = S.drain $ S.take value $ S.unfold UF.repeatM (return n)
4444
4545{-# INLINE enumerateFromStepIntegral #-}
4646enumerateFromStepIntegral :: Monad m => Int -> Int -> m ()
@@ -56,7 +56,6 @@ enumerateFromToIntegral value n =
5656-- Stream transformation
5757-------------------------------------------------------------------------------
5858
59−-- XXX Investigate
6059{-# INLINE take #-}
6160take :: Monad m => Int -> Int -> m ()
6261take value n =
@@ -73,9 +72,9 @@ takeWhileM value n =
7372 (UF.enumerateFromToIntegral (value + n)))
7473 n
7574
76−{-# INLINE dropOne #-}
77−dropOne :: Monad m => Int -> Int -> m ()
78−dropOne value n =
75+{-# INLINE _dropOne #-}
76+_dropOne :: Monad m => Int -> Int -> m ()
77+_dropOne value n =
7978 S.drain $ S.unfold (UF.drop 1 (UF.enumerateFromToIntegral (value + n))) n
8079
8180{-# INLINE dropAll #-}
@@ -147,14 +146,15 @@ o_1_space_serial_transformation value =
147146 , benchIO "filterAllOut" $ filterAllOut value
148147 , benchIO "filterAllIn" $ filterAllIn value
149148 , benchIO "filterSome" $ filterSome value
150− , benchIO "dropOne" $ dropOne value
149+ -- This will take nanoseconds, We need to fix the benchmark reporting to
150+ -- have microseconds as the minimum unit before uncommenting this.
151+ -- , benchIO "dropOne" $ dropOne value
151152 , benchIO "dropAll" $ dropAll value
152153 , benchIO "dropWhileMTrue" $ dropWhileMTrue value
153154 , benchIO "dropWhileMFalse" $ dropWhileMFalse value
154155 ]
155156 ]
156157
157−
158158o_1_space_serial_combination :: Int -> [Benchmark]
159159o_1_space_serial_combination value =
160160 [ bgroup "combination"
@@ -162,7 +162,6 @@ o_1_space_serial_combination value =
162162 ]
163163 ]
164164
165−
166165o_1_space_serial_outerProduct :: Int -> [Benchmark]
167166o_1_space_serial_outerProduct value =
168167 [ bgroup "outer-product"
benchmark/Streamly/Benchmark/Prelude/Serial.hs+4−1
@@ -1570,7 +1570,10 @@ inspect $ 'concatMapWithAppend `hasNoType` ''SPEC
15701570{-# INLINE concatUnfoldRepl #-}
15711571concatUnfoldRepl :: Int -> Int -> Int -> IO ()
15721572concatUnfoldRepl outer inner n =
1573− S.drain $ S.concatUnfold (UF.replicateM inner) (sourceUnfoldrM outer n)
1573+ S.drain
1574+ $ S.concatUnfold
1575+ (UF.lmap return (UF.replicateM inner))
1576+ (sourceUnfoldrM outer n)
15741577
15751578#ifdef INSPECTION
15761579inspect $ hasNoTypeClasses 'concatUnfoldRepl
benchmark/Streamly/Benchmark/Prelude/WSerial.hs+2−2
@@ -126,7 +126,7 @@ o_1_space_concat value =
126126concatUnfoldInterleaveRepl4xN :: Int -> Int -> IO ()
127127concatUnfoldInterleaveRepl4xN value n =
128128 S.drain $ Internal.concatUnfoldInterleave
129− (UF.replicateM 4)
129+ (UF.lmap return (UF.replicateM 4))
130130 (sourceUnfoldrM (value `div` 4) n)
131131
132132#ifdef INSPECTION
@@ -140,7 +140,7 @@ inspect $ hasNoTypeClasses 'concatUnfoldInterleaveRepl4xN
140140concatUnfoldRoundrobinRepl4xN :: Int -> Int -> IO ()
141141concatUnfoldRoundrobinRepl4xN value n =
142142 S.drain $ Internal.concatUnfoldRoundrobin
143− (UF.replicateM 4)
143+ (UF.lmap return (UF.replicateM 4))
144144 (sourceUnfoldrM (value `div` 4) n)
145145
146146#ifdef INSPECTION
src/Streamly/Internal/Data/Unfold.hs+45−29
@@ -484,30 +484,36 @@ unfoldrM next = Unfold step return
484484-- Specialized Generation
485485------------------------------------------------------------------------------
486486
487−-- XXX replicateM? Maybe just replicate?
488487-- | Generates a stream replicating the seed @n@ times.
489488--
490489{-# INLINE replicateM #-}
491−replicateM :: Monad m => Int -> Unfold m a a
490+replicateM :: Monad m => Int -> Unfold m (m a) a
492491replicateM n = Unfold step inject
492+
493493 where
494+
494495 inject x = return (x, n)
496+
495497 {-# INLINE_LATE step #-}
496− step (x, i) = return $
497− if i <= 0
498− then Stop
499− else Yield x (x, i - 1)
498+ step (x, i) = do
499+ x1 <- x
500+ return
501+ $ if i <= 0
502+ then Stop
503+ else Yield x1 (x, i - 1)
500504
501−-- XXX repeatM? Maybe just repeat?
502505-- | Generates an infinite stream repeating the seed.
503506--
504507{-# INLINE repeatM #-}
505−repeatM :: Monad m => Unfold m a a
508+repeatM :: Monad m => Unfold m (m a) a
506509repeatM = Unfold step return
507510 where
508511 {-# INLINE_LATE step #-}
509− step x = return $ Yield x x
512+ step x = x >>= \x1 -> return $ Yield x1 x
510513
514+-- | Generates an infinite stream starting with the given seed and applying the
515+-- given function repeteadly.
516+--
511517{-# INLINE iterateM #-}
512518iterateM :: Monad m => (a -> m a) -> Unfold m a a
513519iterateM f = Unfold step f
@@ -517,6 +523,9 @@ iterateM f = Unfold step f
517523 fx <- f x
518524 return $ Yield fx fx
519525
526+-- | @fromIndicesM gen@ generates an infinite stream of values using @gen@
527+-- starting from the seed.
528+--
520529{-# INLINE_NORMAL fromIndicesM #-}
521530fromIndicesM :: Monad m => (Int -> m a) -> Unfold m Int a
522531fromIndicesM gen = Unfold step return
@@ -585,52 +594,59 @@ filter f = filterM (return . f)
585594{-# INLINE_NORMAL drop #-}
586595drop :: Monad m => Int -> Unfold m a b -> Unfold m a b
587596drop n (Unfold step inject) = Unfold step' inject'
588− where
597+
598+ where
599+
589600 inject' a = do
590601 b <- inject a
591602 return (b, n)
603+
592604 {-# INLINE_LATE step' #-}
593605 step' (st, i)
594606 | i > 0 = do
595607 r <- step st
596− return $
597− case r of
598− Yield _ s -> Skip (s, i - 1)
599− Skip s -> Skip (s, i)
600− Stop -> Stop
608+ return
609+ $ case r of
610+ Yield _ s -> Skip (s, i - 1)
611+ Skip s -> Skip (s, i)
612+ Stop -> Stop
601613 | otherwise = do
602614 r <- step st
603− return $
604− case r of
605− Yield x s -> Yield x (s, 0)
606− Skip s -> Skip (s, 0)
607− Stop -> Stop
615+ return
616+ $ case r of
617+ Yield x s -> Yield x (s, 0)
618+ Skip s -> Skip (s, 0)
619+ Stop -> Stop
608620
609621{-# INLINE_NORMAL dropWhileM #-}
610622dropWhileM :: Monad m => (b -> m Bool) -> Unfold m a b -> Unfold m a b
611623dropWhileM f (Unfold step inject) = Unfold step' inject'
612− where
624+
625+ where
626+
613627 inject' a = do
614628 b <- inject a
615629 return $ Left b
630+
616631 {-# INLINE_LATE step' #-}
617632 step' (Left st) = do
618633 r <- step st
619634 case r of
620635 Yield x s -> do
621636 b <- f x
622− if b
623− then return $ Skip (Left s)
624− else return $ Yield x (Right s)
637+ return
638+ $ if b
639+ then Skip (Left s)
640+ else Yield x (Right s)
625641 Skip s -> return $ Skip (Left s)
626642 Stop -> return Stop
627−
628643 step' (Right st) = do
629644 r <- step st
630− case r of
631− Yield x s -> return $ Yield x (Right s)
632− Skip s -> return $ Skip (Right s)
633− Stop -> return Stop
645+ return
646+ $ case r of
647+ Yield x s -> Yield x (Right s)
648+ Skip s -> Skip (Right s)
649+ Stop -> Stop
634650
635651{-# INLINE dropWhile #-}
636652dropWhile :: Monad m => (b -> Bool) -> Unfold m a b -> Unfold m a b
test/Streamly/Test/Internal/Data/Unfold.hs+6−4
@@ -90,7 +90,7 @@ replicateM :: Property
9090replicateM =
9191 property $ \x ->
9292 runIdentity $ do
93− x1 <- S.toList $ S.unfold (UF.replicateM x) ()
93+ x1 <- S.toList $ S.unfold (UF.replicateM x) (return ())
9494 let x2 = P.replicate x ()
9595 return $ x1 == x2
9696
@@ -98,7 +98,7 @@ repeatM :: Property
9898repeatM =
9999 property $ \x ->
100100 runIdentity $ do
101− x1 <- S.toList $ S.take x $ S.unfold UF.repeatM ()
101+ x1 <- S.toList $ S.take x $ S.unfold UF.repeatM (return ())
102102 let x2 = P.take x $ P.repeat ()
103103 return $ x1 == x2
104104
@@ -142,8 +142,10 @@ drop :: Property
142142drop =
143143 property $ \x y ->
144144 runIdentity $ do
145− x1 <- S.toList $ S.unfold (UF.drop x (UF.replicateM (x + y))) ()
146− x2 <- S.toList $ S.drop x $ S.unfold (UF.replicateM (x + y)) ()
145+ x1 <- S.toList
146+ $ S.unfold (UF.drop x (UF.replicateM (x + y))) (return ())
147+ x2 <- S.toList
148+ $ S.drop x $ S.unfold (UF.replicateM (x + y)) (return ())
147149 return $ x1 == x2
148150
149151dropWhileM :: Property