on master help sign in
Fix Some minor refactorings of toList/toStream folds #863
6976363Ranjeet Kumar Ranjan
benchmark/Streamly/Benchmark/Data/Fold.hs+1−1
@@ -291,7 +291,7 @@ o_n_heap_serial value =
291 -- as the structure cannot be lazily consumed until fully built.
291 -- as the structure cannot be lazily consumed until fully built.
292 [
292 [
293 benchIOSink value "toList" (S.fold FL.toList)
293 benchIOSink value "toList" (S.fold FL.toList)
294− , benchIOSink value "toListRevF" (S.fold FL.toListRevF)
294+ , benchIOSink value "toListRev" (S.fold FL.toListRev)
295 ]
295 ]
296 ]
296 ]
297
297
benchmark/Streamly/Benchmark/Prelude/Serial/Elimination.hs+5−5
@@ -36,12 +36,12 @@ import qualified Streamly.Internal.Data.Stream.StreamD as D
36
36
37import qualified Streamly.Prelude as S
37import qualified Streamly.Prelude as S
38import qualified Streamly.Internal.Data.Stream.IsStream as Internal
38import qualified Streamly.Internal.Data.Stream.IsStream as Internal
39+import qualified Streamly.Internal.Data.Fold as FL
39
40
40import Gauge
41import Gauge
41import Streamly.Prelude (SerialT, IsStream, serially)
42import Streamly.Prelude (SerialT, IsStream, serially)
42import Streamly.Benchmark.Common
43import Streamly.Benchmark.Common
43import Streamly.Benchmark.Prelude
44import Streamly.Benchmark.Prelude
44−import qualified Streamly.Internal.Data.Stream.IsStream as IP
45import Prelude hiding (length, sum, or, and, any, all, notElem, elem, (!!),
45import Prelude hiding (length, sum, or, and, any, all, notElem, elem, (!!),
46 lookup, repeat, minimum, maximum, product, last, mapM_, init)
46 lookup, repeat, minimum, maximum, product, last, mapM_, init)
47import qualified Prelude
47import qualified Prelude
@@ -539,9 +539,9 @@ o_n_heap_elimination_toList value =
539 [ bgroup "toList"
539 [ bgroup "toList"
540 -- Converting the stream to a list or pure stream in a strict monad
540 -- Converting the stream to a list or pure stream in a strict monad
541 [ benchIOSink value "toListRev" Internal.toListRev
541 [ benchIOSink value "toListRev" Internal.toListRev
542− , benchIOSink value "toPureRev" Internal.toPureRev
542+ , benchIOSink value "toStreamRev" Internal.toStreamRev
543− , benchIOSink value "toStream" (S.fold IP.toStream)
543+ , benchIOSink value "toStream" (S.fold FL.toStream)
544− , benchIOSink value "toStreamRev" (S.fold IP.toStreamRev)
544+ , benchIOSink value "toStreamRev" (S.fold FL.toStreamRev)
545 ]
545 ]
546 ]
546 ]
547
547
@@ -550,7 +550,7 @@ o_n_space_elimination_toList value =
550 [ bgroup "toList"
550 [ bgroup "toList"
551 -- Converting the stream to a list or pure stream in a strict monad
551 -- Converting the stream to a list or pure stream in a strict monad
552 [ benchIOSink value "toList" S.toList
552 [ benchIOSink value "toList" S.toList
553− , benchIOSink value "toPure" Internal.toPure
553+ , benchIOSink value "toStream" Internal.toStream
554 ]
554 ]
555 ]
555 ]
556
556
src/Streamly/Internal/Data/Fold.hs+37−7
@@ -65,8 +65,10 @@ module Streamly.Internal.Data.Fold
65
65
66 -- ** Collectors
66 -- ** Collectors
67 , toList
67 , toList
68− , toListRevF -- experimental
68+ , toListRev -- experimental
69− -- $toListRevF
69+ -- $toListRev
70+ , toStream
71+ , toStreamRev
70
72
71 -- * Terminating Folds
73 -- * Terminating Folds
72 , drainN
74 , drainN
@@ -108,6 +110,7 @@ module Streamly.Internal.Data.Fold
108 -- ** Mapping
110 -- ** Mapping
109 , transform
111 , transform
110 , map
112 , map
113+
111 --, lsequence
114 --, lsequence
112 , lmapM
115 , lmapM
113 , indexed
116 , indexed
@@ -239,6 +242,7 @@ import Streamly.Internal.Data.Stream.Serial (SerialT)
239import qualified Data.Map.Strict as Map
242import qualified Data.Map.Strict as Map
240import qualified Streamly.Internal.Data.Pipe.Types as Pipe
243import qualified Streamly.Internal.Data.Pipe.Types as Pipe
241import qualified Streamly.Internal.Data.Stream.IsStream.Enumeration as Stream
244import qualified Streamly.Internal.Data.Stream.IsStream.Enumeration as Stream
245+import qualified Streamly.Internal.Data.Stream.StreamK as K
242import qualified Prelude
246import qualified Prelude
243
247
244import Prelude hiding
248import Prelude hiding
@@ -704,9 +708,9 @@ foldMapM act = mkAccumM_ step (pure mempty)
704-- To Containers
708-- To Containers
705------------------------------------------------------------------------------
709------------------------------------------------------------------------------
706
710
707−-- $toListRevF
711+-- $toListRev
708-- This is more efficient than 'Streamly.Internal.Data.Fold.toList'. toList is
712-- 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'.
710
714
711-- | Buffers the input stream to a list in the reverse order of the input.
715-- | Buffers the input stream to a list in the reverse order of the input.
712--
716--
@@ -716,9 +720,9 @@ foldMapM act = mkAccumM_ step (pure mempty)
716-- @since 0.7.0
720-- @since 0.7.0
717
721
718-- xn : ... : x2 : x1 : []
722-- xn : ... : x2 : x1 : []
719−{-# INLINABLE toListRevF #-}
723+{-# INLINABLE toListRev #-}
720−toListRevF :: Monad m => Fold m a [a]
724+toListRev :: Monad m => Fold m a [a]
721−toListRevF = mkAccum_ (flip (:)) []
725+toListRev = mkAccum_ (flip (:)) []
722
726
723------------------------------------------------------------------------------
727------------------------------------------------------------------------------
724-- Partial Folds
728-- Partial Folds
@@ -1782,3 +1786,29 @@ concatSequence _f _p = undefined
1782chunksBetween :: -- Monad m =>
1786chunksBetween :: -- Monad m =>
1783 Int -> Int -> Fold m a b -> Fold m b c -> Fold m a c
1787 Int -> Int -> Fold m a b -> Fold m b c -> Fold m a c
1784chunksBetween _low _high _f1 _f2 = undefined
1788chunksBetween _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
43 , foldl1'
43 , foldl1'
44 , foldlM'
44 , foldlM'
45
45
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−
51 -- * Specific Fold Functions
46 -- * Specific Fold Functions
52 -- | Folds as functions of the shape @t m a -> m b@.
47 -- | Folds as functions of the shape @t m a -> m b@.
53 --
48 --
@@ -111,8 +106,8 @@ module Streamly.Internal.Data.Stream.IsStream.Eliminate
111 -- ** To Containers
106 -- ** To Containers
112 , toList
107 , toList
113 , toListRev
108 , toListRev
114− , toPure -- XXX move toStream to Data.Fold and rename this to toStream?
109+ , toStream
115− , toPureRev
110+ , toStreamRev
116
111
117 -- * Concurrent Folds
112 -- * Concurrent Folds
118 , foldAsync
113 , foldAsync
@@ -152,7 +147,6 @@ import Control.Monad.Catch (MonadThrow)
152import Control.Monad.IO.Class (MonadIO(..))
147import Control.Monad.IO.Class (MonadIO(..))
153import Data.Functor.Identity (Identity (..))
148import Data.Functor.Identity (Identity (..))
154import Foreign.Storable (Storable)
149import Foreign.Storable (Storable)
155−import Streamly.Internal.Data.Fold.Types (Fold (..))
156import Streamly.Internal.Data.Parser (Parser (..))
150import Streamly.Internal.Data.Parser (Parser (..))
157import Streamly.Internal.Data.SVar (MonadAsync, defState)
151import Streamly.Internal.Data.SVar (MonadAsync, defState)
158import Streamly.Internal.Data.Stream.IsStream.Common
152import Streamly.Internal.Data.Stream.IsStream.Common
@@ -763,58 +757,29 @@ toHandle h = go
763 yieldk a r = liftIO (IO.hPutStrLn h a) >> go r
757 yieldk a r = liftIO (IO.hPutStrLn h a) >> go r
764 in K.foldStream defState yieldk single stop m1
758 in K.foldStream defState yieldk single stop m1
765
759
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−
795-- | Convert a stream to a pure stream.
760-- | Convert a stream to a pure stream.
796--
761--
797-- @
762-- @
798−-- toPure = foldr cons nil
763+-- toStream = foldr cons nil
799-- @
764-- @
800--
765--
801-- /Internal/
766-- /Internal/
802--
767--
803−{-# INLINE toPure #-}
768+{-# INLINE toStream #-}
804−toPure :: Monad m => SerialT m a -> m (SerialT Identity a)
769+toStream :: Monad m => SerialT m a -> m (SerialT Identity a)
805−toPure = foldr K.cons K.nil
770+toStream = foldr K.cons K.nil
806
771
807-- | Convert a stream to a pure stream in reverse order.
772-- | Convert a stream to a pure stream in reverse order.
808--
773--
809-- @
774-- @
810−-- toPureRev = foldl' (flip cons) nil
775+-- toStreamRev = foldl' (flip cons) nil
811-- @
776-- @
812--
777--
813-- /Internal/
778-- /Internal/
814--
779--
815−{-# INLINE toPureRev #-}
780+{-# INLINE toStreamRev #-}
816−toPureRev :: Monad m => SerialT m a -> m (SerialT Identity a)
781+toStreamRev :: Monad m => SerialT m a -> m (SerialT Identity a)
817−toPureRev = foldl' (flip K.cons) K.nil
782+toStreamRev = foldl' (flip K.cons) K.nil
818
783
819------------------------------------------------------------------------------
784------------------------------------------------------------------------------
820-- Concurrent Application
785-- Concurrent Application