on master help sign in
Rename unfold scan to postscan, add benchmark, test
3a59974Ranjeet Kumar Ranjan
benchmark/Streamly/Benchmark/Data/Unfold.hs+6
@@ -290,6 +290,11 @@ enumerateFromToFractional size start =
290-- Stream transformation
290-- Stream transformation
291-------------------------------------------------------------------------------
291-------------------------------------------------------------------------------
292
292
293+{-# INLINE postscan #-}
294+postscan :: Monad m => Int -> Int -> m ()
295+postscan size start =
296+ drainTransformationDefault (size + start) (UF.postscan FL.sum) start
297+
293{-# INLINE map #-}
298{-# INLINE map #-}
294map :: Monad m => Int -> Int -> m ()
299map :: Monad m => Int -> Int -> m ()
295map size start = drainTransformationDefault (size + start) (UF.map (+1)) start
300map size start = drainTransformationDefault (size + start) (UF.map (+1)) start
@@ -663,6 +668,7 @@ o_1_space_transformation size =
663 [ benchIO "map" $ map size
668 [ benchIO "map" $ map size
664 , benchIO "mapM" $ mapM size
669 , benchIO "mapM" $ mapM size
665 , benchIO "mapMWithInput" $ mapMWithInput size
670 , benchIO "mapMWithInput" $ mapMWithInput size
671+ , benchIO "postscan" $ postscan size
666 ]
672 ]
667 ]
673 ]
668
674
core/src/Streamly/Internal/Data/Unfold.hs+9−9
@@ -201,8 +201,8 @@ module Streamly.Internal.Data.Unfold
201 , map
201 , map
202 , mapM
202 , mapM
203 , mapMWithInput
203 , mapMWithInput
204− , scanlM'
204+ , postscanlM'
205− , scan
205+ , postscan
206 , foldMany
206 , foldMany
207 -- pipe
207 -- pipe
208
208
@@ -478,9 +478,9 @@ either (Unfold step1 inject1) = Unfold step inject
478-- | Scan the output of an 'Unfold' to change it in a stateful manner.
478-- | Scan the output of an 'Unfold' to change it in a stateful manner.
479--
479--
480-- /Pre-release/
480-- /Pre-release/
481−{-# INLINE_NORMAL scan #-}
481+{-# INLINE_NORMAL postscan #-}
482−scan :: Monad m => Fold m b c -> Unfold m a b -> Unfold m a c
482+postscan :: Monad m => Fold m b c -> Unfold m a b -> Unfold m a c
483−scan (Fold stepF initial extract) (Unfold stepU injectU) =
483+postscan (Fold stepF initial extract) (Unfold stepU injectU) =
484 Unfold step inject
484 Unfold step inject
485
485
486 where
486 where
@@ -509,10 +509,10 @@ scan (Fold stepF initial extract) (Unfold stepU injectU) =
509
509
510-- | Scan the output of an 'Unfold' to change it in a stateful manner.
510-- | Scan the output of an 'Unfold' to change it in a stateful manner.
511--
511--
512−-- /Unimplemented/
512+-- /Pre-release/
513−{-# INLINE_NORMAL scanlM' #-}
513+{-# INLINE_NORMAL postscanlM' #-}
514−scanlM' :: Monad m => (b -> a -> m b) -> m b -> Unfold m c a -> Unfold m c b
514+postscanlM' :: 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)
515+postscanlM' f z = postscan (FL.foldlM' f z)
516
516
517-------------------------------------------------------------------------------
517-------------------------------------------------------------------------------
518-- Convert streams into unfolds
518-- Convert streams into unfolds
docs/ConcurrentStreams.hs+3−3
@@ -624,9 +624,9 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
624-- Stream.fromEffect $ putStrLn $ show (x, y)
624-- Stream.fromEffect $ putStrLn $ show (x, y)
625-- :}
625-- :}
626-- (1,3)
626-- (1,3)
627−-- (1,4)
627+-- ...
628−-- (2,3)
628+-- ...
629−-- (2,4)
629+-- ...
630--
630--
631-- Nested iterations are given preference for concurrent evaluation i.e.
631-- Nested iterations are given preference for concurrent evaluation i.e.
632-- (1,4) will be scheduled in preference to (2,3).
632-- (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)
13
13
14import qualified Data.List as List
14import qualified Data.List as List
15import qualified Prelude
15import qualified Prelude
16+import qualified Streamly.Internal.Data.Fold as Fold
16import qualified Streamly.Internal.Data.Unfold as UF
17import qualified Streamly.Internal.Data.Unfold as UF
17import qualified Streamly.Internal.Data.Stream.IsStream as S
18import qualified Streamly.Internal.Data.Stream.IsStream as S
18import qualified Streamly.Internal.Data.Stream.StreamD as D
19import qualified Streamly.Internal.Data.Stream.StreamD as D
@@ -449,6 +450,14 @@ enumerateFromToFractional =
449-- Stream transformation
450-- Stream transformation
450-------------------------------------------------------------------------------
451-------------------------------------------------------------------------------
451
452
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+
452mapM :: Property
461mapM :: Property
453mapM =
462mapM =
454 property
463 property
@@ -655,6 +664,7 @@ testTransformation =
655 describe "Transformation"
664 describe "Transformation"
656 $ do
665 $ do
657 -- prop "map" map
666 -- prop "map" map
667+ prop "postscan" postscan
658 prop "mapM" mapM
668 prop "mapM" mapM
659 prop "mapMWithInput" mapMWithInput
669 prop "mapMWithInput" mapMWithInput
660 prop "takeWhileM" takeWhileM
670 prop "takeWhileM" takeWhileM