on master help sign in
Rename Fold2 to Consumer
commitea2367875458467ce0df7f0560a5b8f6b71407ae authorHarendra Kumar <harendra@composewell.com> dateFri 8 Oct 2021, 16:53 (5 years ago) parent3cda01f
6 files changed+35−32
src/Streamly/Internal/Data/Consumer/Type.hs+15−3 src/Streamly/Internal/Data/Fold.hs−11 src/Streamly/Internal/Data/Fold/Type.hs+8−8 src/Streamly/Internal/Data/Stream/IsStream/Reduce.hs+3−2 src/Streamly/Internal/Data/Stream/StreamD/Type.hs+4−3 src/Streamly/Internal/FileSystem/Handle.hs+5−5
src/Streamly/Internal/Data/Consumer/Type.hs+15−3
@@ -11,10 +11,15 @@
1111module Streamly.Internal.Data.Consumer.Type
1212 (
1313 -- * Types
14− Fold2 (..)
14+ Consumer (..)
15+
16+ -- Consumers
17+ , drainBy
1518 )
1619where
1720
21+import Control.Monad (void)
22+
1823-- All folds in the Fold module should be implemented as Refolds.
1924--
2025-- | Experimental type to provide a side input to the fold for generating the
@@ -23,6 +28,13 @@ where
2328-- monadic action. This is a generalized version of 'Fold'.
2429--
2530-- /Internal/
26−data Fold2 m c a b =
31+data Consumer m c a b =
2732 -- | @Fold @ @ step @ @ inject @ @ extract@
28− forall s. Fold2 (s -> a -> m s) (c -> m s) (s -> m b)
33+ forall s. Consumer (s -> a -> m s) (c -> m s) (s -> m b)
34+
35+-- |
36+--
37+-- /Internal/
38+{-# INLINABLE drainBy #-}
39+drainBy :: Monad m => (a -> m b) -> Consumer m c a ()
40+drainBy f = Consumer (const (void . f)) (\_ -> return ()) return
src/Streamly/Internal/Data/Fold.hs−11
@@ -234,16 +234,12 @@ module Streamly.Internal.Data.Fold
234234 , duplicate
235235 , finish
236236
237− -- * Fold2
238− , drainBy2
239−
240237 -- * Deprecated
241238 , sequence
242239 , mapM
243240 )
244241where
245242
246−import Control.Monad (void)
247243import Data.Bifunctor (first)
248244import Data.Either (isLeft, isRight)
249245import Data.Functor.Identity (Identity(..))
@@ -416,13 +412,6 @@ scan = undefined
416412drainBy :: Monad m => (a -> m b) -> Fold m a ()
417413drainBy f = lmapM f drain
418414
419−-- |
420−--
421−-- /Internal/
422−{-# INLINABLE drainBy2 #-}
423−drainBy2 :: Monad m => (a -> m b) -> Fold2 m c a ()
424−drainBy2 f = Fold2 (const (void . f)) (\_ -> return ()) return
425−
426415-- | Extract the last element of the input stream, if any.
427416--
428417-- > last = fmap getLast $ Fold.foldMap (Last . Just)
src/Streamly/Internal/Data/Fold/Type.hs+8−8
@@ -259,8 +259,7 @@ module Streamly.Internal.Data.Fold.Type
259259 , duplicate
260260 , finish
261261
262− -- * Fold2
263− , Fold2 (..)
262+ -- * Consumer
264263 , simplify
265264 , chunksOf2
266265 )
@@ -272,7 +271,7 @@ import Data.Maybe (isJust, fromJust)
272271import Fusion.Plugin.Types (Fuse(..))
273272import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
274273import Streamly.Internal.Data.Tuple.Strict (Tuple'(..), Tuple3'(..))
275−import Streamly.Internal.Data.Consumer.Type (Fold2(..))
274+import Streamly.Internal.Data.Consumer.Type (Consumer(..))
276275
277276import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
278277
@@ -577,8 +576,8 @@ mkFoldM_ step initial = mkFoldM step initial return
577576-- | Convert more general type 'Fold2' into a simpler type 'Fold'
578577--
579578-- /Internal/
580−simplify :: Functor m => Fold2 m c a b -> c -> Fold m a b
581−simplify (Fold2 step inject extract) c =
579+simplify :: Functor m => Consumer m c a b -> c -> Fold m a b
580+simplify (Consumer step inject extract) c =
582581 Fold (\x a -> Partial <$> step x a) (Partial <$> inject c) extract
583582
584583------------------------------------------------------------------------------
@@ -1379,9 +1378,10 @@ chunksOf n split = many (take n split)
13791378--
13801379-- /Internal/
13811380{-# INLINE chunksOf2 #-}
1382−chunksOf2 :: Monad m => Int -> Fold m a b -> Fold2 m x b c -> Fold2 m x a c
1383−chunksOf2 n (Fold step1 initial1 extract1) (Fold2 step2 inject2 extract2) =
1384− Fold2 step' inject' extract'
1381+chunksOf2 :: Monad m =>
1382+ Int -> Fold m a b -> Consumer m x b c -> Consumer m x a c
1383+chunksOf2 n (Fold step1 initial1 extract1) (Consumer step2 inject2 extract2) =
1384+ Consumer step' inject' extract'
13851385
13861386 where
13871387
src/Streamly/Internal/Data/Stream/IsStream/Reduce.hs+3−2
@@ -155,7 +155,8 @@ import Data.Kind (Type)
155155import Data.Maybe (isNothing)
156156import Foreign.Storable (Storable)
157157import Streamly.Internal.Control.Concurrent (MonadAsync)
158−import Streamly.Internal.Data.Fold.Type (Fold (..), Fold2 (..))
158+import Streamly.Internal.Data.Fold.Type (Fold (..))
159+import Streamly.Internal.Data.Consumer.Type (Consumer (..))
159160import Streamly.Internal.Data.Parser (Parser (..))
160161import Streamly.Internal.Data.Array.Foreign.Type (Array)
161162import Streamly.Internal.Data.Stream.IsStream.Common
@@ -997,7 +998,7 @@ chunksOf n f = fromStreamD . D.chunksOf n f . toStreamD
997998{-# INLINE chunksOf2 #-}
998999chunksOf2
9991000 :: (IsStream t, Monad m)
1000− => Int -> m c -> Fold2 m c a b -> t m a -> t m b
1001+ => Int -> m c -> Consumer m c a b -> t m a -> t m b
10011002chunksOf2 n action f m = fromStreamD $ D.groupsOf2 n action f (toStreamD m)
10021003
10031004-- | @arraysOf n stream@ groups the elements in the input stream into arrays of
src/Streamly/Internal/Data/Stream/StreamD/Type.hs+4−3
@@ -99,7 +99,8 @@ import GHC.Base (build)
9999import GHC.Types (SPEC(..))
100100import Prelude hiding (map, mapM, foldr, take, concatMap, takeWhile)
101101
102−import Streamly.Internal.Data.Fold.Type (Fold(..), Fold2(..))
102+import Streamly.Internal.Data.Fold.Type (Fold(..))
103+import Streamly.Internal.Data.Consumer.Type (Consumer(..))
103104import Streamly.Internal.Data.Stream.StreamD.Step (Step (..))
104105import Streamly.Internal.Data.SVar.Type (State, adaptState, defState)
105106import Streamly.Internal.Data.Unfold.Type (Unfold(..))
@@ -932,10 +933,10 @@ groupsOf2
932933 :: Monad m
933934 => Int
934935 -> m c
935− -> Fold2 m c a b
936+ -> Consumer m c a b
936937 -> Stream m a
937938 -> Stream m b
938−groupsOf2 n input (Fold2 fstep inject extract) (Stream step state) =
939+groupsOf2 n input (Consumer fstep inject extract) (Stream step state) =
939940 n `seq` Stream step' (GroupStart2 state)
940941
941942 where
src/Streamly/Internal/FileSystem/Handle.hs+5−5
@@ -111,7 +111,7 @@ import Prelude hiding (read)
111111
112112import Streamly.Internal.BaseCompat
113113import Streamly.Internal.Data.Fold (Fold)
114−import Streamly.Internal.Data.Fold.Type (Fold2(..))
114+import Streamly.Internal.Data.Consumer.Type (Consumer(..))
115115import Streamly.Internal.Data.Unfold.Type (Unfold(..))
116116import Streamly.Internal.Data.Array.Foreign.Type
117117 (Array(..), writeNUnsafe, unsafeFreezeWithShrink)
@@ -457,9 +457,9 @@ writeChunks :: (MonadIO m, Storable a) => Handle -> Fold m (Array a) ()
457457writeChunks h = FL.drainBy (putChunk h)
458458
459459{-# INLINE writeChunks2 #-}
460−writeChunks2 :: (MonadIO m, Storable a) => Fold2 m Handle (Array a) ()
460+writeChunks2 :: (MonadIO m, Storable a) => Consumer m Handle (Array a) ()
461461writeChunks2 =
462− Fold2 (\h arr -> liftIO $ putChunk h arr >> return h)
462+ Consumer (\h arr -> liftIO $ putChunk h arr >> return h)
463463 return
464464 (\_ -> return ())
465465
@@ -499,7 +499,7 @@ writeWithBufferOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
499499writeWithBufferOf n h = FL.chunksOf n (writeNUnsafe n) (writeChunks h)
500500
501501{-# INLINE writeWithBufferOf2 #-}
502−writeWithBufferOf2 :: MonadIO m => Int -> Fold2 m Handle Word8 ()
502+writeWithBufferOf2 :: MonadIO m => Int -> Consumer m Handle Word8 ()
503503writeWithBufferOf2 n = FL.chunksOf2 n (writeNUnsafe n) writeChunks2
504504
505505-- | Write a byte stream to a file handle. Accumulates the input in chunks of
@@ -514,7 +514,7 @@ write :: MonadIO m => Handle -> Fold m Word8 ()
514514write = writeWithBufferOf defaultChunkSize
515515
516516{-# INLINE write2 #-}
517−write2 :: MonadIO m => Fold2 m Handle Word8 ()
517+write2 :: MonadIO m => Consumer m Handle Word8 ()
518518write2 = writeWithBufferOf2 defaultChunkSize
519519
520520{-