on master help sign in
Rename Array.Fold to Fold.Chunked (#2118)
17af28eRanjeet Ranjan
core/src/Streamly/Internal/Data/Array/Fold.hs → core/src/Streamly/Internal/Data/Fold/Chunked.hsrenamed+37−37
@@ -1,5 +1,5 @@
1-- |
1-- |
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 @@
19--
19--
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
25--
25--
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"
30--
30--
31−module Streamly.Internal.Data.Array.Fold
31+module Streamly.Internal.Data.Fold.Chunked
32 (
32 (
33− ArrayFold (..)
33+ ChunkFold (..)
34
34
35 -- * Construction
35 -- * Construction
36 , fromFold
36 , fromFold
@@ -88,7 +88,7 @@ import Prelude hiding (concatMap, take)
88--
88--
89-- /Pre-release/
89-- /Pre-release/
90--
90--
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)
92
92
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) =>
103#else
103#else
104fromFold :: forall m a b. (MonadIO m, Unbox a) =>
104fromFold :: forall m a b. (MonadIO m, Unbox a) =>
105#endif
105#endif
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))
109
109
110 where
110 where
111
111
@@ -145,9 +145,9 @@ fromParserD :: forall m a b. (MonadIO m) =>
145#else
145#else
146fromParserD :: forall m a b. (MonadIO m, Unbox a) =>
146fromParserD :: forall m a b. (MonadIO m, Unbox a) =>
147#endif
147#endif
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)
151
151
152 where
152 where
153
153
@@ -191,7 +191,7 @@ fromParser :: forall m a b. (MonadThrow m, MonadIO m) =>
191#else
191#else
192fromParser :: forall m a b. (MonadThrow m, MonadIO m, Unbox a) =>
192fromParser :: forall m a b. (MonadThrow m, MonadIO m, Unbox a) =>
193#endif
193#endif
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
196
196
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
204
204
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.
210--
210--
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
215
215
216-- | Map a monadic function on the output of a fold.
216-- | Map a monadic function on the output of a fold.
217--
217--
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
222
222
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/
230--
230--
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
234
234
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/
239--
239--
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
243
243
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
253
253
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
263
263
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
269
269
@@ -289,16 +289,16 @@ instance MonadThrow m => Applicative (ArrayFold m a) where
289--
289--
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
296
296
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'.
299--
299--
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
304
304
@@ -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
321
321
322 where
322 where
323
323
@@ -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"
331
331
332 initial = do
332 initial = do
333 res <- initial1
333 res <- initial1
core/src/Streamly/Internal/Data/Stream/Chunked.hs+12−12
@@ -64,7 +64,7 @@ import Prelude hiding (null, last, (!!), read, concat, unlines)
64import Streamly.Data.Fold (Fold)
64import Streamly.Data.Fold (Fold)
65import Streamly.Data.Stream (Stream)
65import Streamly.Data.Stream (Stream)
66import Streamly.Internal.Data.Array.Type (Array(..))
66import Streamly.Internal.Data.Array.Type (Array(..))
67−import Streamly.Internal.Data.Array.Fold (ArrayFold(..))
67+import Streamly.Internal.Data.Fold.Chunked (ChunkFold(..))
68import Streamly.Internal.Data.Parser (ParseError(..))
68import Streamly.Internal.Data.Parser (ParseError(..))
69import Streamly.Internal.Data.Stream
69import Streamly.Internal.Data.Stream
70 (fromStreamD, fromStreamK, toStreamD, toStreamK)
70 (fromStreamD, fromStreamK, toStreamD, toStreamK)
@@ -387,7 +387,7 @@ foldBreakK (FL.Fold fstep initial extract) stream = do
387-- | Fold an array stream using the supplied 'Fold'. Returns the fold result
387-- | Fold an array stream using the supplied 'Fold'. Returns the fold result
388-- and the unconsumed stream.
388-- and the unconsumed stream.
389--
389--
390−-- > foldBreak f = runArrayFoldBreak (ArrayFold.fromFold f)
390+-- > foldBreak f = runArrayFoldBreak (ChunkFold.fromFold f)
391--
391--
392-- /Internal/
392-- /Internal/
393--
393--
@@ -404,7 +404,7 @@ foldBreak f =
404 . toStreamK
404 . toStreamK
405-- If foldBreak performs better than runArrayFoldBreak we can use a rewrite
405-- If foldBreak performs better than runArrayFoldBreak we can use a rewrite
406-- rule to rewrite runArrayFoldBreak to fold.
406-- rule to rewrite runArrayFoldBreak to fold.
407−-- foldBreak f = runArrayFoldBreak (ArrayFold.fromFold f)
407+-- foldBreak f = runArrayFoldBreak (ChunkFold.fromFold f)
408
408
409-------------------------------------------------------------------------------
409-------------------------------------------------------------------------------
410-- Fold to a single Array
410-- Fold to a single Array
@@ -789,7 +789,7 @@ parseBreak p =
789-- | Note that this is not the same as using a @Parser (Array a) m b@ with the
789-- | Note that this is not the same as using a @Parser (Array a) m b@ with the
790-- regular "Streamly.Internal.Data.IsStream.parse" function. The regular parse
790-- regular "Streamly.Internal.Data.IsStream.parse" function. The regular parse
791-- would consume the input arrays as single unit. This parser parses in the way
791-- would consume the input arrays as single unit. This parser parses in the way
792−-- as described in the ArrayFold module. The input arrays are treated as @n@
792+-- as described in the ChunkFold module. The input arrays are treated as @n@
793-- element units and can be consumed partially. The remaining elements are
793-- element units and can be consumed partially. The remaining elements are
794-- inserted in the source stream as an array.
794-- inserted in the source stream as an array.
795--
795--
@@ -937,8 +937,8 @@ parseArr p s = fmap fromStreamD <$> parseBreakD p (toStreamD s)
937--
937--
938{-# INLINE runArrayFold #-}
938{-# INLINE runArrayFold #-}
939runArrayFold :: (MonadIO m, MonadThrow m, Unbox a) =>
939runArrayFold :: (MonadIO m, MonadThrow m, Unbox a) =>
940− ArrayFold m a b -> Stream m (A.Array a) -> m b
940+ ChunkFold m a b -> Stream m (A.Array a) -> m b
941−runArrayFold (ArrayFold p) s = fst <$> runArrayParserDBreak p (toStreamD s)
941+runArrayFold (ChunkFold p) s = fst <$> runArrayParserDBreak p (toStreamD s)
942
942
943-- | Like 'fold' but also returns the remaining stream.
943-- | Like 'fold' but also returns the remaining stream.
944--
944--
@@ -946,8 +946,8 @@ runArrayFold (ArrayFold p) s = fst <$> runArrayParserDBreak p (toStreamD s)
946--
946--
947{-# INLINE runArrayFoldBreak #-}
947{-# INLINE runArrayFoldBreak #-}
948runArrayFoldBreak :: (MonadIO m, MonadThrow m, Unbox a) =>
948runArrayFoldBreak :: (MonadIO m, MonadThrow m, Unbox a) =>
949− ArrayFold m a b -> Stream m (A.Array a) -> m (b, Stream m (A.Array a))
949+ ChunkFold m a b -> Stream m (A.Array a) -> m (b, Stream m (A.Array a))
950−runArrayFoldBreak (ArrayFold p) s =
950+runArrayFoldBreak (ChunkFold p) s =
951 second fromStreamD <$> runArrayParserDBreak p (toStreamD s)
951 second fromStreamD <$> runArrayParserDBreak p (toStreamD s)
952
952
953{-# ANN type ParseChunksState Fuse #-}
953{-# ANN type ParseChunksState Fuse #-}
@@ -964,11 +964,11 @@ data ParseChunksState x inpBuf st pst =
964{-# INLINE_NORMAL runArrayFoldManyD #-}
964{-# INLINE_NORMAL runArrayFoldManyD #-}
965runArrayFoldManyD
965runArrayFoldManyD
966 :: (MonadThrow m, Unbox a)
966 :: (MonadThrow m, Unbox a)
967− => ArrayFold m a b
967+ => ChunkFold m a b
968 -> D.Stream m (Array a)
968 -> D.Stream m (Array a)
969 -> D.Stream m b
969 -> D.Stream m b
970runArrayFoldManyD
970runArrayFoldManyD
971− (ArrayFold (PRD.Parser pstep initial extract)) (D.Stream step state) =
971+ (ChunkFold (PRD.Parser pstep initial extract)) (D.Stream step state) =
972
972
973 D.Stream stepOuter (ParseChunksInit [] state)
973 D.Stream stepOuter (ParseChunksInit [] state)
974
974
@@ -1141,7 +1141,7 @@ runArrayFoldManyD
1141
1141
1142 stepOuter _ (ParseChunksYield a next) = return $ D.Yield a next
1142 stepOuter _ (ParseChunksYield a next) = return $ D.Yield a next
1143
1143
1144−-- | Apply an 'ArrayFold' repeatedly on an array stream and emit the
1144+-- | Apply an 'ChunkFold' repeatedly on an array stream and emit the
1145-- fold outputs in the output stream.
1145-- fold outputs in the output stream.
1146--
1146--
1147-- See "Streamly.Data.Stream.foldMany" for more details.
1147-- See "Streamly.Data.Stream.foldMany" for more details.
@@ -1150,7 +1150,7 @@ runArrayFoldManyD
1150{-# INLINE runArrayFoldMany #-}
1150{-# INLINE runArrayFoldMany #-}
1151runArrayFoldMany
1151runArrayFoldMany
1152 :: (MonadThrow m, Unbox a)
1152 :: (MonadThrow m, Unbox a)
1153− => ArrayFold m a b
1153+ => ChunkFold m a b
1154 -> Stream m (Array a)
1154 -> Stream m (Array a)
1155 -> Stream m b
1155 -> Stream m b
1156runArrayFoldMany p m = fromStreamD $ runArrayFoldManyD p (toStreamD m)
1156runArrayFoldMany p m = fromStreamD $ runArrayFoldManyD p (toStreamD m)
core/streamly-core.cabal+1−1
@@ -298,6 +298,7 @@ library
298 , Streamly.Internal.Data.Unfold.Enumeration
298 , Streamly.Internal.Data.Unfold.Enumeration
299 , Streamly.Internal.Data.Fold.Tee
299 , Streamly.Internal.Data.Fold.Tee
300 , Streamly.Internal.Data.Fold
300 , Streamly.Internal.Data.Fold
301+ , Streamly.Internal.Data.Fold.Chunked
301 , Streamly.Internal.Data.Fold.Window
302 , Streamly.Internal.Data.Fold.Window
302 , Streamly.Internal.Data.Parser
303 , Streamly.Internal.Data.Parser
303 , Streamly.Internal.Data.Pipe
304 , Streamly.Internal.Data.Pipe
@@ -322,7 +323,6 @@ library
322 , Streamly.Internal.Data.Array.Generic
323 , Streamly.Internal.Data.Array.Generic
323 , Streamly.Internal.Data.Array
324 , Streamly.Internal.Data.Array
324 , Streamly.Internal.Data.Array.Mut.Stream
325 , Streamly.Internal.Data.Array.Mut.Stream
325− , Streamly.Internal.Data.Array.Fold
326
326
327 -- streamly-serde
327 -- streamly-serde
328 , Streamly.Internal.Data.Binary.Decode
328 , Streamly.Internal.Data.Binary.Decode