@@ -1,5 +1,5 @@
11-- |
2−-- Module : Streamly.Internal.Data.Consumer.Type
2+-- Module : Streamly.Internal.Data.Refold.Type
33-- Copyright : (c) 2019 Composewell Technologies
44-- License : BSD-3-Clause
55-- Maintainer : streamly@composewell.com
@@ -7,29 +7,29 @@
77-- Portability : GHC
88--
99-- 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
1111-- value, therefore, it is more like a 'Semigroup' operation.
1212--
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
1414-- incrementally. This is useful in incremental builder like use cases.
1515--
1616-- 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
1818-- in this situation.
1919--
20−-- 'Consumer' type is to 'Fold' as 'Unfold' type is to 'Stream'. Like 'Unfold'
21−-- provides better optimizaiton than stream in nested operations similarly
22−-- 'Consumer' provides better optimization than 'Fold'.
20+-- 'Refold' type is to 'Fold' as 'Unfold' type is to 'Stream'. 'Unfold'
21+-- provides better optimizaiton than stream in nested operations, similarly,
22+-- 'Refold' provides better optimization than 'Fold'.
2323--
24−module Streamly.Internal.Data.Consumer.Type
24+module Streamly.Internal.Data.Refold.Type
2525 (
2626 -- * Types
27− Consumer (..)
27+ Refold (..)
2828
2929 -- * Constructors
3030 , foldl'
3131
32− -- * Consumers
32+ -- * Refolds
3333 -- ** Accumulators
3434 , sconcat
3535 , drainBy
@@ -54,11 +54,11 @@ import Prelude hiding (take, iterate)
5454
5555-- $setup
5656-- >>> :m
57−-- >>> import qualified Streamly.Internal.Data.Consumer.Type as Consumer
57+-- >>> import qualified Streamly.Internal.Data.Refold.Type as Refold
5858-- >>> import qualified Streamly.Internal.Data.Fold.Type as Fold
5959-- >>> import qualified Streamly.Internal.Data.Stream.IsStream as Stream
6060
61−-- All folds in the Fold module should be implemented using Consumers.
61+-- All folds in the Fold module should be implemented using Refolds.
6262--
6363-- | Like 'Fold' except that the initial state of the accmulator can be
6464-- generated using a dynamically supplied input. This affords better stream
@@ -66,9 +66,9 @@ import Prelude hiding (take, iterate)
6666-- is determined based on a dynamic value.
6767--
6868-- /Internal/
69−data Consumer m c a b =
69+data Refold m c a b =
7070 -- | @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)
7272
7373------------------------------------------------------------------------------
7474-- Left fold constructors
@@ -84,9 +84,9 @@ data Consumer m c a b =
8484-- /Internal/
8585--
8686{-# 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
8888foldl' step =
89− Consumer
89+ Refold
9090 (\s a -> return $ Partial $ step s a)
9191 (return . Partial)
9292 return
@@ -99,8 +99,8 @@ foldl' step =
9999--
100100-- /Internal/
101101{-# INLINE lmapM #-}
102−lmapM :: Monad m => (a -> m b) -> Consumer m c b r -> Consumer m c a r
103−lmapM f (Consumer step inject extract) = Consumer step1 inject extract
102+lmapM :: Monad m => (a -> m b) -> Refold m c b r -> Refold m c a r
103+lmapM f (Refold step inject extract) = Refold step1 inject extract
104104
105105 where
106106
@@ -114,8 +114,8 @@ lmapM f (Consumer step inject extract) = Consumer step1 inject extract
114114--
115115-- /Internal/
116116{-# INLINE rmapM #-}
117−rmapM :: Monad m => (b -> m c) -> Consumer m x a b -> Consumer m x a c
118−rmapM f (Consumer step inject extract) = Consumer step1 inject1 (extract >=> f)
117+rmapM :: Monad m => (b -> m c) -> Refold m x a b -> Refold m x a c
118+rmapM f (Refold step inject extract) = Refold step1 inject1 (extract >=> f)
119119
120120 where
121121
@@ -123,15 +123,15 @@ rmapM f (Consumer step inject extract) = Consumer step1 inject1 (extract >=> f)
123123 step1 s a = step s a >>= mapMStep f
124124
125125------------------------------------------------------------------------------
126−-- Consumers
126+-- Refolds
127127------------------------------------------------------------------------------
128128
129129-- |
130130--
131131-- /Internal/
132132{-# INLINE drainBy #-}
133−drainBy :: Monad m => (c -> a -> m b) -> Consumer m c a ()
134−drainBy f = Consumer step inject extract
133+drainBy :: Monad m => (c -> a -> m b) -> Refold m c a ()
134+drainBy f = Refold step inject extract
135135
136136 where
137137
@@ -148,14 +148,14 @@ drainBy f = Consumer step inject extract
148148-- | Append the elements of an input stream to a provided starting value.
149149--
150150-- >>> 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
152152-- Sum {getSum = 65}
153153--
154−-- >>> sconcat = Consumer.foldl' (<>)
154+-- >>> sconcat = Refold.foldl' (<>)
155155--
156156-- /Internal/
157157{-# INLINE sconcat #-}
158−sconcat :: (Monad m, Semigroup a) => Consumer m a a a
158+sconcat :: (Monad m, Semigroup a) => Refold m a a a
159159sconcat = foldl' (<>)
160160
161161------------------------------------------------------------------------------
@@ -166,9 +166,9 @@ sconcat = foldl' (<>)
166166--
167167-- /Internal/
168168{-# INLINE append #-}
169−append :: Monad m => Consumer m x a b -> Consumer m b a b -> Consumer m x a b
170−append (Consumer step1 inject1 extract1) (Consumer step2 inject2 extract2) =
171− Consumer step inject extract
169+append :: Monad m => Refold m x a b -> Refold m b a b -> Refold m x a b
170+append (Refold step1 inject1 extract1) (Refold step2 inject2 extract2) =
171+ Refold step inject extract
172172
173173 where
174174
@@ -198,9 +198,9 @@ append (Consumer step1 inject1 extract1) (Consumer step2 inject2 extract2) =
198198-- the output of the previous run to the next.
199199--
200200-- /Internal/
201−iterate :: Monad m => Consumer m b a b -> Consumer m b a b
202−iterate (Consumer step1 inject1 extract1) =
203− Consumer step inject extract1
201+iterate :: Monad m => Refold m b a b -> Refold m b a b
202+iterate (Refold step1 inject1 extract1) =
203+ Refold step inject extract1
204204
205205 where
206206
@@ -226,8 +226,8 @@ data Tuple'Fused a b = Tuple'Fused !a !b deriving Show
226226--
227227-- /Internal/
228228{-# INLINE take #-}
229−take :: Monad m => Int -> Consumer m x a b -> Consumer m x a b
230−take n (Consumer fstep finject fextract) = Consumer step inject extract
229+take :: Monad m => Int -> Refold m x a b -> Refold m x a b
230+take n (Refold fstep finject fextract) = Refold step inject extract
231231
232232 where
233233