on master help sign in
Rename chunksOf to groupsOf

* Deprecate chunksOf in Data.Fold as it is exposed. * No deprecation is required in Data.Stream as it is a new module.

commita43577b4b11c336bf61c38e89842e1b7768dce44 authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateMon 20 Feb 2023, 10:49 (4 years ago) parentc895000
14 files changed+58−47
benchmark/Streamly/Benchmark/FileSystem/Handle/Read.hs+20−20 core/src/Streamly/Data/Fold.hs+11−1 core/src/Streamly/Internal/Data/Fold.hs+2−2 core/src/Streamly/Internal/Data/Fold/Type.hs+8−8 core/src/Streamly/Internal/Data/Stream/Chunked.hs+2−2 core/src/Streamly/Internal/Data/Stream/StreamD/Nesting.hs+1−1 core/src/Streamly/Internal/Data/Stream/StreamD/Type.hs+4−4 core/src/Streamly/Internal/FileSystem/Dir.hs+1−1 core/src/Streamly/Internal/FileSystem/File.hs+2−2 core/src/Streamly/Internal/FileSystem/Handle.hs+2−2 docs/User/ProjectRelated/Changelog.md+1 src/Streamly/Internal/Data/Stream/IsStream/Reduce.hs+2−2 src/Streamly/Internal/Network/Inet/TCP.hs+1−1 src/Streamly/Internal/Network/Socket.hs+1−1
benchmark/Streamly/Benchmark/FileSystem/Handle/Read.hs+20−20
@@ -215,7 +215,7 @@ o_1_space_reduce_toBytes env =
215215
216216chunksOfSum :: Int -> Handle -> IO Int
217217chunksOfSum n inh =
218− S.fold Fold.length $ IP.chunksOf n FL.sum (S.unfold FH.reader inh)
218+ S.fold Fold.length $ IP.groupsOf n FL.sum (S.unfold FH.reader inh)
219219
220220foldManyPostChunksOfSum :: Int -> Handle -> IO Int
221221foldManyPostChunksOfSum n inh =
@@ -231,20 +231,20 @@ foldManyChunksOfSum n inh =
231231-- Even though allocations remain the same in both cases inlining improves time
232232-- by 4x.
233233-- | Slice in chunks of size n and get the count of chunks.
234−{-# INLINE chunksOf #-}
235−chunksOf :: Int -> Handle -> IO Int
236−chunksOf n inh =
234+{-# INLINE groupsOf #-}
235+groupsOf :: Int -> Handle -> IO Int
236+groupsOf n inh =
237237 -- writeNUnsafe gives 2.5x boost here over writeN.
238238 S.fold Fold.length
239− $ IP.chunksOf n (AT.writeNUnsafe n) (S.unfold FH.reader inh)
239+ $ IP.groupsOf n (AT.writeNUnsafe n) (S.unfold FH.reader inh)
240240
241241#ifdef INSPECTION
242−inspect $ hasNoTypeClasses 'chunksOf
243−inspect $ 'chunksOf `hasNoType` ''Step
244−inspect $ 'chunksOf `hasNoType` ''FoldMany
245−inspect $ 'chunksOf `hasNoType` ''AT.ArrayUnsafe -- AT.writeNUnsafe
242+inspect $ hasNoTypeClasses 'groupsOf
243+inspect $ 'groupsOf `hasNoType` ''Step
244+inspect $ 'groupsOf `hasNoType` ''FoldMany
245+inspect $ 'groupsOf `hasNoType` ''AT.ArrayUnsafe -- AT.writeNUnsafe
246246 -- FH.read/A.read
247−inspect $ 'chunksOf `hasNoType` ''IUF.ConcatState -- FH.read/UF.many
247+inspect $ 'groupsOf `hasNoType` ''IUF.ConcatState -- FH.read/UF.many
248248#endif
249249
250250{-# INLINE arraysOf #-}
@@ -255,10 +255,10 @@ arraysOf n inh =
255255o_1_space_reduce_read_grouped :: BenchEnv -> [Benchmark]
256256o_1_space_reduce_read_grouped env =
257257 [ bgroup "reduce/read/chunks"
258− [ mkBench ("S.chunksOf " ++ show (bigSize env) ++ " FL.sum") env $
258+ [ mkBench ("S.groupsOf " ++ show (bigSize env) ++ " FL.sum") env $
259259 \inh _ ->
260260 chunksOfSum (bigSize env) inh
261− , mkBench "S.chunksOf 1 FL.sum" env $ \inh _ ->
261+ , mkBench "S.groupsOf 1 FL.sum" env $ \inh _ ->
262262 chunksOfSum 1 inh
263263
264264 -- XXX investigate why we need inline/noinline in these cases (GHC)
@@ -281,14 +281,14 @@ o_1_space_reduce_read_grouped env =
281281 $ \inh _ -> inline foldManyChunksOfSum 1 inh
282282
283283 -- folding chunks to arrays
284− , mkBenchSmall "S.chunksOf 1" env $ \inh _ ->
285− chunksOf 1 inh
286− , mkBench "S.chunksOf 10" env $ \inh _ ->
287− chunksOf 10 inh
288− , mkBench "S.chunksOf 1000" env $ \inh _ ->
289− chunksOf 1000 inh
290−
291− -- arraysOf may use a different impl than chunksOf
284+ , mkBenchSmall "S.groupsOf 1" env $ \inh _ ->
285+ groupsOf 1 inh
286+ , mkBench "S.groupsOf 10" env $ \inh _ ->
287+ groupsOf 10 inh
288+ , mkBench "S.groupsOf 1000" env $ \inh _ ->
289+ groupsOf 1000 inh
290+
291+ -- arraysOf may use a different impl than groupsOf
292292 , mkBenchSmall "S.arraysOf 1" env $ \inh _ ->
293293 arraysOf 1 inh
294294 , mkBench "S.arraysOf 10" env $ \inh _ ->
core/src/Streamly/Data/Fold.hs+11−1
@@ -325,7 +325,7 @@ module Streamly.Data.Fold
325325
326326 -- ** Splitting
327327 , many
328− , chunksOf
328+ , groupsOf
329329 -- , intervalsOf
330330
331331 -- ** Nesting
@@ -335,6 +335,7 @@ module Streamly.Data.Fold
335335 , morphInner
336336
337337 -- * Deprecated
338+ , chunksOf
338339 , foldr
339340 , drainBy
340341 , last
@@ -362,3 +363,12 @@ import Streamly.Internal.Data.Fold.Container
362363-- >>> import Data.Function ((&))
363364-- >>> import qualified Streamly.Data.Fold as Fold
364365-- >>> import qualified Streamly.Data.Stream as Stream
366+
367+--------------------------------------------------------------------------------
368+-- Deprecated
369+--------------------------------------------------------------------------------
370+
371+{-# DEPRECATED chunksOf "Please use 'groupsOf' instead" #-}
372+{-# INLINE chunksOf #-}
373+chunksOf :: Monad m => Int -> Fold m a b -> Fold m b c -> Fold m a c
374+chunksOf = groupsOf
core/src/Streamly/Internal/Data/Fold.hs+2−2
@@ -283,7 +283,7 @@ module Streamly.Internal.Data.Fold
283283 -- ** Splitting
284284 , many
285285 , manyPost
286− , chunksOf
286+ , groupsOf
287287 , chunksBetween
288288 , refoldMany
289289 , refoldMany1
@@ -461,7 +461,7 @@ breakStreamK strm fl = fmap f $ K.foldBreak fl (Stream.toStreamK strm)
461461--
462462-- >>> :{
463463-- let f :: Fold IO Int (Stream Identity (Array Int))
464−-- f = Fold.chunksOf 2 (Array.writeN 3) Fold.toStream
464+-- f = Fold.groupsOf 2 (Array.writeN 3) Fold.toStream
465465-- in pure f
466466-- >>= Fold.addOne 1
467467-- >>= Fold.addStream (Stream.enumerateFromTo 2 4)
core/src/Streamly/Internal/Data/Fold/Type.hs+8−8
@@ -382,7 +382,7 @@ module Streamly.Internal.Data.Fold.Type
382382 , ManyState
383383 , many
384384 , manyPost
385− , chunksOf
385+ , groupsOf
386386 , refoldMany
387387 , refoldMany1
388388
@@ -1334,7 +1334,7 @@ catEithers = lmap (either id id)
13341334-- Parsing
13351335------------------------------------------------------------------------------
13361336
1337−-- Required to fuse "take" with "many" in "chunksOf", for ghc-9.x
1337+-- Required to fuse "take" with "many" in "groupsOf", for ghc-9.x
13381338{-# ANN type Tuple'Fused Fuse #-}
13391339data Tuple'Fused a b = Tuple'Fused !a !b deriving Show
13401340
@@ -1712,25 +1712,25 @@ manyPost (Fold sstep sinitial sextract) (Fold cstep cinitial cextract) =
17121712 Partial s -> cextract s
17131713 Done b -> return b
17141714
1715−-- | @chunksOf n split collect@ repeatedly applies the @split@ fold to chunks
1715+-- | @groupsOf n split collect@ repeatedly applies the @split@ fold to chunks
17161716-- of @n@ items in the input stream and supplies the result to the @collect@
17171717-- fold.
17181718--
17191719-- Definition:
17201720--
1721−-- >>> chunksOf n split = Fold.many (Fold.take n split)
1721+-- >>> groupsOf n split = Fold.many (Fold.take n split)
17221722--
17231723-- Example:
17241724--
1725−-- >>> twos = Fold.chunksOf 2 Fold.toList Fold.toList
1725+-- >>> twos = Fold.groupsOf 2 Fold.toList Fold.toList
17261726-- >>> Stream.fold twos $ Stream.fromList [1..10]
17271727-- [[1,2],[3,4],[5,6],[7,8],[9,10]]
17281728--
17291729-- Stops when @collect@ stops.
17301730--
1731−{-# INLINE chunksOf #-}
1732−chunksOf :: Monad m => Int -> Fold m a b -> Fold m b c -> Fold m a c
1733−chunksOf n split = many (take n split)
1731+{-# INLINE groupsOf #-}
1732+groupsOf :: Monad m => Int -> Fold m a b -> Fold m b c -> Fold m a c
1733+groupsOf n split = many (take n split)
17341734
17351735------------------------------------------------------------------------------
17361736-- Refold and Fold Combinators
core/src/Streamly/Internal/Data/Stream/Chunked.hs+2−2
@@ -111,7 +111,7 @@ import qualified Streamly.Internal.Data.Stream.StreamK as K
111111-- | @arraysOf n stream@ groups the elements in the input stream into arrays of
112112-- @n@ elements each.
113113--
114−-- > arraysOf n = Stream.chunksOf n (Array.writeN n)
114+-- > arraysOf n = Stream.groupsOf n (Array.writeN n)
115115--
116116-- /Pre-release/
117117{-# INLINE arraysOf #-}
@@ -541,7 +541,7 @@ toArraysInRange low high (Fold step initial extract) =
541541{-# INLINE _toArraysOf #-}
542542_toArraysOf :: (MonadIO m, Unbox a)
543543 => Int -> Fold m a (Stream Identity (Array a))
544−_toArraysOf n = FL.chunksOf n (A.writeNF n) FL.toStream
544+_toArraysOf n = FL.groupsOf n (A.writeNF n) FL.toStream
545545-}
546546
547547-------------------------------------------------------------------------------
core/src/Streamly/Internal/Data/Stream/StreamD/Nesting.hs+1−1
@@ -138,7 +138,7 @@ module Streamly.Internal.Data.Stream.StreamD.Nesting
138138
139139 -- ** Grouping
140140 -- | Group segments of a stream and fold. Special case of parsing.
141− , chunksOf
141+ , groupsOf
142142 , groupsBy
143143 , groupsRollingBy
144144
core/src/Streamly/Internal/Data/Stream/StreamD/Type.hs+4−4
@@ -120,7 +120,7 @@ module Streamly.Internal.Data.Stream.StreamD.Type
120120 , FoldManyPost (..)
121121 , foldMany
122122 , foldManyPost
123− , chunksOf
123+ , groupsOf
124124 , refoldMany
125125
126126 -- * Fold Iterate
@@ -1860,9 +1860,9 @@ foldMany (Fold fstep initial extract) (Stream step state) =
18601860 step' _ (FoldManyYield b next) = return $ Yield b next
18611861 step' _ FoldManyDone = return Stop
18621862
1863−{-# INLINE chunksOf #-}
1864−chunksOf :: Monad m => Int -> Fold m a b -> Stream m a -> Stream m b
1865−chunksOf n f = foldMany (FL.take n f)
1863+{-# INLINE groupsOf #-}
1864+groupsOf :: Monad m => Int -> Fold m a b -> Stream m a -> Stream m b
1865+groupsOf n f = foldMany (FL.take n f)
18661866
18671867-- Keep the argument order consistent with refoldIterateM.
18681868
core/src/Streamly/Internal/FileSystem/Dir.hs+1−1
@@ -448,7 +448,7 @@ writeChunksWithBufferOf n h = lpackArraysChunksOf n (writeChunks h)
448448-- @since 0.7.0
449449{-# INLINE writeWithBufferOf #-}
450450writeWithBufferOf :: MonadIO m => Int -> Handle -> Fold m Word8 ()
451−writeWithBufferOf n h = FL.chunksOf n (writeNUnsafe n) (writeChunks h)
451+writeWithBufferOf n h = FL.groupsOf n (writeNUnsafe n) (writeChunks h)
452452
453453-- > write = 'writeWithBufferOf' A.defaultChunkSize
454454--
core/src/Streamly/Internal/FileSystem/File.hs+2−2
@@ -103,7 +103,7 @@ import Prelude hiding (read)
103103import qualified Control.Monad.Catch as MC
104104import qualified System.IO as SIO
105105
106−import Streamly.Data.Fold (chunksOf, drain)
106+import Streamly.Data.Fold (groupsOf, drain)
107107import Streamly.Internal.Data.Array.Type (Array(..), writeNUnsafe)
108108import Streamly.Internal.Data.Fold.Type (Fold(..))
109109import Streamly.Data.Stream (Stream)
@@ -464,7 +464,7 @@ writeChunks path = Fold step initial extract
464464writeWith :: (MonadIO m, MonadCatch m)
465465 => Int -> FilePath -> Fold m Word8 ()
466466writeWith n path =
467− chunksOf n (writeNUnsafe n) (writeChunks path)
467+ groupsOf n (writeNUnsafe n) (writeChunks path)
468468
469469{-# DEPRECATED writeWithBufferOf "Please use 'writeWith' instead" #-}
470470{-# INLINE writeWithBufferOf #-}
core/src/Streamly/Internal/FileSystem/Handle.hs+2−2
@@ -500,11 +500,11 @@ writeChunksWithBufferOf = writeChunksWith
500500-- Bytes in the input stream are collected into a buffer until we have a chunk
501501-- of @reqSize@ and then written to the IO device.
502502--
503−-- >>> writeWith n h = Fold.chunksOf n (Array.writeNUnsafe n) (Handle.writeChunks h)
503+-- >>> writeWith n h = Fold.groupsOf n (Array.writeNUnsafe n) (Handle.writeChunks h)
504504--
505505{-# INLINE writeWith #-}
506506writeWith :: MonadIO m => Int -> Handle -> Fold m Word8 ()
507−writeWith n h = FL.chunksOf n (writeNUnsafe n) (writeChunks h)
507+writeWith n h = FL.groupsOf n (writeNUnsafe n) (writeChunks h)
508508
509509-- | Same as 'writeWith'
510510--
docs/User/ProjectRelated/Changelog.md+1
@@ -49,6 +49,7 @@ higher level operations with additional dependencies.
4949 * `serialWith` renamed to `splitWith`.
5050 * `variance`, and `stdDev` deprecated. Please use the
5151 `streamly-statistics` package instead.
52+ * `chunksOf` is renamed to `groupsOf`.
5253* In `Streamly.Network.Socket`:
5354 * `readWithBufferOf` renamed to `readWith`
5455 * `readChunksWithBufferOf` renamed to `readChunksWith`
src/Streamly/Internal/Data/Stream/IsStream/Reduce.hs+2−2
@@ -193,7 +193,7 @@ import qualified Streamly.Internal.Data.Parser.ParserD as PRD (Parser(..))
193193import qualified Streamly.Internal.Data.Stream.IsStream.Type as IsStream
194194import qualified Streamly.Internal.Data.Stream.StreamD as D
195195 ( foldMany
196− , chunksOf
196+ , groupsOf
197197 , refoldMany
198198 , foldIterateM
199199 , refoldIterateM
@@ -983,7 +983,7 @@ splitOnSuffixSeqAny _subseq _f _m = undefined
983983chunksOf
984984 :: (IsStream t, Monad m)
985985 => Int -> Fold m a b -> t m a -> t m b
986−chunksOf n f = fromStreamD . D.chunksOf n f . toStreamD
986+chunksOf n f = fromStreamD . D.groupsOf n f . toStreamD
987987
988988-- | @arraysOf n stream@ groups the elements in the input stream into arrays of
989989-- @n@ elements each.
src/Streamly/Internal/Network/Inet/TCP.hs+1−1
@@ -399,7 +399,7 @@ writeWithBufferOf
399399 -> PortNumber
400400 -> Fold m Word8 ()
401401writeWithBufferOf n addr port =
402− FL.chunksOf n (writeNUnsafe n) (writeChunks addr port)
402+ FL.groupsOf n (writeNUnsafe n) (writeChunks addr port)
403403
404404-- | Write a stream to the supplied IPv4 host address and port number.
405405--
src/Streamly/Internal/Network/Socket.hs+1−1
@@ -502,7 +502,7 @@ putBytesWith n h m = putChunks h $ A.arraysOf n m
502502--
503503{-# INLINE writeWith #-}
504504writeWith :: MonadIO m => Int -> Socket -> Fold m Word8 ()
505−writeWith n h = FL.chunksOf n (A.writeNUnsafe n) (writeChunks h)
505+writeWith n h = FL.groupsOf n (A.writeNUnsafe n) (writeChunks h)
506506
507507-- | Same as 'writeWith'
508508--