on master help sign in
Fix Some minor refactorings of toList/toStream folds #863

Fix refactoring issues in benchmark Remove redundant import Address review comments Revert haddock reference

commit6976363939f21497147369d6d35a3a3a64010a1c authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateWed 10 Feb 2021, 15:14 (6 years ago) parent11831be
4 files changed+53−58
benchmark/Streamly/Benchmark/Data/Fold.hs+1−1 benchmark/Streamly/Benchmark/Prelude/Serial/Elimination.hs+5−5 src/Streamly/Internal/Data/Fold.hs+37−7 src/Streamly/Internal/Data/Stream/IsStream/Eliminate.hs+10−45
benchmark/Streamly/Benchmark/Data/Fold.hs+1−1
@@ -291,7 +291,7 @@ o_n_heap_serial value =
291291 -- as the structure cannot be lazily consumed until fully built.
292292 [
293293 benchIOSink value "toList" (S.fold FL.toList)
294− , benchIOSink value "toListRevF" (S.fold FL.toListRevF)
294+ , benchIOSink value "toListRev" (S.fold FL.toListRev)
295295 ]
296296 ]
297297
benchmark/Streamly/Benchmark/Prelude/Serial/Elimination.hs+5−5
@@ -36,12 +36,12 @@ import qualified Streamly.Internal.Data.Stream.StreamD as D
3636
3737import qualified Streamly.Prelude as S
3838import qualified Streamly.Internal.Data.Stream.IsStream as Internal
39+import qualified Streamly.Internal.Data.Fold as FL
3940
4041import Gauge
4142import Streamly.Prelude (SerialT, IsStream, serially)
4243import Streamly.Benchmark.Common
4344import Streamly.Benchmark.Prelude
44−import qualified Streamly.Internal.Data.Stream.IsStream as IP
4545import Prelude hiding (length, sum, or, and, any, all, notElem, elem, (!!),
4646 lookup, repeat, minimum, maximum, product, last, mapM_, init)
4747import qualified Prelude
@@ -539,9 +539,9 @@ o_n_heap_elimination_toList value =
539539 [ bgroup "toList"
540540 -- Converting the stream to a list or pure stream in a strict monad
541541 [ benchIOSink value "toListRev" Internal.toListRev
542− , benchIOSink value "toPureRev" Internal.toPureRev
543− , benchIOSink value "toStream" (S.fold IP.toStream)
544− , benchIOSink value "toStreamRev" (S.fold IP.toStreamRev)
542+ , benchIOSink value "toStreamRev" Internal.toStreamRev
543+ , benchIOSink value "toStream" (S.fold FL.toStream)
544+ , benchIOSink value "toStreamRev" (S.fold FL.toStreamRev)
545545 ]
546546 ]
547547
@@ -550,7 +550,7 @@ o_n_space_elimination_toList value =
550550 [ bgroup "toList"
551551 -- Converting the stream to a list or pure stream in a strict monad
552552 [ benchIOSink value "toList" S.toList
553− , benchIOSink value "toPure" Internal.toPure
553+ , benchIOSink value "toStream" Internal.toStream
554554 ]
555555 ]
556556
src/Streamly/Internal/Data/Fold.hs+37−7
@@ -65,8 +65,10 @@ module Streamly.Internal.Data.Fold
6565
6666 -- ** Collectors
6767 , toList
68− , toListRevF -- experimental
69− -- $toListRevF
68+ , toListRev -- experimental
69+ -- $toListRev
70+ , toStream
71+ , toStreamRev
7072
7173 -- * Terminating Folds
7274 , drainN
@@ -108,6 +110,7 @@ module Streamly.Internal.Data.Fold
108110 -- ** Mapping
109111 , transform
110112 , map
113+
111114 --, lsequence
112115 , lmapM
113116 , indexed
@@ -239,6 +242,7 @@ import Streamly.Internal.Data.Stream.Serial (SerialT)
239242import qualified Data.Map.Strict as Map
240243import qualified Streamly.Internal.Data.Pipe.Types as Pipe
241244import qualified Streamly.Internal.Data.Stream.IsStream.Enumeration as Stream
245+import qualified Streamly.Internal.Data.Stream.StreamK as K
242246import qualified Prelude
243247
244248import Prelude hiding
@@ -704,9 +708,9 @@ foldMapM act = mkAccumM_ step (pure mempty)
704708-- To Containers
705709------------------------------------------------------------------------------
706710
707−-- $toListRevF
711+-- $toListRev
708712-- This is more efficient than 'Streamly.Internal.Data.Fold.toList'. toList is
709−-- exactly the same as reversing the list after 'toListRevF'.
713+-- exactly the same as reversing the list after 'toListRev'.
710714
711715-- | Buffers the input stream to a list in the reverse order of the input.
712716--
@@ -716,9 +720,9 @@ foldMapM act = mkAccumM_ step (pure mempty)
716720-- @since 0.7.0
717721
718722-- xn : ... : x2 : x1 : []
719−{-# INLINABLE toListRevF #-}
720−toListRevF :: Monad m => Fold m a [a]
721−toListRevF = mkAccum_ (flip (:)) []
723+{-# INLINABLE toListRev #-}
724+toListRev :: Monad m => Fold m a [a]
725+toListRev = mkAccum_ (flip (:)) []
722726
723727------------------------------------------------------------------------------
724728-- Partial Folds
@@ -1782,3 +1786,29 @@ concatSequence _f _p = undefined
17821786chunksBetween :: -- Monad m =>
17831787 Int -> Int -> Fold m a b -> Fold m b c -> Fold m a c
17841788chunksBetween _low _high _f1 _f2 = undefined
1789+
1790+-- | A fold that buffers its input to a pure stream.
1791+--
1792+-- /Warning!/ working on large streams accumulated as buffers in memory could
1793+-- be very inefficient, consider using "Streamly.Data.Array" instead.
1794+--
1795+-- /Internal/
1796+{-# INLINE toStream #-}
1797+toStream :: Monad m => Fold m a (SerialT Identity a)
1798+toStream = mkAccum (\f x -> f . (x `K.cons`)) id ($ K.nil)
1799+
1800+-- This is more efficient than 'toStream'. toStream is exactly the same as
1801+-- reversing the stream after toStreamRev.
1802+--
1803+-- | Buffers the input stream to a pure stream in the reverse order of the
1804+-- input.
1805+--
1806+-- /Warning!/ working on large streams accumulated as buffers in memory could
1807+-- be very inefficient, consider using "Streamly.Data.Array" instead.
1808+--
1809+-- /Internal/
1810+
1811+-- xn : ... : x2 : x1 : []
1812+{-# INLINABLE toStreamRev #-}
1813+toStreamRev :: Monad m => Fold m a (SerialT Identity a)
1814+toStreamRev = mkAccum_ (flip K.cons) K.nil
src/Streamly/Internal/Data/Stream/IsStream/Eliminate.hs+10−45
@@ -43,11 +43,6 @@ module Streamly.Internal.Data.Stream.IsStream.Eliminate
4343 , foldl1'
4444 , foldlM'
4545
46− -- * Specific 'Fold's
47− -- XXX Move to Data.Fold?
48− , toStream -- XXX rename to write? buffer?
49− , toStreamRev -- XXX rename to writeRev? bufferRev?
50−
5146 -- * Specific Fold Functions
5247 -- | Folds as functions of the shape @t m a -> m b@.
5348 --
@@ -111,8 +106,8 @@ module Streamly.Internal.Data.Stream.IsStream.Eliminate
111106 -- ** To Containers
112107 , toList
113108 , toListRev
114− , toPure -- XXX move toStream to Data.Fold and rename this to toStream?
115− , toPureRev
109+ , toStream
110+ , toStreamRev
116111
117112 -- * Concurrent Folds
118113 , foldAsync
@@ -152,7 +147,6 @@ import Control.Monad.Catch (MonadThrow)
152147import Control.Monad.IO.Class (MonadIO(..))
153148import Data.Functor.Identity (Identity (..))
154149import Foreign.Storable (Storable)
155−import Streamly.Internal.Data.Fold.Types (Fold (..))
156150import Streamly.Internal.Data.Parser (Parser (..))
157151import Streamly.Internal.Data.SVar (MonadAsync, defState)
158152import Streamly.Internal.Data.Stream.IsStream.Common
@@ -763,58 +757,29 @@ toHandle h = go
763757 yieldk a r = liftIO (IO.hPutStrLn h a) >> go r
764758 in K.foldStream defState yieldk single stop m1
765759
766−-- XXX rename these to write/writeRev to make the naming consistent with folds
767−-- in other modules.
768−--
769−-- | A fold that buffers its input to a pure stream.
770−--
771−-- /Warning!/ working on large streams accumulated as buffers in memory could
772−-- be very inefficient, consider using "Streamly.Data.Array" instead.
773−--
774−-- /Internal/
775−{-# INLINE toStream #-}
776−toStream :: Monad m => Fold m a (SerialT Identity a)
777−toStream = FL.mkAccum (\f x -> f . (x `K.cons`)) id ($ K.nil)
778−
779−-- This is more efficient than 'toStream'. toStream is exactly the same as
780−-- reversing the stream after toStreamRev.
781−--
782−-- | Buffers the input stream to a pure stream in the reverse order of the
783−-- input.
784−--
785−-- /Warning!/ working on large streams accumulated as buffers in memory could
786−-- be very inefficient, consider using "Streamly.Data.Array" instead.
787−--
788−-- /Internal/
789−
790−-- xn : ... : x2 : x1 : []
791−{-# INLINABLE toStreamRev #-}
792−toStreamRev :: Monad m => Fold m a (SerialT Identity a)
793−toStreamRev = FL.mkAccum_ (flip K.cons) K.nil
794−
795760-- | Convert a stream to a pure stream.
796761--
797762-- @
798−-- toPure = foldr cons nil
763+-- toStream = foldr cons nil
799764-- @
800765--
801766-- /Internal/
802767--
803−{-# INLINE toPure #-}
804−toPure :: Monad m => SerialT m a -> m (SerialT Identity a)
805−toPure = foldr K.cons K.nil
768+{-# INLINE toStream #-}
769+toStream :: Monad m => SerialT m a -> m (SerialT Identity a)
770+toStream = foldr K.cons K.nil
806771
807772-- | Convert a stream to a pure stream in reverse order.
808773--
809774-- @
810−-- toPureRev = foldl' (flip cons) nil
775+-- toStreamRev = foldl' (flip cons) nil
811776-- @
812777--
813778-- /Internal/
814779--
815−{-# INLINE toPureRev #-}
816−toPureRev :: Monad m => SerialT m a -> m (SerialT Identity a)
817−toPureRev = foldl' (flip K.cons) K.nil
780+{-# INLINE toStreamRev #-}
781+toStreamRev :: Monad m => SerialT m a -> m (SerialT Identity a)
782+toStreamRev = foldl' (flip K.cons) K.nil
818783
819784------------------------------------------------------------------------------
820785-- Concurrent Application