@@ -1,5 +1,5 @@
2−-- Module : Streamly.Internal.Data.Array.Fold
2+-- Module : Streamly.Internal.Data.Fold.Chunked
3-- Copyright : (c) 2021 Composewell Technologies
3-- Copyright : (c) 2021 Composewell Technologies
4-- License : BSD-3-Clause
4-- License : BSD-3-Clause
5-- Maintainer : streamly@composewell.com
5-- Maintainer : streamly@composewell.com
@@ -19,18 +19,18 @@
20-- >>> import qualified Streamly.Data.Fold as Fold
20-- >>> import qualified Streamly.Data.Fold as Fold
21-- >>> import qualified Streamly.Internal.Data.Stream.Chunked as ArrayStream
21-- >>> import qualified Streamly.Internal.Data.Stream.Chunked as ArrayStream
22−-- >>> import qualified Streamly.Internal.Data.Array.Fold as ArrayFold
22+-- >>> import qualified Streamly.Internal.Data.Fold.Chunked as ChunkFold
23-- >>> import qualified Streamly.Internal.Data.Stream as Stream (arraysOf)
23-- >>> import qualified Streamly.Internal.Data.Stream as Stream (arraysOf)
24-- >>> import qualified Streamly.Data.Stream as Stream
24-- >>> import qualified Streamly.Data.Stream as Stream
26−-- >>> f = ArrayFold.fromFold (Fold.take 7 Fold.toList)
26+-- >>> f = ChunkFold.fromFold (Fold.take 7 Fold.toList)
27-- >>> s = Stream.arraysOf 5 $ Stream.fromList "hello world"
27-- >>> s = Stream.arraysOf 5 $ Stream.fromList "hello world"
28-- >>> ArrayStream.runArrayFold f s
28-- >>> ArrayStream.runArrayFold f s
29-- "hello w"
29-- "hello w"
31−module Streamly.Internal.Data.Array.Fold
31+module Streamly.Internal.Data.Fold.Chunked
33− ArrayFold (..)
33+ ChunkFold (..)
35 -- * Construction
35 -- * Construction
36 , fromFold
36 , fromFold
@@ -88,7 +88,7 @@ import Prelude hiding (concatMap, take)
89-- /Pre-release/
89-- /Pre-release/
91−newtype ArrayFold m a b = ArrayFold (ParserD.Parser (Array a) m b)
91+newtype ChunkFold m a b = ChunkFold (ParserD.Parser (Array a) m b)
93-------------------------------------------------------------------------------
93-------------------------------------------------------------------------------
94-- Constructing array stream folds from element folds and parsers
94-- Constructing array stream folds from element folds and parsers
@@ -103,9 +103,9 @@ fromFold :: forall m a b. (MonadIO m) =>
104fromFold :: forall m a b. (MonadIO m, Unbox a) =>
104fromFold :: forall m a b. (MonadIO m, Unbox a) =>
106− Fold.Fold m a b -> ArrayFold m a b
106+ Fold.Fold m a b -> ChunkFold m a b
107fromFold (Fold.Fold fstep finitial fextract) =
107fromFold (Fold.Fold fstep finitial fextract) =
108− ArrayFold (ParserD.Parser step initial (fmap (Done 0) . fextract))
108+ ChunkFold (ParserD.Parser step initial (fmap (Done 0) . fextract))
@@ -145,9 +145,9 @@ fromParserD :: forall m a b. (MonadIO m) =>
146fromParserD :: forall m a b. (MonadIO m, Unbox a) =>
146fromParserD :: forall m a b. (MonadIO m, Unbox a) =>
148− ParserD.Parser a m b -> ArrayFold m a b
148+ ParserD.Parser a m b -> ChunkFold m a b
149fromParserD (ParserD.Parser step1 initial1 extract1) =
149fromParserD (ParserD.Parser step1 initial1 extract1) =
150− ArrayFold (ParserD.Parser step initial1 extract1)
150+ ChunkFold (ParserD.Parser step initial1 extract1)
@@ -191,7 +191,7 @@ fromParser :: forall m a b. (MonadThrow m, MonadIO m) =>
192fromParser :: forall m a b. (MonadThrow m, MonadIO m, Unbox a) =>
192fromParser :: forall m a b. (MonadThrow m, MonadIO m, Unbox a) =>
194− Parser.Parser a m b -> ArrayFold m a b
194+ Parser.Parser a m b -> ChunkFold m a b
195fromParser = fromParserD . ParserD.fromParserK
195fromParser = fromParserD . ParserD.fromParserK
197-- | Adapt an array stream fold.
197-- | Adapt an array stream fold.
@@ -199,8 +199,8 @@ fromParser = fromParserD . ParserD.fromParserK
199-- /Pre-release/
199-- /Pre-release/
200{-# INLINE adaptFold #-}
200{-# INLINE adaptFold #-}
201adaptFold :: forall m a b. (MonadIO m) =>
201adaptFold :: forall m a b. (MonadIO m) =>
202− Fold.Fold m (Array a) b -> ArrayFold m a b
202+ Fold.Fold m (Array a) b -> ChunkFold m a b
203−adaptFold f = ArrayFold $ ParserD.fromFold f
203+adaptFold f = ChunkFold $ ParserD.fromFold f
205-------------------------------------------------------------------------------
205-------------------------------------------------------------------------------
206-- Functor
206-- Functor
@@ -209,16 +209,16 @@ adaptFold f = ArrayFold $ ParserD.fromFold f
209-- | Maps a function over the result of fold.
209-- | Maps a function over the result of fold.
211-- /Pre-release/
211-- /Pre-release/
212−instance Functor m => Functor (ArrayFold m a) where
212+instance Functor m => Functor (ChunkFold m a) where
213 {-# INLINE fmap #-}
213 {-# INLINE fmap #-}
214− fmap f (ArrayFold p) = ArrayFold $ fmap f p
214+ fmap f (ChunkFold p) = ChunkFold $ fmap f p
216-- | Map a monadic function on the output of a fold.
216-- | Map a monadic function on the output of a fold.
218-- /Pre-release/
218-- /Pre-release/
219{-# INLINE rmapM #-}
219{-# INLINE rmapM #-}
220−rmapM :: Monad m => (b -> m c) -> ArrayFold m a b -> ArrayFold m a c
220+rmapM :: Monad m => (b -> m c) -> ChunkFold m a b -> ChunkFold m a c
221−rmapM f (ArrayFold p) = ArrayFold $ ParserD.rmapM f p
221+rmapM f (ChunkFold p) = ChunkFold $ ParserD.rmapM f p
223-------------------------------------------------------------------------------
223-------------------------------------------------------------------------------
224-- Sequential applicative
224-- Sequential applicative
@@ -229,8 +229,8 @@ rmapM f (ArrayFold p) = ArrayFold $ ParserD.rmapM f p
229-- /Pre-release/
229-- /Pre-release/
231{-# INLINE fromPure #-}
231{-# INLINE fromPure #-}
232−fromPure :: Monad m => b -> ArrayFold m a b
232+fromPure :: Monad m => b -> ChunkFold m a b
233−fromPure = ArrayFold . ParserD.fromPure
233+fromPure = ChunkFold . ParserD.fromPure
235-- | A fold that always yields the result of an effectful action without
235-- | A fold that always yields the result of an effectful action without
236-- consuming any input.
236-- consuming any input.
@@ -238,8 +238,8 @@ fromPure = ArrayFold . ParserD.fromPure
238-- /Pre-release/
238-- /Pre-release/
240{-# INLINE fromEffect #-}
240{-# INLINE fromEffect #-}
241−fromEffect :: Monad m => m b -> ArrayFold m a b
241+fromEffect :: Monad m => m b -> ChunkFold m a b
242−fromEffect = ArrayFold . ParserD.fromEffect
242+fromEffect = ChunkFold . ParserD.fromEffect
244-- | Applies two folds sequentially on the input stream and combines their
244-- | Applies two folds sequentially on the input stream and combines their
245-- results using the supplied function.
245-- results using the supplied function.
@@ -247,9 +247,9 @@ fromEffect = ArrayFold . ParserD.fromEffect
247-- /Pre-release/
247-- /Pre-release/
248{-# INLINE split_ #-}
248{-# INLINE split_ #-}
249split_ :: MonadThrow m =>
249split_ :: MonadThrow m =>
250− ArrayFold m x a -> ArrayFold m x b -> ArrayFold m x b
250+ ChunkFold m x a -> ChunkFold m x b -> ChunkFold m x b
251−split_ (ArrayFold p1) (ArrayFold p2) =
251+split_ (ChunkFold p1) (ChunkFold p2) =
252− ArrayFold $ ParserD.noErrorUnsafeSplit_ p1 p2
252+ ChunkFold $ ParserD.noErrorUnsafeSplit_ p1 p2
254-- | Applies two folds sequentially on the input stream and combines their
254-- | Applies two folds sequentially on the input stream and combines their
255-- results using the supplied function.
255-- results using the supplied function.
@@ -257,13 +257,13 @@ split_ (ArrayFold p1) (ArrayFold p2) =
257-- /Pre-release/
257-- /Pre-release/
258{-# INLINE splitWith #-}
258{-# INLINE splitWith #-}
259splitWith :: MonadThrow m
259splitWith :: MonadThrow m
260− => (a -> b -> c) -> ArrayFold m x a -> ArrayFold m x b -> ArrayFold m x c
260+ => (a -> b -> c) -> ChunkFold m x a -> ChunkFold m x b -> ChunkFold m x c
261−splitWith f (ArrayFold p1) (ArrayFold p2) =
261+splitWith f (ChunkFold p1) (ChunkFold p2) =
262− ArrayFold $ ParserD.noErrorUnsafeSplitWith f p1 p2
262+ ChunkFold $ ParserD.noErrorUnsafeSplitWith f p1 p2
264-- | 'Applicative' form of 'splitWith'.
264-- | 'Applicative' form of 'splitWith'.
265-- > (<*>) = splitWith id
265-- > (<*>) = splitWith id
266−instance MonadThrow m => Applicative (ArrayFold m a) where
266+instance MonadThrow m => Applicative (ChunkFold m a) where
267 {-# INLINE pure #-}
267 {-# INLINE pure #-}
268 pure = fromPure
268 pure = fromPure
@@ -289,16 +289,16 @@ instance MonadThrow m => Applicative (ArrayFold m a) where
290{-# INLINE concatMap #-}
290{-# INLINE concatMap #-}
291concatMap :: MonadThrow m =>
291concatMap :: MonadThrow m =>
292− (b -> ArrayFold m a c) -> ArrayFold m a b -> ArrayFold m a c
292+ (b -> ChunkFold m a c) -> ChunkFold m a b -> ChunkFold m a c
293−concatMap func (ArrayFold p) =
293+concatMap func (ChunkFold p) =
294− let f x = let ArrayFold y = func x in y
294+ let f x = let ChunkFold y = func x in y
295− in ArrayFold $ ParserD.noErrorUnsafeConcatMap f p
295+ in ChunkFold $ ParserD.noErrorUnsafeConcatMap f p
297-- | Monad instance applies folds sequentially. Next fold can depend on the
297-- | Monad instance applies folds sequentially. Next fold can depend on the
298-- output of the previous fold. See 'concatMap'.
298-- output of the previous fold. See 'concatMap'.
300-- > (>>=) = flip concatMap
300-- > (>>=) = flip concatMap
301−instance MonadThrow m => Monad (ArrayFold m a) where
301+instance MonadThrow m => Monad (ChunkFold m a) where
302 {-# INLINE return #-}
302 {-# INLINE return #-}
303 return = pure
303 return = pure
@@ -315,9 +315,9 @@ instance MonadThrow m => Monad (ArrayFold m a) where
315-- | Take @n@ array elements (@a@) from a stream of arrays (@Array a@).
315-- | Take @n@ array elements (@a@) from a stream of arrays (@Array a@).
316{-# INLINE take #-}
316{-# INLINE take #-}
317take :: forall m a b. (Monad m, Unbox a) =>
317take :: forall m a b. (Monad m, Unbox a) =>
318− Int -> ArrayFold m a b -> ArrayFold m a b
318+ Int -> ChunkFold m a b -> ChunkFold m a b
319−take n (ArrayFold (ParserD.Parser step1 initial1 extract1)) =
319+take n (ChunkFold (ParserD.Parser step1 initial1 extract1)) =
320− ArrayFold $ ParserD.Parser step initial extract
320+ ChunkFold $ ParserD.Parser step initial extract
@@ -327,7 +327,7 @@ take n (ArrayFold (ParserD.Parser step1 initial1 extract1)) =
327 return $ case r of
327 return $ case r of
328 Done _ b -> IDone b
328 Done _ b -> IDone b
329 Error err -> IError err
329 Error err -> IError err
330− _ -> error "Bug: ArrayFold take invalid state in initial"
330+ _ -> error "Bug: ChunkFold take invalid state in initial"
332 initial = do
332 initial = do
333 res <- initial1
333 res <- initial1