on master help sign in
rename flatten to concat

concat is the conventional naming for flattening, and we do not need this name for something else (e.g. flattening array of arrays). So we can reuse this for consistency.

commit9bf7415f0bfe660957a340188c347a1c713cc4b1 authorHarendra Kumar <harendra.kumar@gmail.com> dateSat 17 Aug 2019, 03:29 (7 years ago) parent1bfca9e
9 files changed+38−45
src/Streamly/Data/String.hs+1−2 src/Streamly/FileSystem/FD.hs+6−8 src/Streamly/FileSystem/File.hs+11−12 src/Streamly/FileSystem/Handle.hs+2−3 src/Streamly/Memory/Array.hs+2−2 src/Streamly/Memory/ArrayStream.hs+10−10 src/Streamly/Network/Client.hs+1−2 src/Streamly/Network/Socket.hs+3−4 test/Arrays.hs+2−2
src/Streamly/Data/String.hs+1−2
@@ -90,7 +90,6 @@ import Streamly.Memory.Array (Array)
9090
9191import qualified Streamly.Prelude as S
9292import qualified Streamly.Memory.Array as A
93−import qualified Streamly.Memory.ArrayStream as AS
9493import qualified Streamly.Streams.StreamD as D
9594
9695-- type String = List Char
@@ -276,4 +275,4 @@ unlines = AS.unlinesBy '\n'
276275-- > unwords . words /= id
277276{-# INLINE unwords #-}
278277unwords :: (MonadAsync m, IsStream t) => t m (Array Char) -> t m Char
279−unwords = AS.flatten . (S.intersperse (A.fromList " "))
278+unwords = A.concat . (S.intersperse (A.fromList " "))
src/Streamly/FileSystem/FD.hs+6−8
@@ -136,10 +136,9 @@ import Prelude hiding (read)
136136import qualified GHC.IO.FD as FD
137137import qualified GHC.IO.Device as RawIO
138138
139−import Streamly.Memory.Array.Types (Array(..))
139+import Streamly.Memory.Array.Types (Array(..), byteLength, defaultChunkSize)
140140import Streamly.Streams.Serial (SerialT)
141141import Streamly.Streams.StreamK.Type (IsStream, mkStream)
142−import Streamly.Memory.Array.Types (byteLength, defaultChunkSize)
143142
144143#if !defined(mingw32_HOST_OS)
145144import Streamly.Memory.Array.Types (groupIOVecsOf)
@@ -151,7 +150,6 @@ import qualified Streamly.FileSystem.FDIO as RawIO hiding (write)
151150-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
152151
153152import qualified Streamly.Memory.Array as A
154−import qualified Streamly.Memory.ArrayStream as AS
155153import qualified Streamly.Prelude as S
156154import qualified Streamly.Streams.StreamD.Type as D
157155
@@ -327,7 +325,7 @@ readArrays = readArraysOfUpto defaultChunkSize
327325--
328326{-# INLINE readInChunksOf #-}
329327readInChunksOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
330−readInChunksOf chunkSize h = AS.flatten $ readArraysOfUpto chunkSize h
328+readInChunksOf chunkSize h = A.concat $ readArraysOfUpto chunkSize h
331329
332330-- TODO
333331-- read :: (IsStream t, MonadIO m, Storable a) => Handle -> t m a
@@ -339,7 +337,7 @@ readInChunksOf chunkSize h = AS.flatten $ readArraysOfUpto chunkSize h
339337-- @since 0.7.0
340338{-# INLINE read #-}
341339read :: (IsStream t, MonadIO m) => Handle -> t m Word8
342−read = AS.flatten . readArrays
340+read = A.concat . readArrays
343341
344342-------------------------------------------------------------------------------
345343-- Writing
@@ -361,7 +359,7 @@ writeArrays h m = S.mapM_ (liftIO . writeArray h) m
361359{-# INLINE writeArraysPackedUpto #-}
362360writeArraysPackedUpto :: (MonadIO m, Storable a)
363361 => Int -> Handle -> SerialT m (Array a) -> m ()
364−writeArraysPackedUpto n h xs = writeArrays h $ AS.compact n xs
362+writeArraysPackedUpto n h xs = writeArrays h $ A.compact n xs
365363
366364#if !defined(mingw32_HOST_OS)
367365-- XXX this is incomplete
@@ -401,7 +399,7 @@ _writevArraysPackedUpto n h xs =
401399-- @since 0.7.0
402400{-# INLINE writeInChunksOf #-}
403401writeInChunksOf :: MonadIO m => Int -> Handle -> SerialT m Word8 -> m ()
404−writeInChunksOf n h m = writeArrays h $ AS.arraysOf n m
402+writeInChunksOf n h m = writeArrays h $ A.arraysOf n m
405403
406404-- > write = 'writeInChunksOf' A.defaultChunkSize
407405--
@@ -534,7 +532,7 @@ readSliceWith chunkSize h pos len = undefined
534532{-# INLINE readSlice #-}
535533readSlice :: (IsStream t, MonadIO m, Storable a)
536534 => Handle -> Int -> Int -> t m a
537−readSlice = readSliceWith A.defaultChunkSize
535+readSlice = readSliceWith defaultChunkSize
538536
539537-- | @readSliceRev h i count@ streams a slice from the file handle @h@ starting
540538-- at index @i@ and reading up to @count@ elements in the reverse direction
src/Streamly/FileSystem/File.hs+11−12
@@ -115,7 +115,7 @@ import Prelude hiding (read)
115115
116116import qualified System.IO as SIO
117117
118−import Streamly.Memory.Array.Types (Array(..))
118+import Streamly.Memory.Array.Types (Array(..), defaultChunkSize)
119119import Streamly.Streams.Serial (SerialT)
120120import Streamly.Streams.StreamK.Type (IsStream)
121121import Streamly.SVar (MonadAsync)
@@ -123,8 +123,7 @@ import Streamly.SVar (MonadAsync)
123123-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
124124
125125import qualified Streamly.FileSystem.Handle as FH
126−import qualified Streamly.Memory.ArrayStream as AS
127−import qualified Streamly.Memory.Array.Types as A hiding (flattenArrays)
126+import qualified Streamly.Memory.Array as A
128127import qualified Streamly.Prelude as S
129128
130129-------------------------------------------------------------------------------
@@ -203,7 +202,7 @@ readArraysOf size file = withFile file ReadMode (FH.readArraysOf size)
203202{-# INLINE readArrays #-}
204203readArrays :: (IsStream t, MonadCatch m, MonadIO m)
205204 => FilePath -> t m (Array Word8)
206−readArrays = readArraysOf A.defaultChunkSize
205+readArrays = readArraysOf defaultChunkSize
207206
208207-------------------------------------------------------------------------------
209208-- Read File to Stream
@@ -226,7 +225,7 @@ readInChunksOf chunkSize h = A.flattenArrays $ readArraysOf chunkSize h
226225-- TODO
227226-- read :: (IsStream t, MonadIO m, Storable a) => Handle -> t m a
228227--
229−-- > read = 'readByChunks' A.defaultChunkSize
228+-- > read = 'readByChunks' defaultChunkSize
230229-- | Generate a stream of elements of the given type from a file specified by
231230-- path. The stream ends when EOF is encountered. File is locked using multiple
232231-- reader and single writer locking mode.
@@ -234,7 +233,7 @@ readInChunksOf chunkSize h = A.flattenArrays $ readArraysOf chunkSize h
234233-- @since 0.7.0
235234{-# INLINE read #-}
236235read :: (IsStream t, MonadCatch m, MonadIO m) => FilePath -> t m Word8
237−read file = AS.flatten $ withFile file ReadMode FH.readArrays
236+read file = A.concat $ withFile file ReadMode FH.readArrays
238237
239238{-
240239-- | Generate a stream of elements of the given type from a file 'Handle'. The
@@ -290,9 +289,9 @@ writeArrays = writeArraysMode WriteMode
290289{-# INLINE writeInChunksOf #-}
291290writeInChunksOf :: (MonadAsync m, MonadCatch m)
292291 => Int -> FilePath -> SerialT m Word8 -> m ()
293−writeInChunksOf n file xs = writeArrays file $ AS.arraysOf n xs
292+writeInChunksOf n file xs = writeArrays file $ A.arraysOf n xs
294293
295−-- > write = 'writeInChunksOf' A.defaultChunkSize
294+-- > write = 'writeInChunksOf' defaultChunkSize
296295--
297296-- | Write a byte stream to a file. Combines the bytes in chunks of size
298297-- up to 'A.defaultChunkSize' before writing. If the file exists it is
@@ -302,7 +301,7 @@ writeInChunksOf n file xs = writeArrays file $ AS.arraysOf n xs
302301-- @since 0.7.0
303302{-# INLINE write #-}
304303write :: (MonadAsync m, MonadCatch m) => FilePath -> SerialT m Word8 -> m ()
305−write = writeInChunksOf A.defaultChunkSize
304+write = writeInChunksOf defaultChunkSize
306305
307306{-
308307{-# INLINE write #-}
@@ -326,7 +325,7 @@ appendArrays = writeArraysMode AppendMode
326325{-# INLINE appendByChunks #-}
327326appendByChunks :: (MonadAsync m, MonadCatch m)
328327 => Int -> FilePath -> SerialT m Word8 -> m ()
329−appendByChunks n file xs = appendArrays file $ AS.arraysOf n xs
328+appendByChunks n file xs = appendArrays file $ A.arraysOf n xs
330329
331330-- | Append a byte stream to a file. Combines the bytes in chunks of size up to
332331-- 'A.defaultChunkSize' before writing. If the file exists then the new data
@@ -336,7 +335,7 @@ appendByChunks n file xs = appendArrays file $ AS.arraysOf n xs
336335-- @since 0.7.0
337336{-# INLINE append #-}
338337append :: (MonadAsync m, MonadCatch m) => FilePath -> SerialT m Word8 -> m ()
339−append = appendByChunks A.defaultChunkSize
338+append = appendByChunks defaultChunkSize
340339
341340{-
342341-- | Like 'append' but the file is not locked for exclusive writes.
@@ -461,7 +460,7 @@ readSliceWith chunkSize h pos len = undefined
461460{-# INLINE readSlice #-}
462461readSlice :: (IsStream t, MonadIO m, Storable a)
463462 => Handle -> Int -> Int -> t m a
464−readSlice = readSliceWith A.defaultChunkSize
463+readSlice = readSliceWith defaultChunkSize
465464
466465-- | @readSliceRev h i count@ streams a slice from the file handle @h@ starting
467466-- at index @i@ and reading up to @count@ elements in the reverse direction
src/Streamly/FileSystem/Handle.hs+2−3
@@ -129,7 +129,6 @@ import Streamly.Fold (Fold)
129129import qualified Streamly.Fold as FL
130130import qualified Streamly.Fold.Types as FL
131131import qualified Streamly.Memory.Array as A
132−import qualified Streamly.Memory.ArrayStream as AS
133132import qualified Streamly.Streams.StreamD.Type as D
134133
135134-------------------------------------------------------------------------------
@@ -232,7 +231,7 @@ readArrays = readArraysOf defaultChunkSize
232231-- @since 0.7.0
233232{-# INLINE readInChunksOf #-}
234233readInChunksOf :: (IsStream t, MonadIO m) => Int -> Handle -> t m Word8
235−readInChunksOf chunkSize h = AS.flatten $ readArraysOf chunkSize h
234+readInChunksOf chunkSize h = A.concat $ readArraysOf chunkSize h
236235
237236-- TODO
238237-- Generate a stream of elements of the given type from a file 'Handle'.
@@ -244,7 +243,7 @@ readInChunksOf chunkSize h = AS.flatten $ readArraysOf chunkSize h
244243-- @since 0.7.0
245244{-# INLINE read #-}
246245read :: (IsStream t, MonadIO m) => Handle -> t m Word8
247−read = AS.flatten . readArrays
246+read = A.concat . readArrays
248247
249248-------------------------------------------------------------------------------
250249-- Writing
src/Streamly/Memory/Array.hs+2−2
@@ -125,7 +125,7 @@ module Streamly.Memory.Array
125125 , AS.arraysOf
126126
127127 -- ** Elimination
128− , AS.flatten
128+ , AS.concat
129129 -- , AS.concatRev
130130 , AS.unlinesBy
131131 , AS.toArray
@@ -143,7 +143,7 @@ import Foreign.ForeignPtr (withForeignPtr)
143143import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
144144import Foreign.Ptr (minusPtr, plusPtr)
145145import Foreign.Storable (Storable(..))
146−import Prelude hiding (length, null, last, map, (!!), read)
146+import Prelude hiding (length, null, last, map, (!!), read, concat)
147147
148148import Streamly.Memory.Array.Types (Array(..), length)
149149import Streamly.Streams.Serial (SerialT)
src/Streamly/Memory/ArrayStream.hs+10−10
@@ -24,8 +24,8 @@ module Streamly.Memory.ArrayStream
2424 arraysOf
2525
2626 -- * Flattening to elements
27− , flatten
28− -- , _flattenArraysRev
27+ , concat
28+ , concatRev
2929 , unlinesBy
3030
3131 -- * Transformation
@@ -43,7 +43,7 @@ import Data.Word (Word8)
4343import Foreign.ForeignPtr (withForeignPtr)
4444import Foreign.Ptr (minusPtr, plusPtr, castPtr)
4545import Foreign.Storable (Storable(..))
46−import Prelude hiding (length, null, last, map, (!!), read)
46+import Prelude hiding (length, null, last, map, (!!), read, concat)
4747
4848import Streamly.Memory.Array.Types (Array(..), length)
4949import Streamly.Streams.Serial (SerialT)
@@ -62,12 +62,12 @@ import qualified Streamly.Streams.Prelude as P
6262--
6363-- Same as the following but more efficient:
6464--
65−-- > flatten = S.concatMap A.read
65+-- > concat = S.concatMap A.read
6666--
6767-- @since 0.7.0
68−{-# INLINE flatten #-}
69−flatten :: (IsStream t, MonadIO m, Storable a) => t m (Array a) -> t m a
70−flatten m = D.fromStreamD $ A.flattenArrays (D.toStreamD m)
68+{-# INLINE concat #-}
69+concat :: (IsStream t, MonadIO m, Storable a) => t m (Array a) -> t m a
70+concat m = D.fromStreamD $ A.flattenArrays (D.toStreamD m)
7171
7272-- XXX should we have a reverseArrays API to reverse the stream of arrays
7373-- instead?
@@ -76,9 +76,9 @@ flatten m = D.fromStreamD $ A.flattenArrays (D.toStreamD m)
7676-- contents of each array before flattening.
7777--
7878-- @since 0.7.0
79−{-# INLINE _flattenArraysRev #-}
80−_flattenArraysRev :: (IsStream t, MonadIO m, Storable a) => t m (Array a) -> t m a
81−_flattenArraysRev m = D.fromStreamD $ A.flattenArraysRev (D.toStreamD m)
79+{-# INLINE concatRev #-}
80+concatRev :: (IsStream t, MonadIO m, Storable a) => t m (Array a) -> t m a
81+concatRev m = D.fromStreamD $ A.flattenArraysRev (D.toStreamD m)
8282
8383-- XXX use an Array instead as separator? Or use a separate unlinesArraysBySeq
8484-- API for that?
src/Streamly/Network/Client.hs+1−2
@@ -73,7 +73,6 @@ import Streamly.Streams.StreamK.Type (IsStream)
7373
7474import qualified Streamly.Fold.Types as FL
7575import qualified Streamly.Memory.Array as A
76−import qualified Streamly.Memory.ArrayStream as AS
7776import qualified Streamly.Prelude as S
7877import qualified Streamly.Network.Socket as SK
7978
@@ -111,7 +110,7 @@ withConnection addr port =
111110{-# INLINE read #-}
112111read :: (IsStream t, MonadCatch m, MonadIO m)
113112 => (Word8, Word8, Word8, Word8) -> PortNumber -> t m Word8
114−read addr port = AS.flatten $ withConnection addr port SK.readArrays
113+read addr port = A.concat $ withConnection addr port SK.readArrays
115114
116115-------------------------------------------------------------------------------
117116-- Writing
src/Streamly/Network/Socket.hs+3−4
@@ -95,8 +95,7 @@ import Streamly.Fold (Fold)
9595import qualified Streamly.Fold as FL
9696import qualified Streamly.Fold.Types as FL
9797import qualified Streamly.Memory.Array as A
98−import qualified Streamly.Memory.ArrayStream as AS
99−import qualified Streamly.Memory.Array.Types as A hiding (flattenArrays)
98+import qualified Streamly.Memory.Array.Types as A
10099import qualified Streamly.Prelude as S
101100
102101-- | @'withSocket' socket act@ runs the monadic computation @act@ passing the
@@ -266,7 +265,7 @@ readInChunksOf chunkSize h = A.flattenArrays $ readArraysUpto chunkSize h
266265-- @since 0.7.0
267266{-# INLINE read #-}
268267read :: (IsStream t, MonadIO m) => Socket -> t m Word8
269−read = AS.flatten . readArrays
268+read = A.concat . readArrays
270269
271270-------------------------------------------------------------------------------
272271-- Writing
@@ -302,7 +301,7 @@ writeArrays h = FL.drainBy (liftIO . writeArray h)
302301-- @since 0.7.0
303302{-# INLINE writeInChunksOfS #-}
304303writeInChunksOfS :: MonadIO m => Int -> Socket -> SerialT m Word8 -> m ()
305−writeInChunksOfS n h m = writeArraysS h $ AS.arraysOf n m
304+writeInChunksOfS n h m = writeArraysS h $ A.arraysOf n m
306305
307306-- | Write a byte stream to a socket. Accumulates the input in chunks of
308307-- specified number of bytes before writing.
test/Arrays.hs+2−2
@@ -84,7 +84,7 @@ testFlattenArrays =
8484 forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
8585 monadicIO $ do
8686 xs <- S.toList
87− $ A.flatten
87+ $ A.concat
8888 $ A.arraysOf 240
8989 $ S.fromList list
9090 assert (xs == list)
@@ -109,5 +109,5 @@ main = hspec
109109 prop "read . writeN n === id" $ testFromToStreamN
110110 prop "readRev . write === reverse" $ testToStreamRev
111111 prop "arraysOf concats to original" $ testArraysOf
112− prop "flattenArrays concats to original" $ testFlattenArrays
112+ prop "concats to original" $ testFlattenArrays
113113 prop "read . write === id" $ testFromToStream