on master help sign in
Rename Array.Fold to Fold.Chunked (#2118)

* Rename Streamly.Internal.Data.Array.Fold to Streamly.Internal.Data.Fold.Chunked * Rename ArrayFold to ChunkFold

commit17af28e6f31f47f1f7248b2424ef91005d84b754 authorRanjeet Ranjan <ranjeetr@composewell.com> dateWed 16 Nov 2022, 09:09 (4 years ago) parent297574d
3 files changed+50−50
core/src/Streamly/Internal/Data/Array/Fold.hs → core/src/Streamly/Internal/Data/Fold/Chunked.hs+37−37 core/src/Streamly/Internal/Data/Stream/Chunked.hs+12−12 core/streamly-core.cabal+1−1
core/src/Streamly/Internal/Data/Array/Fold.hs → core/src/Streamly/Internal/Data/Fold/Chunked.hsrenamed+37−37
@@ -1,5 +1,5 @@
11-- |
2−-- Module : Streamly.Internal.Data.Array.Fold
2+-- Module : Streamly.Internal.Data.Fold.Chunked
33-- Copyright : (c) 2021 Composewell Technologies
44-- License : BSD-3-Clause
55-- Maintainer : streamly@composewell.com
@@ -19,18 +19,18 @@
1919--
2020-- >>> import qualified Streamly.Data.Fold as Fold
2121-- >>> 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
2323-- >>> import qualified Streamly.Internal.Data.Stream as Stream (arraysOf)
2424-- >>> import qualified Streamly.Data.Stream as Stream
2525--
26−-- >>> f = ArrayFold.fromFold (Fold.take 7 Fold.toList)
26+-- >>> f = ChunkFold.fromFold (Fold.take 7 Fold.toList)
2727-- >>> s = Stream.arraysOf 5 $ Stream.fromList "hello world"
2828-- >>> ArrayStream.runArrayFold f s
2929-- "hello w"
3030--
31−module Streamly.Internal.Data.Array.Fold
31+module Streamly.Internal.Data.Fold.Chunked
3232 (
33− ArrayFold (..)
33+ ChunkFold (..)
3434
3535 -- * Construction
3636 , fromFold
@@ -88,7 +88,7 @@ import Prelude hiding (concatMap, take)
8888--
8989-- /Pre-release/
9090--
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)
9292
9393-------------------------------------------------------------------------------
9494-- Constructing array stream folds from element folds and parsers
@@ -103,9 +103,9 @@ fromFold :: forall m a b. (MonadIO m) =>
103103#else
104104fromFold :: forall m a b. (MonadIO m, Unbox a) =>
105105#endif
106− Fold.Fold m a b -> ArrayFold m a b
106+ Fold.Fold m a b -> ChunkFold m a b
107107fromFold (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))
109109
110110 where
111111
@@ -145,9 +145,9 @@ fromParserD :: forall m a b. (MonadIO m) =>
145145#else
146146fromParserD :: forall m a b. (MonadIO m, Unbox a) =>
147147#endif
148− ParserD.Parser a m b -> ArrayFold m a b
148+ ParserD.Parser a m b -> ChunkFold m a b
149149fromParserD (ParserD.Parser step1 initial1 extract1) =
150− ArrayFold (ParserD.Parser step initial1 extract1)
150+ ChunkFold (ParserD.Parser step initial1 extract1)
151151
152152 where
153153
@@ -191,7 +191,7 @@ fromParser :: forall m a b. (MonadThrow m, MonadIO m) =>
191191#else
192192fromParser :: forall m a b. (MonadThrow m, MonadIO m, Unbox a) =>
193193#endif
194− Parser.Parser a m b -> ArrayFold m a b
194+ Parser.Parser a m b -> ChunkFold m a b
195195fromParser = fromParserD . ParserD.fromParserK
196196
197197-- | Adapt an array stream fold.
@@ -199,8 +199,8 @@ fromParser = fromParserD . ParserD.fromParserK
199199-- /Pre-release/
200200{-# INLINE adaptFold #-}
201201adaptFold :: forall m a b. (MonadIO m) =>
202− Fold.Fold m (Array a) b -> ArrayFold m a b
203−adaptFold f = ArrayFold $ ParserD.fromFold f
202+ Fold.Fold m (Array a) b -> ChunkFold m a b
203+adaptFold f = ChunkFold $ ParserD.fromFold f
204204
205205-------------------------------------------------------------------------------
206206-- Functor
@@ -209,16 +209,16 @@ adaptFold f = ArrayFold $ ParserD.fromFold f
209209-- | Maps a function over the result of fold.
210210--
211211-- /Pre-release/
212−instance Functor m => Functor (ArrayFold m a) where
212+instance Functor m => Functor (ChunkFold m a) where
213213 {-# INLINE fmap #-}
214− fmap f (ArrayFold p) = ArrayFold $ fmap f p
214+ fmap f (ChunkFold p) = ChunkFold $ fmap f p
215215
216216-- | Map a monadic function on the output of a fold.
217217--
218218-- /Pre-release/
219219{-# INLINE rmapM #-}
220−rmapM :: Monad m => (b -> m c) -> ArrayFold m a b -> ArrayFold m a c
221−rmapM f (ArrayFold p) = ArrayFold $ ParserD.rmapM f p
220+rmapM :: Monad m => (b -> m c) -> ChunkFold m a b -> ChunkFold m a c
221+rmapM f (ChunkFold p) = ChunkFold $ ParserD.rmapM f p
222222
223223-------------------------------------------------------------------------------
224224-- Sequential applicative
@@ -229,8 +229,8 @@ rmapM f (ArrayFold p) = ArrayFold $ ParserD.rmapM f p
229229-- /Pre-release/
230230--
231231{-# INLINE fromPure #-}
232−fromPure :: Monad m => b -> ArrayFold m a b
233−fromPure = ArrayFold . ParserD.fromPure
232+fromPure :: Monad m => b -> ChunkFold m a b
233+fromPure = ChunkFold . ParserD.fromPure
234234
235235-- | A fold that always yields the result of an effectful action without
236236-- consuming any input.
@@ -238,8 +238,8 @@ fromPure = ArrayFold . ParserD.fromPure
238238-- /Pre-release/
239239--
240240{-# INLINE fromEffect #-}
241−fromEffect :: Monad m => m b -> ArrayFold m a b
242−fromEffect = ArrayFold . ParserD.fromEffect
241+fromEffect :: Monad m => m b -> ChunkFold m a b
242+fromEffect = ChunkFold . ParserD.fromEffect
243243
244244-- | Applies two folds sequentially on the input stream and combines their
245245-- results using the supplied function.
@@ -247,9 +247,9 @@ fromEffect = ArrayFold . ParserD.fromEffect
247247-- /Pre-release/
248248{-# INLINE split_ #-}
249249split_ :: MonadThrow m =>
250− ArrayFold m x a -> ArrayFold m x b -> ArrayFold m x b
251−split_ (ArrayFold p1) (ArrayFold p2) =
252− ArrayFold $ ParserD.noErrorUnsafeSplit_ p1 p2
250+ ChunkFold m x a -> ChunkFold m x b -> ChunkFold m x b
251+split_ (ChunkFold p1) (ChunkFold p2) =
252+ ChunkFold $ ParserD.noErrorUnsafeSplit_ p1 p2
253253
254254-- | Applies two folds sequentially on the input stream and combines their
255255-- results using the supplied function.
@@ -257,13 +257,13 @@ split_ (ArrayFold p1) (ArrayFold p2) =
257257-- /Pre-release/
258258{-# INLINE splitWith #-}
259259splitWith :: MonadThrow m
260− => (a -> b -> c) -> ArrayFold m x a -> ArrayFold m x b -> ArrayFold m x c
261−splitWith f (ArrayFold p1) (ArrayFold p2) =
262− ArrayFold $ ParserD.noErrorUnsafeSplitWith f p1 p2
260+ => (a -> b -> c) -> ChunkFold m x a -> ChunkFold m x b -> ChunkFold m x c
261+splitWith f (ChunkFold p1) (ChunkFold p2) =
262+ ChunkFold $ ParserD.noErrorUnsafeSplitWith f p1 p2
263263
264264-- | 'Applicative' form of 'splitWith'.
265265-- > (<*>) = splitWith id
266−instance MonadThrow m => Applicative (ArrayFold m a) where
266+instance MonadThrow m => Applicative (ChunkFold m a) where
267267 {-# INLINE pure #-}
268268 pure = fromPure
269269
@@ -289,16 +289,16 @@ instance MonadThrow m => Applicative (ArrayFold m a) where
289289--
290290{-# INLINE concatMap #-}
291291concatMap :: MonadThrow m =>
292− (b -> ArrayFold m a c) -> ArrayFold m a b -> ArrayFold m a c
293−concatMap func (ArrayFold p) =
294− let f x = let ArrayFold y = func x in y
295− in ArrayFold $ ParserD.noErrorUnsafeConcatMap f p
292+ (b -> ChunkFold m a c) -> ChunkFold m a b -> ChunkFold m a c
293+concatMap func (ChunkFold p) =
294+ let f x = let ChunkFold y = func x in y
295+ in ChunkFold $ ParserD.noErrorUnsafeConcatMap f p
296296
297297-- | Monad instance applies folds sequentially. Next fold can depend on the
298298-- output of the previous fold. See 'concatMap'.
299299--
300300-- > (>>=) = flip concatMap
301−instance MonadThrow m => Monad (ArrayFold m a) where
301+instance MonadThrow m => Monad (ChunkFold m a) where
302302 {-# INLINE return #-}
303303 return = pure
304304
@@ -315,9 +315,9 @@ instance MonadThrow m => Monad (ArrayFold m a) where
315315-- | Take @n@ array elements (@a@) from a stream of arrays (@Array a@).
316316{-# INLINE take #-}
317317take :: forall m a b. (Monad m, Unbox a) =>
318− Int -> ArrayFold m a b -> ArrayFold m a b
319−take n (ArrayFold (ParserD.Parser step1 initial1 extract1)) =
320− ArrayFold $ ParserD.Parser step initial extract
318+ Int -> ChunkFold m a b -> ChunkFold m a b
319+take n (ChunkFold (ParserD.Parser step1 initial1 extract1)) =
320+ ChunkFold $ ParserD.Parser step initial extract
321321
322322 where
323323
@@ -327,7 +327,7 @@ take n (ArrayFold (ParserD.Parser step1 initial1 extract1)) =
327327 return $ case r of
328328 Done _ b -> IDone b
329329 Error err -> IError err
330− _ -> error "Bug: ArrayFold take invalid state in initial"
330+ _ -> error "Bug: ChunkFold take invalid state in initial"
331331
332332 initial = do
333333 res <- initial1
core/src/Streamly/Internal/Data/Stream/Chunked.hs+12−12
@@ -64,7 +64,7 @@ import Prelude hiding (null, last, (!!), read, concat, unlines)
6464import Streamly.Data.Fold (Fold)
6565import Streamly.Data.Stream (Stream)
6666import Streamly.Internal.Data.Array.Type (Array(..))
67−import Streamly.Internal.Data.Array.Fold (ArrayFold(..))
67+import Streamly.Internal.Data.Fold.Chunked (ChunkFold(..))
6868import Streamly.Internal.Data.Parser (ParseError(..))
6969import Streamly.Internal.Data.Stream
7070 (fromStreamD, fromStreamK, toStreamD, toStreamK)
@@ -387,7 +387,7 @@ foldBreakK (FL.Fold fstep initial extract) stream = do
387387-- | Fold an array stream using the supplied 'Fold'. Returns the fold result
388388-- and the unconsumed stream.
389389--
390−-- > foldBreak f = runArrayFoldBreak (ArrayFold.fromFold f)
390+-- > foldBreak f = runArrayFoldBreak (ChunkFold.fromFold f)
391391--
392392-- /Internal/
393393--
@@ -404,7 +404,7 @@ foldBreak f =
404404 . toStreamK
405405-- If foldBreak performs better than runArrayFoldBreak we can use a rewrite
406406-- rule to rewrite runArrayFoldBreak to fold.
407−-- foldBreak f = runArrayFoldBreak (ArrayFold.fromFold f)
407+-- foldBreak f = runArrayFoldBreak (ChunkFold.fromFold f)
408408
409409-------------------------------------------------------------------------------
410410-- Fold to a single Array
@@ -789,7 +789,7 @@ parseBreak p =
789789-- | Note that this is not the same as using a @Parser (Array a) m b@ with the
790790-- regular "Streamly.Internal.Data.IsStream.parse" function. The regular parse
791791-- 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@
793793-- element units and can be consumed partially. The remaining elements are
794794-- inserted in the source stream as an array.
795795--
@@ -937,8 +937,8 @@ parseArr p s = fmap fromStreamD <$> parseBreakD p (toStreamD s)
937937--
938938{-# INLINE runArrayFold #-}
939939runArrayFold :: (MonadIO m, MonadThrow m, Unbox a) =>
940− ArrayFold m a b -> Stream m (A.Array a) -> m b
941−runArrayFold (ArrayFold p) s = fst <$> runArrayParserDBreak p (toStreamD s)
940+ ChunkFold m a b -> Stream m (A.Array a) -> m b
941+runArrayFold (ChunkFold p) s = fst <$> runArrayParserDBreak p (toStreamD s)
942942
943943-- | Like 'fold' but also returns the remaining stream.
944944--
@@ -946,8 +946,8 @@ runArrayFold (ArrayFold p) s = fst <$> runArrayParserDBreak p (toStreamD s)
946946--
947947{-# INLINE runArrayFoldBreak #-}
948948runArrayFoldBreak :: (MonadIO m, MonadThrow m, Unbox a) =>
949− ArrayFold m a b -> Stream m (A.Array a) -> m (b, Stream m (A.Array a))
950−runArrayFoldBreak (ArrayFold p) s =
949+ ChunkFold m a b -> Stream m (A.Array a) -> m (b, Stream m (A.Array a))
950+runArrayFoldBreak (ChunkFold p) s =
951951 second fromStreamD <$> runArrayParserDBreak p (toStreamD s)
952952
953953{-# ANN type ParseChunksState Fuse #-}
@@ -964,11 +964,11 @@ data ParseChunksState x inpBuf st pst =
964964{-# INLINE_NORMAL runArrayFoldManyD #-}
965965runArrayFoldManyD
966966 :: (MonadThrow m, Unbox a)
967− => ArrayFold m a b
967+ => ChunkFold m a b
968968 -> D.Stream m (Array a)
969969 -> D.Stream m b
970970runArrayFoldManyD
971− (ArrayFold (PRD.Parser pstep initial extract)) (D.Stream step state) =
971+ (ChunkFold (PRD.Parser pstep initial extract)) (D.Stream step state) =
972972
973973 D.Stream stepOuter (ParseChunksInit [] state)
974974
@@ -1141,7 +1141,7 @@ runArrayFoldManyD
11411141
11421142 stepOuter _ (ParseChunksYield a next) = return $ D.Yield a next
11431143
1144−-- | Apply an 'ArrayFold' repeatedly on an array stream and emit the
1144+-- | Apply an 'ChunkFold' repeatedly on an array stream and emit the
11451145-- fold outputs in the output stream.
11461146--
11471147-- See "Streamly.Data.Stream.foldMany" for more details.
@@ -1150,7 +1150,7 @@ runArrayFoldManyD
11501150{-# INLINE runArrayFoldMany #-}
11511151runArrayFoldMany
11521152 :: (MonadThrow m, Unbox a)
1153− => ArrayFold m a b
1153+ => ChunkFold m a b
11541154 -> Stream m (Array a)
11551155 -> Stream m b
11561156runArrayFoldMany p m = fromStreamD $ runArrayFoldManyD p (toStreamD m)
core/streamly-core.cabal+1−1
@@ -298,6 +298,7 @@ library
298298 , Streamly.Internal.Data.Unfold.Enumeration
299299 , Streamly.Internal.Data.Fold.Tee
300300 , Streamly.Internal.Data.Fold
301+ , Streamly.Internal.Data.Fold.Chunked
301302 , Streamly.Internal.Data.Fold.Window
302303 , Streamly.Internal.Data.Parser
303304 , Streamly.Internal.Data.Pipe
@@ -322,7 +323,6 @@ library
322323 , Streamly.Internal.Data.Array.Generic
323324 , Streamly.Internal.Data.Array
324325 , Streamly.Internal.Data.Array.Mut.Stream
325− , Streamly.Internal.Data.Array.Fold
326326
327327 -- streamly-serde
328328 , Streamly.Internal.Data.Binary.Decode