on master help sign in
Rename unfold scan to postscan, add benchmark, test
commit3a59974ed86596c104e3e548ce181a5e0917331c authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateWed 16 Mar 2022, 14:14 (5 years ago) parent3b3bda7
4 files changed+28−12
benchmark/Streamly/Benchmark/Data/Unfold.hs+6 core/src/Streamly/Internal/Data/Unfold.hs+9−9 docs/ConcurrentStreams.hs+3−3 test/Streamly/Test/Data/Unfold.hs+10
benchmark/Streamly/Benchmark/Data/Unfold.hs+6
@@ -290,6 +290,11 @@ enumerateFromToFractional size start =
290290-- Stream transformation
291291-------------------------------------------------------------------------------
292292
293+{-# INLINE postscan #-}
294+postscan :: Monad m => Int -> Int -> m ()
295+postscan size start =
296+ drainTransformationDefault (size + start) (UF.postscan FL.sum) start
297+
293298{-# INLINE map #-}
294299map :: Monad m => Int -> Int -> m ()
295300map size start = drainTransformationDefault (size + start) (UF.map (+1)) start
@@ -663,6 +668,7 @@ o_1_space_transformation size =
663668 [ benchIO "map" $ map size
664669 , benchIO "mapM" $ mapM size
665670 , benchIO "mapMWithInput" $ mapMWithInput size
671+ , benchIO "postscan" $ postscan size
666672 ]
667673 ]
668674
core/src/Streamly/Internal/Data/Unfold.hs+9−9
@@ -201,8 +201,8 @@ module Streamly.Internal.Data.Unfold
201201 , map
202202 , mapM
203203 , mapMWithInput
204− , scanlM'
205− , scan
204+ , postscanlM'
205+ , postscan
206206 , foldMany
207207 -- pipe
208208
@@ -478,9 +478,9 @@ either (Unfold step1 inject1) = Unfold step inject
478478-- | Scan the output of an 'Unfold' to change it in a stateful manner.
479479--
480480-- /Pre-release/
481−{-# INLINE_NORMAL scan #-}
482−scan :: Monad m => Fold m b c -> Unfold m a b -> Unfold m a c
483−scan (Fold stepF initial extract) (Unfold stepU injectU) =
481+{-# INLINE_NORMAL postscan #-}
482+postscan :: Monad m => Fold m b c -> Unfold m a b -> Unfold m a c
483+postscan (Fold stepF initial extract) (Unfold stepU injectU) =
484484 Unfold step inject
485485
486486 where
@@ -509,10 +509,10 @@ scan (Fold stepF initial extract) (Unfold stepU injectU) =
509509
510510-- | Scan the output of an 'Unfold' to change it in a stateful manner.
511511--
512−-- /Unimplemented/
513−{-# INLINE_NORMAL scanlM' #-}
514−scanlM' :: Monad m => (b -> a -> m b) -> m b -> Unfold m c a -> Unfold m c b
515−scanlM' f z = scan (FL.foldlM' f z)
512+-- /Pre-release/
513+{-# INLINE_NORMAL postscanlM' #-}
514+postscanlM' :: Monad m => (b -> a -> m b) -> m b -> Unfold m c a -> Unfold m c b
515+postscanlM' f z = postscan (FL.foldlM' f z)
516516
517517-------------------------------------------------------------------------------
518518-- Convert streams into unfolds
docs/ConcurrentStreams.hs+3−3
@@ -624,9 +624,9 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
624624-- Stream.fromEffect $ putStrLn $ show (x, y)
625625-- :}
626626-- (1,3)
627−-- (1,4)
628−-- (2,3)
629−-- (2,4)
627+-- ...
628+-- ...
629+-- ...
630630--
631631-- Nested iterations are given preference for concurrent evaluation i.e.
632632-- (1,4) will be scheduled in preference to (2,3).
test/Streamly/Test/Data/Unfold.hs+10
@@ -13,6 +13,7 @@ import Streamly.Internal.Data.Unfold (Unfold)
1313
1414import qualified Data.List as List
1515import qualified Prelude
16+import qualified Streamly.Internal.Data.Fold as Fold
1617import qualified Streamly.Internal.Data.Unfold as UF
1718import qualified Streamly.Internal.Data.Stream.IsStream as S
1819import qualified Streamly.Internal.Data.Stream.StreamD as D
@@ -449,6 +450,14 @@ enumerateFromToFractional =
449450-- Stream transformation
450451-------------------------------------------------------------------------------
451452
453+postscan :: Property
454+postscan =
455+ property
456+ $ \(ls :: [Int]) ->
457+ let unf = UF.postscan Fold.sum UF.fromList
458+ mList = scanl1 (+) ls
459+ in testUnfold unf ls mList
460+
452461mapM :: Property
453462mapM =
454463 property
@@ -655,6 +664,7 @@ testTransformation =
655664 describe "Transformation"
656665 $ do
657666 -- prop "map" map
667+ prop "postscan" postscan
658668 prop "mapM" mapM
659669 prop "mapMWithInput" mapMWithInput
660670 prop "takeWhileM" takeWhileM