on master help sign in
Rename Fold2 to Consumer
ea23678Harendra Kumar
src/Streamly/Internal/Data/Consumer/Type.hs+15−3
@@ -11,10 +11,15 @@
11module Streamly.Internal.Data.Consumer.Type
11module Streamly.Internal.Data.Consumer.Type
12 (
12 (
13 -- * Types
13 -- * Types
14− Fold2 (..)
14+ Consumer (..)
15+
16+ -- Consumers
17+ , drainBy
15 )
18 )
16where
19where
17
20
21+import Control.Monad (void)
22+
18-- All folds in the Fold module should be implemented as Refolds.
23-- All folds in the Fold module should be implemented as Refolds.
19--
24--
20-- | Experimental type to provide a side input to the fold for generating the
25-- | Experimental type to provide a side input to the fold for generating the
@@ -23,6 +28,13 @@ where
23-- monadic action. This is a generalized version of 'Fold'.
28-- monadic action. This is a generalized version of 'Fold'.
24--
29--
25-- /Internal/
30-- /Internal/
26−data Fold2 m c a b =
31+data Consumer m c a b =
27 -- | @Fold @ @ step @ @ inject @ @ extract@
32 -- | @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
234 , duplicate
234 , duplicate
235 , finish
235 , finish
236
236
237− -- * Fold2
238− , drainBy2
239−
240 -- * Deprecated
237 -- * Deprecated
241 , sequence
238 , sequence
242 , mapM
239 , mapM
243 )
240 )
244where
241where
245
242
246−import Control.Monad (void)
247import Data.Bifunctor (first)
243import Data.Bifunctor (first)
248import Data.Either (isLeft, isRight)
244import Data.Either (isLeft, isRight)
249import Data.Functor.Identity (Identity(..))
245import Data.Functor.Identity (Identity(..))
@@ -416,13 +412,6 @@ scan = undefined
416drainBy :: Monad m => (a -> m b) -> Fold m a ()
412drainBy :: Monad m => (a -> m b) -> Fold m a ()
417drainBy f = lmapM f drain
413drainBy f = lmapM f drain
418
414
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−
426-- | Extract the last element of the input stream, if any.
415-- | Extract the last element of the input stream, if any.
427--
416--
428-- > last = fmap getLast $ Fold.foldMap (Last . Just)
417-- > 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
259 , duplicate
259 , duplicate
260 , finish
260 , finish
261
261
262− -- * Fold2
262+ -- * Consumer
263− , Fold2 (..)
264 , simplify
263 , simplify
265 , chunksOf2
264 , chunksOf2
266 )
265 )
@@ -272,7 +271,7 @@ import Data.Maybe (isJust, fromJust)
272import Fusion.Plugin.Types (Fuse(..))
271import Fusion.Plugin.Types (Fuse(..))
273import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
272import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
274import Streamly.Internal.Data.Tuple.Strict (Tuple'(..), Tuple3'(..))
273import Streamly.Internal.Data.Tuple.Strict (Tuple'(..), Tuple3'(..))
275−import Streamly.Internal.Data.Consumer.Type (Fold2(..))
274+import Streamly.Internal.Data.Consumer.Type (Consumer(..))
276
275
277import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
276import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
278
277
@@ -577,8 +576,8 @@ mkFoldM_ step initial = mkFoldM step initial return
577-- | Convert more general type 'Fold2' into a simpler type 'Fold'
576-- | Convert more general type 'Fold2' into a simpler type 'Fold'
578--
577--
579-- /Internal/
578-- /Internal/
580−simplify :: Functor m => Fold2 m c a b -> c -> Fold m a b
579+simplify :: Functor m => Consumer m c a b -> c -> Fold m a b
581−simplify (Fold2 step inject extract) c =
580+simplify (Consumer step inject extract) c =
582 Fold (\x a -> Partial <$> step x a) (Partial <$> inject c) extract
581 Fold (\x a -> Partial <$> step x a) (Partial <$> inject c) extract
583
582
584------------------------------------------------------------------------------
583------------------------------------------------------------------------------
@@ -1379,9 +1378,10 @@ chunksOf n split = many (take n split)
1379--
1378--
1380-- /Internal/
1379-- /Internal/
1381{-# INLINE chunksOf2 #-}
1380{-# INLINE chunksOf2 #-}
1382−chunksOf2 :: Monad m => Int -> Fold m a b -> Fold2 m x b c -> Fold2 m x a c
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) (Fold2 step2 inject2 extract2) =
1383+chunksOf2 n (Fold step1 initial1 extract1) (Consumer step2 inject2 extract2) =
1384− Fold2 step' inject' extract'
1384+ Consumer step' inject' extract'
1385
1385
1386 where
1386 where
1387
1387
src/Streamly/Internal/Data/Stream/IsStream/Reduce.hs+3−2
@@ -155,7 +155,8 @@ import Data.Kind (Type)
155import Data.Maybe (isNothing)
155import Data.Maybe (isNothing)
156import Foreign.Storable (Storable)
156import Foreign.Storable (Storable)
157import Streamly.Internal.Control.Concurrent (MonadAsync)
157import 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 (..))
159import Streamly.Internal.Data.Parser (Parser (..))
160import Streamly.Internal.Data.Parser (Parser (..))
160import Streamly.Internal.Data.Array.Foreign.Type (Array)
161import Streamly.Internal.Data.Array.Foreign.Type (Array)
161import Streamly.Internal.Data.Stream.IsStream.Common
162import Streamly.Internal.Data.Stream.IsStream.Common
@@ -997,7 +998,7 @@ chunksOf n f = fromStreamD . D.chunksOf n f . toStreamD
997{-# INLINE chunksOf2 #-}
998{-# INLINE chunksOf2 #-}
998chunksOf2
999chunksOf2
999 :: (IsStream t, Monad m)
1000 :: (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
1001chunksOf2 n action f m = fromStreamD $ D.groupsOf2 n action f (toStreamD m)
1002chunksOf2 n action f m = fromStreamD $ D.groupsOf2 n action f (toStreamD m)
1002
1003
1003-- | @arraysOf n stream@ groups the elements in the input stream into arrays of
1004-- | @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)
99import GHC.Types (SPEC(..))
99import GHC.Types (SPEC(..))
100import Prelude hiding (map, mapM, foldr, take, concatMap, takeWhile)
100import Prelude hiding (map, mapM, foldr, take, concatMap, takeWhile)
101
101
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(..))
103import Streamly.Internal.Data.Stream.StreamD.Step (Step (..))
104import Streamly.Internal.Data.Stream.StreamD.Step (Step (..))
104import Streamly.Internal.Data.SVar.Type (State, adaptState, defState)
105import Streamly.Internal.Data.SVar.Type (State, adaptState, defState)
105import Streamly.Internal.Data.Unfold.Type (Unfold(..))
106import Streamly.Internal.Data.Unfold.Type (Unfold(..))
@@ -932,10 +933,10 @@ groupsOf2
932 :: Monad m
933 :: Monad m
933 => Int
934 => Int
934 -> m c
935 -> m c
935− -> Fold2 m c a b
936+ -> Consumer m c a b
936 -> Stream m a
937 -> Stream m a
937 -> Stream m b
938 -> 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) =
939 n `seq` Stream step' (GroupStart2 state)
940 n `seq` Stream step' (GroupStart2 state)
940
941
941 where
942 where
src/Streamly/Internal/FileSystem/Handle.hs+5−5
@@ -111,7 +111,7 @@ import Prelude hiding (read)
111
111
112import Streamly.Internal.BaseCompat
112import Streamly.Internal.BaseCompat
113import Streamly.Internal.Data.Fold (Fold)
113import Streamly.Internal.Data.Fold (Fold)
114−import Streamly.Internal.Data.Fold.Type (Fold2(..))
114+import Streamly.Internal.Data.Consumer.Type (Consumer(..))
115import Streamly.Internal.Data.Unfold.Type (Unfold(..))
115import Streamly.Internal.Data.Unfold.Type (Unfold(..))
116import Streamly.Internal.Data.Array.Foreign.Type
116import Streamly.Internal.Data.Array.Foreign.Type
117 (Array(..), writeNUnsafe, unsafeFreezeWithShrink)
117 (Array(..), writeNUnsafe, unsafeFreezeWithShrink)
@@ -457,9 +457,9 @@ writeChunks :: (MonadIO m, Storable a) => Handle -> Fold m (Array a) ()
457writeChunks h = FL.drainBy (putChunk h)
457writeChunks h = FL.drainBy (putChunk h)
458
458
459{-# INLINE writeChunks2 #-}
459{-# INLINE writeChunks2 #-}
460−writeChunks2 :: (MonadIO m, Storable a) => Fold2 m Handle (Array a) ()
460+writeChunks2 :: (MonadIO m, Storable a) => Consumer m Handle (Array a) ()
461writeChunks2 =
461writeChunks2 =
462− Fold2 (\h arr -> liftIO $ putChunk h arr >> return h)
462+ Consumer (\h arr -> liftIO $ putChunk h arr >> return h)
463 return
463 return
464 (\_ -> return ())
464 (\_ -> return ())
465
465
@@ -499,7 +499,7 @@ writeWithBufferOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
499writeWithBufferOf n h = FL.chunksOf n (writeNUnsafe n) (writeChunks h)
499writeWithBufferOf n h = FL.chunksOf n (writeNUnsafe n) (writeChunks h)
500
500
501{-# INLINE writeWithBufferOf2 #-}
501{-# INLINE writeWithBufferOf2 #-}
502−writeWithBufferOf2 :: MonadIO m => Int -> Fold2 m Handle Word8 ()
502+writeWithBufferOf2 :: MonadIO m => Int -> Consumer m Handle Word8 ()
503writeWithBufferOf2 n = FL.chunksOf2 n (writeNUnsafe n) writeChunks2
503writeWithBufferOf2 n = FL.chunksOf2 n (writeNUnsafe n) writeChunks2
504
504
505-- | Write a byte stream to a file handle. Accumulates the input in chunks of
505-- | 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 ()
514write = writeWithBufferOf defaultChunkSize
514write = writeWithBufferOf defaultChunkSize
515
515
516{-# INLINE write2 #-}
516{-# INLINE write2 #-}
517−write2 :: MonadIO m => Fold2 m Handle Word8 ()
517+write2 :: MonadIO m => Consumer m Handle Word8 ()
518write2 = writeWithBufferOf2 defaultChunkSize
518write2 = writeWithBufferOf2 defaultChunkSize
519
519
520{-
520{-