@@ -1,5 +1,5 @@
2−-- Module : Streamly.Internal.Data.Consumer.Type
2+-- Module : Streamly.Internal.Data.Refold.Type
3-- Copyright : (c) 2019 Composewell Technologies
3-- Copyright : (c) 2019 Composewell Technologies
4-- License : BSD-3-Clause
4-- License : BSD-3-Clause
5-- Maintainer : streamly@composewell.com
5-- Maintainer : streamly@composewell.com
@@ -7,29 +7,29 @@
7-- Portability : GHC
7-- Portability : GHC
9-- The 'Fold' type embeds a default initial value, therefore, it is like a
9-- The 'Fold' type embeds a default initial value, therefore, it is like a
10−-- 'Monoid' whereas the 'Consumer' type has to be supplied with an initial
10+-- 'Monoid' whereas the 'Refold' type has to be supplied with an initial
11-- value, therefore, it is more like a 'Semigroup' operation.
11-- value, therefore, it is more like a 'Semigroup' operation.
13−-- Consumers can be appended to each other or to a fold to build the fold
13+-- Refolds can be appended to each other or to a fold to build the fold
14-- incrementally. This is useful in incremental builder like use cases.
14-- incrementally. This is useful in incremental builder like use cases.
16-- See the file splitting example in the @streamly-examples@ repository for an
16-- See the file splitting example in the @streamly-examples@ repository for an
17−-- application of the 'Consumer' type. The 'Fold' type does not perform as well
17+-- application of the 'Refold' type. The 'Fold' type does not perform as well
18-- in this situation.
18-- in this situation.
20−-- 'Consumer' type is to 'Fold' as 'Unfold' type is to 'Stream'. Like 'Unfold'
20+-- 'Refold' type is to 'Fold' as 'Unfold' type is to 'Stream'. 'Unfold'
21−-- provides better optimizaiton than stream in nested operations similarly
21+-- provides better optimizaiton than stream in nested operations, similarly,
22−-- 'Consumer' provides better optimization than 'Fold'.
22+-- 'Refold' provides better optimization than 'Fold'.
24−module Streamly.Internal.Data.Consumer.Type
24+module Streamly.Internal.Data.Refold.Type
26 -- * Types
26 -- * Types
27− Consumer (..)
27+ Refold (..)
29 -- * Constructors
29 -- * Constructors
32− -- * Consumers
32+ -- * Refolds
33 -- ** Accumulators
33 -- ** Accumulators
@@ -54,11 +54,11 @@ import Prelude hiding (take, iterate)
57−-- >>> import qualified Streamly.Internal.Data.Consumer.Type as Consumer
57+-- >>> import qualified Streamly.Internal.Data.Refold.Type as Refold
58-- >>> import qualified Streamly.Internal.Data.Fold.Type as Fold
58-- >>> import qualified Streamly.Internal.Data.Fold.Type as Fold
59-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream
59-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream
61−-- All folds in the Fold module should be implemented using Consumers.
61+-- All folds in the Fold module should be implemented using Refolds.
63-- | Like 'Fold' except that the initial state of the accmulator can be
63-- | Like 'Fold' except that the initial state of the accmulator can be
64-- generated using a dynamically supplied input. This affords better stream
64-- generated using a dynamically supplied input. This affords better stream
@@ -66,9 +66,9 @@ import Prelude hiding (take, iterate)
66-- is determined based on a dynamic value.
66-- is determined based on a dynamic value.
68-- /Internal/
68-- /Internal/
69−data Consumer m c a b =
69+data Refold m c a b =
70 -- | @Fold @ @ step @ @ inject @ @ extract@
70 -- | @Fold @ @ step @ @ inject @ @ extract@
71− forall s. Consumer (s -> a -> m (Step s b)) (c -> m (Step s b)) (s -> m b)
71+ forall s. Refold (s -> a -> m (Step s b)) (c -> m (Step s b)) (s -> m b)
73------------------------------------------------------------------------------
73------------------------------------------------------------------------------
74-- Left fold constructors
74-- Left fold constructors
@@ -84,9 +84,9 @@ data Consumer m c a b =
84-- /Internal/
84-- /Internal/
86{-# INLINE foldl' #-}
86{-# INLINE foldl' #-}
87−foldl' :: Monad m => (b -> a -> b) -> Consumer m b a b
87+foldl' :: Monad m => (b -> a -> b) -> Refold m b a b
88foldl' step =
88foldl' step =
90 (\s a -> return $ Partial $ step s a)
90 (\s a -> return $ Partial $ step s a)
91 (return . Partial)
91 (return . Partial)
@@ -99,8 +99,8 @@ foldl' step =
100-- /Internal/
100-- /Internal/
101{-# INLINE lmapM #-}
101{-# INLINE lmapM #-}
102−lmapM :: Monad m => (a -> m b) -> Consumer m c b r -> Consumer m c a r
102+lmapM :: Monad m => (a -> m b) -> Refold m c b r -> Refold m c a r
103−lmapM f (Consumer step inject extract) = Consumer step1 inject extract
103+lmapM f (Refold step inject extract) = Refold step1 inject extract
@@ -114,8 +114,8 @@ lmapM f (Consumer step inject extract) = Consumer step1 inject extract
115-- /Internal/
115-- /Internal/
116{-# INLINE rmapM #-}
116{-# INLINE rmapM #-}
117−rmapM :: Monad m => (b -> m c) -> Consumer m x a b -> Consumer m x a c
117+rmapM :: Monad m => (b -> m c) -> Refold m x a b -> Refold m x a c
118−rmapM f (Consumer step inject extract) = Consumer step1 inject1 (extract >=> f)
118+rmapM f (Refold step inject extract) = Refold step1 inject1 (extract >=> f)
@@ -123,15 +123,15 @@ rmapM f (Consumer step inject extract) = Consumer step1 inject1 (extract >=> f)
123 step1 s a = step s a >>= mapMStep f
123 step1 s a = step s a >>= mapMStep f
125------------------------------------------------------------------------------
125------------------------------------------------------------------------------
126−-- Consumers
126+-- Refolds
127------------------------------------------------------------------------------
127------------------------------------------------------------------------------
131-- /Internal/
131-- /Internal/
132{-# INLINE drainBy #-}
132{-# INLINE drainBy #-}
133−drainBy :: Monad m => (c -> a -> m b) -> Consumer m c a ()
133+drainBy :: Monad m => (c -> a -> m b) -> Refold m c a ()
134−drainBy f = Consumer step inject extract
134+drainBy f = Refold step inject extract
@@ -148,14 +148,14 @@ drainBy f = Consumer step inject extract
148-- | Append the elements of an input stream to a provided starting value.
148-- | Append the elements of an input stream to a provided starting value.
150-- >>> stream = Stream.map Data.Monoid.Sum $ Stream.enumerateFromTo 1 10
150-- >>> stream = Stream.map Data.Monoid.Sum $ Stream.enumerateFromTo 1 10
151−-- >>> Stream.fold (Fold.fromConsumer Consumer.sconcat 10) stream
151+-- >>> Stream.fold (Fold.fromRefold Refold.sconcat 10) stream
152-- Sum {getSum = 65}
152-- Sum {getSum = 65}
154−-- >>> sconcat = Consumer.foldl' (<>)
154+-- >>> sconcat = Refold.foldl' (<>)
156-- /Internal/
156-- /Internal/
157{-# INLINE sconcat #-}
157{-# INLINE sconcat #-}
158−sconcat :: (Monad m, Semigroup a) => Consumer m a a a
158+sconcat :: (Monad m, Semigroup a) => Refold m a a a
159sconcat = foldl' (<>)
159sconcat = foldl' (<>)
161------------------------------------------------------------------------------
161------------------------------------------------------------------------------
@@ -166,9 +166,9 @@ sconcat = foldl' (<>)
167-- /Internal/
167-- /Internal/
168{-# INLINE append #-}
168{-# INLINE append #-}
169−append :: Monad m => Consumer m x a b -> Consumer m b a b -> Consumer m x a b
169+append :: Monad m => Refold m x a b -> Refold m b a b -> Refold m x a b
170−append (Consumer step1 inject1 extract1) (Consumer step2 inject2 extract2) =
170+append (Refold step1 inject1 extract1) (Refold step2 inject2 extract2) =
171− Consumer step inject extract
171+ Refold step inject extract
@@ -198,9 +198,9 @@ append (Consumer step1 inject1 extract1) (Consumer step2 inject2 extract2) =
198-- the output of the previous run to the next.
198-- the output of the previous run to the next.
200-- /Internal/
200-- /Internal/
201−iterate :: Monad m => Consumer m b a b -> Consumer m b a b
201+iterate :: Monad m => Refold m b a b -> Refold m b a b
202−iterate (Consumer step1 inject1 extract1) =
202+iterate (Refold step1 inject1 extract1) =
203− Consumer step inject extract1
203+ Refold step inject extract1
@@ -226,8 +226,8 @@ data Tuple'Fused a b = Tuple'Fused !a !b deriving Show
227-- /Internal/
227-- /Internal/
228{-# INLINE take #-}
228{-# INLINE take #-}
229−take :: Monad m => Int -> Consumer m x a b -> Consumer m x a b
229+take :: Monad m => Int -> Refold m x a b -> Refold m x a b
230−take n (Consumer fstep finject fextract) = Consumer step inject extract
230+take n (Refold fstep finject fextract) = Refold step inject extract