Change the unfold/toStream naming for arrays (#2004)

This commit is contained in:
Harendra Kumar 2022-10-20 20:12:37 +05:30 committed by GitHub
parent e03cceb344
commit e8564258ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
33 changed files with 227 additions and 175 deletions

View File

@ -69,7 +69,7 @@ onArray
:: MonadIO m => Int -> (Stream.Stream m Int -> Stream.Stream m Int)
-> Stream Int
-> m (Stream Int)
onArray value f arr = S.fold (A.writeN value) $ f $ S.unfold A.read arr
onArray value f arr = S.fold (A.writeN value) $ f $ S.unfold A.reader arr
scanl' value n = composeN n $ onArray value $ S.scanl' (+) 0
scanl1' value n = composeN n $ onArray value $ S.scanl1' (+)
@ -98,7 +98,7 @@ showInstance = P.show
{-# INLINE pureFoldl' #-}
pureFoldl' :: MonadIO m => Stream Int -> m Int
pureFoldl' = S.foldl' (+) 0 . S.unfold A.read
pureFoldl' = S.foldl' (+) 0 . S.unfold A.reader
-------------------------------------------------------------------------------
-- Elimination
@ -106,11 +106,11 @@ pureFoldl' = S.foldl' (+) 0 . S.unfold A.read
{-# INLINE unfoldReadDrain #-}
unfoldReadDrain :: MonadIO m => Stream Int -> m ()
unfoldReadDrain = S.drain . S.unfold A.read
unfoldReadDrain = S.drain . S.unfold A.reader
{-# INLINE toStreamRevDrain #-}
toStreamRevDrain :: MonadIO m => Stream Int -> m ()
toStreamRevDrain = S.drain . A.toStreamRev
toStreamRevDrain = S.drain . A.readRev
-------------------------------------------------------------------------------
-- Bench groups

View File

@ -108,7 +108,7 @@ inspect $ 'toChunksSumLengths `hasNoType` ''Step
-- | Sum the bytes in a file.
toChunksCountBytes :: Handle -> IO Word8
toChunksCountBytes inh = do
let foldlArr' f z = runIdentity . Stream.foldl' f z . Array.toStream
let foldlArr' f z = runIdentity . Stream.foldl' f z . Array.read
let s = Handle.readChunks inh
Stream.foldl' (\acc arr -> acc + foldlArr' (+) 0 arr) 0 s

View File

@ -7,7 +7,7 @@ import Control.DeepSeq (deepseq)
import qualified Streamly.Internal.Data.Array.Unboxed as IA
import qualified GHC.Exts as GHC
import qualified Streamly.Data.Array.Unboxed as A
-- import qualified Streamly.Data.Array.Unboxed as A
import qualified Streamly.Internal.Data.Array.Unboxed as A
type Stream = A.Array

View File

@ -158,7 +158,7 @@ onArray
-> Stream Int
-> m (Stream Int)
onArray value f arr =
Stream.fold (MArray.writeN value) $ f $ Stream.unfold MArray.read arr
Stream.fold (MArray.writeN value) $ f $ Stream.unfold MArray.reader arr
-------------------------------------------------------------------------------
-- Elimination
@ -166,11 +166,11 @@ onArray value f arr =
{-# INLINE unfoldReadDrain #-}
unfoldReadDrain :: MonadIO m => Stream Int -> m ()
unfoldReadDrain = drain . Stream.unfold MArray.read
unfoldReadDrain = drain . Stream.unfold MArray.reader
{-# INLINE unfoldReadRevDrain #-}
unfoldReadRevDrain :: MonadIO m => Stream Int -> m ()
unfoldReadRevDrain = drain . Stream.unfold MArray.readRev
unfoldReadRevDrain = drain . Stream.unfold MArray.readerRev
{-# INLINE toStreamDRevDrain #-}
toStreamDRevDrain :: MonadIO m => Stream Int -> m ()
@ -183,7 +183,7 @@ toStreamDDrain = drain . Stream.fromStreamD . MArray.toStreamD
{-# INLINE unfoldFold #-}
unfoldFold :: MonadIO m => Stream Int -> m Int
unfoldFold = Stream.fold (Fold.foldl' (+) 0) . Stream.unfold MArray.read
unfoldFold = Stream.fold (Fold.foldl' (+) 0) . Stream.unfold MArray.reader
-------------------------------------------------------------------------------
-- Bench groups
@ -260,7 +260,7 @@ o_1_space_serial_marray value ~(array, indices) =
$ MArray.strip (> 0)
, benchIO' "modifyIndices (+ 1)" (const (return indices))
$ Stream.fold (MArray.modifyIndices (\_idx val -> val + 1) array)
. Stream.unfold Array.read
. Stream.unfold Array.reader
]
-------------------------------------------------------------------------------

View File

@ -189,7 +189,7 @@ getChunksConcatUnfoldCountLines inh =
$ IUS.lines FL.drain
$ SS.decodeLatin1
-- XXX replace with toBytes
$ S.unfoldMany A.read (IFH.readChunks inh)
$ S.unfoldMany A.reader (IFH.readChunks inh)
#ifdef INSPECTION
inspect $ hasNoTypeClasses 'getChunksConcatUnfoldCountLines

View File

@ -134,7 +134,7 @@ _readChunks inh devNull = IUF.fold fld unf inh
where
fld = FH.write devNull
unf = IUF.many A.read FH.chunkReader
unf = IUF.many A.reader FH.chunkReader
-- | Send the chunk content to /dev/null
-- Implicitly benchmarked via 'readWithFromBytesNull'
@ -144,7 +144,7 @@ _readChunksWith inh devNull = IUF.fold fld unf (defaultChunkSize, inh)
where
fld = FH.write devNull
unf = IUF.many A.read FH.chunkReaderWith
unf = IUF.many A.reader FH.chunkReaderWith
o_1_space_copy_fromBytes :: BenchEnv -> [Benchmark]
o_1_space_copy_fromBytes env =

View File

@ -65,10 +65,10 @@ arrInput file = second (fmap Array.fromList) (strInput file)
benchFunctions :: [(String, Array Char -> IO ())]
benchFunctions =
[ ("NFD", IsStream.drain . normalize NFD . Array.toStream)
, ("NFKD", IsStream.drain . normalize NFKD . Array.toStream)
, ("NFC", IsStream.drain . normalize NFC . Array.toStream)
, ("NFKC", IsStream.drain . normalize NFKC . Array.toStream)
[ ("NFD", IsStream.drain . normalize NFD . Array.read)
, ("NFKD", IsStream.drain . normalize NFKD . Array.read)
, ("NFC", IsStream.drain . normalize NFC . Array.read)
, ("NFKC", IsStream.drain . normalize NFKC . Array.read)
]
main :: IO ()

View File

@ -87,7 +87,7 @@ linesUnlinesCopy inh outh =
linesUnlinesArrayWord8Copy :: Handle -> Handle -> IO ()
linesUnlinesArrayWord8Copy inh outh =
Stream.fold (Handle.write outh)
$ Stream.interposeSuffix 10 Array.read
$ Stream.interposeSuffix 10 Array.reader
$ Stream.splitOnSuffix (== 10) Array.write
$ Stream.unfold Handle.reader inh

View File

@ -22,19 +22,19 @@ module Streamly.Data.Array
, A.writeN
, A.write
-- * Elimination
, A.toStream
, A.toStreamRev
-- * Streams
, A.read
, A.readRev
-- * Unfolds
, A.reader
-- * Random Access
, A.length
-- * Folding Arrays
, A.streamFold
-- , A.streamFold
, A.fold
, A.length
)
where

View File

@ -1,5 +1,3 @@
#include "inline.hs"
-- |
-- Module : Streamly.Data.Array.Unboxed
-- Copyright : (c) 2019 Composewell Technologies
@ -53,11 +51,11 @@
module Streamly.Data.Array.Unboxed
(
-- * Types
Unbox (..)
, A.Array
-- * Arrays
-- ** Construction
-- * Construction
-- | When performance matters, the fastest way to generate an array is
-- 'writeN'. 'IsList' and 'IsString' instances can be
-- used to conveniently construct arrays from literal values.
@ -74,27 +72,37 @@ module Streamly.Data.Array.Unboxed
, A.write -- full buffer
, writeLastN -- drop old (ring buffer)
-- ** Elimination
-- * Conversion
-- 'GHC.Exts.toList' from "GHC.Exts" can be used to convert an array to a
-- list.
, A.toList
, A.read
, A.readRev
-- ** Casting
-- * Unfolds
, A.reader
, A.readerRev
-- * Casting
, cast
, asBytes
-- ** Random Access
-- * Random Access
, A.length
-- , (!!)
, A.getIndex
-- * Deprecated
, read
, readRev
)
where
import Streamly.Internal.Data.Array.Unboxed as A
#include "inline.hs"
import Streamly.Internal.Data.Unfold (Unfold)
import Streamly.Internal.Data.Array.Unboxed as A hiding (read, readRev)
import Streamly.Internal.Data.Unboxed (Unbox (..))
import Prelude hiding (read)
-- $setup
-- >>> :m
@ -104,3 +112,17 @@ import Streamly.Internal.Data.Unboxed (Unbox (..))
-- >>> import Streamly.Data.Array.Unboxed (Array)
-- >>> import qualified Streamly.Internal.Data.Stream as Stream
-- >>> import qualified Streamly.Data.Array.Unboxed as Array
-- | Same as 'reader'
--
{-# DEPRECATED read "Please use 'reader' instead" #-}
{-# INLINE_NORMAL read #-}
read :: (Monad m, Unbox a) => Unfold m (Array a) a
read = reader
-- | Same as 'readerRev'
--
{-# DEPRECATED readRev "Please use 'readerRev' instead" #-}
{-# INLINE_NORMAL readRev #-}
readRev :: (Monad m, Unbox a) => Unfold m (Array a) a
readRev = readerRev

View File

@ -13,11 +13,11 @@
module Streamly.Data.Array.Unboxed.Mut
(
-- * Types
Unbox (..)
, Array
-- * Arrays
-- ** Construction
-- * Construction
-- Uninitialized Arrays
, new
@ -30,29 +30,31 @@ module Streamly.Data.Array.Unboxed.Mut
, write -- full buffer
-- writeLastN
-- ** Appending elements
-- * Appending elements
, snoc
-- ** Appending streams
-- * Appending streams
, appendN
, append
-- ** Inplace mutation
-- * Inplace mutation
, putIndex
-- ** Random access
-- * Random access
, getIndex
-- ** Elimination
-- * Conversion
, toList
, read
, readRev
-- ** Casting
-- * Unfolds
, reader
, readerRev
-- * Casting
, cast
, asBytes
-- ** Size
-- * Size
, length
)
where

View File

@ -30,13 +30,13 @@ module Streamly.Internal.Data.Array
-- * Elimination
, length
, read
, reader
, toList
, toStreamD
, toStreamDRev
, toStream
, toStreamRev
, readStreamD
, readRevStreamD
, read
, readRev
, foldl'
, foldr
@ -173,9 +173,9 @@ fromList xs = unsafePerformIO $ fromStreamD $ D.fromList xs
length :: Array a -> Int
length = arrLen
{-# INLINE_NORMAL read #-}
read :: MonadIO m => Unfold m (Array a) a
read = Unfold.lmap unsafeThaw MArray.read
{-# INLINE_NORMAL reader #-}
reader :: MonadIO m => Unfold m (Array a) a
reader = Unfold.lmap unsafeThaw MArray.reader
-------------------------------------------------------------------------------
-- Elimination - to streams
@ -191,23 +191,23 @@ toList arr = loop 0
loop i | i == len = []
loop i = getIndexUnsafe arr i : loop (i + 1)
{-# INLINE_NORMAL toStreamD #-}
toStreamD :: MonadIO m => Array a -> D.Stream m a
toStreamD = MArray.toStreamD . unsafeThaw
{-# INLINE_NORMAL readStreamD #-}
readStreamD :: MonadIO m => Array a -> D.Stream m a
readStreamD = MArray.toStreamD . unsafeThaw
{-# INLINE_NORMAL toStreamDRev #-}
toStreamDRev :: Monad m => Array a -> D.Stream m a
toStreamDRev arr@Array{..} =
{-# INLINE_NORMAL readRevStreamD #-}
readRevStreamD :: Monad m => Array a -> D.Stream m a
readRevStreamD arr@Array{..} =
D.map (getIndexUnsafe arr)
$ D.enumerateFromThenToIntegral (arrLen - 1) (arrLen - 2) 0
{-# INLINE_EARLY toStream #-}
toStream :: MonadIO m => Array a -> Stream m a
toStream = Stream.fromStreamD . toStreamD
{-# INLINE_EARLY read #-}
read :: MonadIO m => Array a -> Stream m a
read = Stream.fromStreamD . readStreamD
{-# INLINE_EARLY toStreamRev #-}
toStreamRev :: Monad m => Array a -> Stream m a
toStreamRev = Stream.fromStreamD . toStreamDRev
{-# INLINE_EARLY readRev #-}
readRev :: Monad m => Array a -> Stream m a
readRev = Stream.fromStreamD . readRevStreamD
-------------------------------------------------------------------------------
-- Elimination - using Folds
@ -215,11 +215,11 @@ toStreamRev = Stream.fromStreamD . toStreamDRev
{-# INLINE_NORMAL foldl' #-}
foldl' :: (b -> a -> b) -> b -> Array a -> b
foldl' f z arr = unsafePerformIO $ D.foldl' f z $ toStreamD arr
foldl' f z arr = unsafePerformIO $ D.foldl' f z $ readStreamD arr
{-# INLINE_NORMAL foldr #-}
foldr :: (a -> b -> b) -> b -> Array a -> b
foldr f z arr = unsafePerformIO $ D.foldr f z $ toStreamD arr
foldr f z arr = unsafePerformIO $ D.foldr f z $ readStreamD arr
instance NFData a => NFData (Array a) where
{-# INLINE rnf #-}
@ -227,11 +227,11 @@ instance NFData a => NFData (Array a) where
{-# INLINE fold #-}
fold :: MonadIO m => Fold m a b -> Array a -> m b
fold f arr = D.fold f (toStreamD arr)
fold f arr = D.fold f (readStreamD arr)
{-# INLINE streamFold #-}
streamFold :: MonadIO m => (Stream m a -> m b) -> Array a -> m b
streamFold f arr = f (toStream arr)
streamFold f arr = f (read arr)
-------------------------------------------------------------------------------
-- Random reads and writes

View File

@ -84,9 +84,10 @@ module Streamly.Internal.Data.Array.Mut.Type
-- * Eliminating and Reading
-- ** To streams
, read
-- , readRev
-- ** Unfolds
, reader
-- , readerRev
, producer -- experimental
-- ** To containers
, toStreamD
@ -95,9 +96,6 @@ module Streamly.Internal.Data.Array.Mut.Type
-- , toStreamKRev
, toList
-- experimental
, producer
-- ** Random reads
, getIndex
, getIndexUnsafe
@ -570,9 +568,9 @@ producer = Producer step inject extract
-- | Unfold an array into a stream.
--
{-# INLINE_NORMAL read #-}
read :: MonadIO m => Unfold m (Array a) a
read = Producer.simplify producer
{-# INLINE_NORMAL reader #-}
reader :: MonadIO m => Unfold m (Array a) a
reader = Producer.simplify producer
--------------------------------------------------------------------------------
-- Appending arrays

View File

@ -49,12 +49,17 @@ module Streamly.Internal.Data.Array.Unboxed
, writeLastN
-- * Elimination
-- ** Conversion
, A.toList
, A.toStream
, A.toStreamRev
, read
, unsafeRead -- XXX readUnsafe?
-- ** Streams
, A.read
, A.readRev
-- ** Unfolds
, reader
, readerUnsafe
, A.readerRev
, producer -- experimental
-- * Random Access
@ -103,6 +108,10 @@ module Streamly.Internal.Data.Array.Unboxed
-- ** Folding
, streamFold
, fold
-- * Deprecated
, A.toStream
, A.toStreamRev
)
where
@ -187,12 +196,10 @@ producer =
-- | Unfold an array into a stream.
--
-- /Since 0.7.0 (Streamly.Memory.Array)/
--
-- @since 0.8.0
{-# INLINE_NORMAL read #-}
read :: forall m a. (Monad m, Unboxed a) => Unfold m (Array a) a
read = Producer.simplify producer
-- @since 0.9.0
{-# INLINE_NORMAL reader #-}
reader :: forall m a. (Monad m, Unboxed a) => Unfold m (Array a) a
reader = Producer.simplify producer
-- | Unfold an array into a stream, does not check the end of the array, the
-- user is responsible for terminating the stream within the array bounds. For
@ -205,9 +212,9 @@ read = Producer.simplify producer
--
-- /Pre-release/
--
{-# INLINE_NORMAL unsafeRead #-}
unsafeRead :: forall m a. (Monad m, Unboxed a) => Unfold m (Array a) a
unsafeRead = Unfold step inject
{-# INLINE_NORMAL readerUnsafe #-}
readerUnsafe :: forall m a. (Monad m, Unboxed a) => Unfold m (Array a) a
readerUnsafe = Unfold step inject
where
inject (Array contents start end) =
@ -484,7 +491,7 @@ runPipe f arr = P.runPipe (toArrayMinChunk (length arr)) $ f (A.read arr)
streamTransform :: forall m a b. (MonadIO m, Unboxed a, Unboxed b)
=> (Stream m a -> Stream m b) -> Array a -> m (Array b)
streamTransform f arr =
P.fold (A.writeWith (length arr)) $ Stream.toStreamK $ f (A.toStream arr)
P.fold (A.writeWith (length arr)) $ Stream.toStreamK $ f (A.read arr)
-------------------------------------------------------------------------------
-- Casts
@ -544,16 +551,18 @@ asCStringUnsafe arr act = do
-- Folds
-------------------------------------------------------------------------------
-- XXX We can directly use toStreamD and D.fold here.
-- | Fold an array using a 'Fold'.
--
-- /Pre-release/
{-# INLINE fold #-}
fold :: forall m a b. (Monad m, Unboxed a) => Fold m a b -> Array a -> m b
fold f arr = P.fold f (Stream.toStreamK (A.toStream arr))
fold f arr = P.fold f (Stream.toStreamK (A.read arr))
-- | Fold an array using a stream fold operation.
--
-- /Pre-release/
{-# INLINE streamFold #-}
streamFold :: (Monad m, Unboxed a) => (Stream m a -> m b) -> Array a -> m b
streamFold f arr = f (A.toStream arr)
streamFold f arr = f (A.read arr)

View File

@ -96,9 +96,9 @@ module Streamly.Internal.Data.Array.Unboxed.Mut.Type
-- * Eliminating and Reading
-- ** To streams
, read
, readRevWith
, readRev
, reader
, readerRevWith
, readerRev
-- ** To containers
, toStreamDWith
@ -1088,7 +1088,7 @@ getSlice index len (Array contents start e _) =
-- XXX consider the bulk update/accumulation/permutation APIs from vector.
-- | You may not need to reverse an array because you can consume it in reverse
-- using 'readRev'. To reverse large arrays you can read in reverse and write
-- using 'readerRev'. To reverse large arrays you can read in reverse and write
-- to another array. However, in-place reverse can be useful to take adavantage
-- of cache locality and when you do not want to allocate additional memory.
--
@ -1350,9 +1350,9 @@ data FlattenState s contents a =
OuterLoop s
| InnerLoop s contents !Int !Int
-- | Use the "read" unfold instead.
-- | Use the "reader" unfold instead.
--
-- @flattenArrays = unfoldMany read@
-- @flattenArrays = unfoldMany reader@
--
-- We can try this if there are any fusion issues in the unfold.
--
@ -1379,9 +1379,9 @@ flattenArrays (D.Stream step state) = D.Stream step' (OuterLoop state)
x <- liftIO $ peekWith contents p
return $ D.Yield x (InnerLoop st contents (INDEX_NEXT(p,a)) end)
-- | Use the "readRev" unfold instead.
-- | Use the "readerRev" unfold instead.
--
-- @flattenArrays = unfoldMany readRev@
-- @flattenArrays = unfoldMany readerRev@
--
-- We can try this if there are any fusion issues in the unfold.
--
@ -1459,15 +1459,15 @@ producer = producerWith liftIO
-- | Unfold an array into a stream.
--
-- @since 0.7.0
{-# INLINE_NORMAL read #-}
read :: forall m a. (MonadIO m, Unboxed a) => Unfold m (Array a) a
read = Producer.simplify producer
{-# INLINE_NORMAL reader #-}
reader :: forall m a. (MonadIO m, Unboxed a) => Unfold m (Array a) a
reader = Producer.simplify producer
{-# INLINE_NORMAL readRevWith #-}
readRevWith ::
{-# INLINE_NORMAL readerRevWith #-}
readerRevWith ::
forall m a. (Monad m, Unboxed a)
=> (forall b. IO b -> m b) -> Unfold m (Array a) a
readRevWith liftio = Unfold step inject
readerRevWith liftio = Unfold step inject
where
inject (Array contents start end _) =
@ -1483,9 +1483,9 @@ readRevWith liftio = Unfold step inject
-- | Unfold an array into a stream in reverse order.
--
-- /Pre-release/
{-# INLINE_NORMAL readRev #-}
readRev :: forall m a. (MonadIO m, Unboxed a) => Unfold m (Array a) a
readRev = readRevWith liftIO
{-# INLINE_NORMAL readerRev #-}
readerRev :: forall m a. (MonadIO m, Unboxed a) => Unfold m (Array a) a
readerRev = readerRevWith liftIO
-------------------------------------------------------------------------------
-- to Lists and streams
@ -1543,9 +1543,9 @@ toStreamDWith liftio Array{..} = D.Stream step arrStart
r <- peekWith arrContents p
return $ D.Yield r (INDEX_NEXT(p,a))
-- | Use the 'read' unfold instead.
-- | Use the 'reader' unfold instead.
--
-- @toStreamD = D.unfold read@
-- @toStreamD = D.unfold reader@
--
-- We can try this if the unfold has any performance issues.
{-# INLINE_NORMAL toStreamD #-}
@ -1585,9 +1585,9 @@ toStreamDRevWith liftio Array{..} =
r <- peekWith arrContents p
return $ D.Yield r (INDEX_PREV(p,a))
-- | Use the 'readRev' unfold instead.
-- | Use the 'readerRev' unfold instead.
--
-- @toStreamDRev = D.unfold readRev@
-- @toStreamDRev = D.unfold readerRev@
--
-- We can try this if the unfold has any perf issues.
{-# INLINE_NORMAL toStreamDRev #-}
@ -1970,7 +1970,7 @@ fromArrayStreamK :: (Unboxed a, MonadIO m) =>
K.Stream m (Array a) -> m (Array a)
fromArrayStreamK as = do
len <- arrayStreamKLength as
fromStreamDN len $ D.unfoldMany read $ D.fromStreamK as
fromStreamDN len $ D.unfoldMany reader $ D.fromStreamK as
-- CAUTION: a very large number (millions) of arrays can degrade performance
-- due to GC overhead because we need to buffer the arrays before we flatten

View File

@ -130,18 +130,18 @@ arraysOf n str = fromStreamD $ A.arraysOf n (toStreamD str)
concat :: (Monad m, Unboxed a) => Stream m (Array a) -> Stream m a
-- concat m = fromStreamD $ A.flattenArrays (toStreamD m)
-- concat m = fromStreamD $ D.concatMap A.toStreamD (toStreamD m)
concat m = fromStreamD $ D.unfoldMany A.read (toStreamD m)
concat m = fromStreamD $ D.unfoldMany A.reader (toStreamD m)
-- | Convert a stream of arrays into a stream of their elements reversing the
-- contents of each array before flattening.
--
-- > concatRev = Stream.unfoldMany Array.readRev
-- > concatRev = Stream.unfoldMany Array.readerRev
--
-- @since 0.7.0
{-# INLINE concatRev #-}
concatRev :: (Monad m, Unboxed a) => Stream m (Array a) -> Stream m a
-- concatRev m = fromStreamD $ A.flattenArraysRev (toStreamD m)
concatRev m = fromStreamD $ D.unfoldMany A.readRev (toStreamD m)
concatRev m = fromStreamD $ D.unfoldMany A.readerRev (toStreamD m)
-------------------------------------------------------------------------------
-- Intersperse and append
@ -153,12 +153,12 @@ concatRev m = fromStreamD $ D.unfoldMany A.readRev (toStreamD m)
-- /Pre-release/
{-# INLINE interpose #-}
interpose :: (Monad m, Unboxed a) => a -> Stream m (Array a) -> Stream m a
interpose x = S.interpose x A.read
interpose x = S.interpose x A.reader
{-# INLINE intercalateSuffix #-}
intercalateSuffix :: (Monad m, Unboxed a)
=> Array a -> Stream m (Array a) -> Stream m a
intercalateSuffix = S.intercalateSuffix A.read
intercalateSuffix = S.intercalateSuffix A.reader
-- | Flatten a stream of arrays appending the given element after each
-- array.
@ -168,7 +168,7 @@ intercalateSuffix = S.intercalateSuffix A.read
interposeSuffix :: (Monad m, Unboxed a)
=> a -> Stream m (Array a) -> Stream m a
-- interposeSuffix x = fromStreamD . A.unlines x . toStreamD
interposeSuffix x = S.interposeSuffix x A.read
interposeSuffix x = S.interposeSuffix x A.reader
data FlattenState s a =
OuterLoop s

View File

@ -39,7 +39,7 @@ module Streamly.Internal.Data.Array.Unboxed.Type
-- * Elimination
, unsafeIndexIO
, unsafeIndex
, unsafeIndex -- getIndexUnsafe
, byteLength
, length
@ -47,13 +47,15 @@ module Streamly.Internal.Data.Array.Unboxed.Type
, foldr
, splitAt
, readRev
, toStreamD
, toStreamDRev
, toStreamK
, toStreamKRev
, toStream
, toStreamRev
, read
, readRev
, readerRev
, toList
-- * Folds
@ -341,10 +343,10 @@ length arr = MA.length (unsafeThaw arr)
-- | Unfold an array into a stream in reverse order.
--
-- @since 0.8.0
{-# INLINE_NORMAL readRev #-}
readRev :: forall m a. (Monad m, Unboxed a) => Unfold m (Array a) a
readRev = Unfold.lmap unsafeThaw $ MA.readRevWith (return . unsafeInlineIO)
-- @since 0.9.0
{-# INLINE_NORMAL readerRev #-}
readerRev :: forall m a. (Monad m, Unboxed a) => Unfold m (Array a) a
readerRev = Unfold.lmap unsafeThaw $ MA.readerRevWith (return . unsafeInlineIO)
{-# INLINE_NORMAL toStreamD #-}
toStreamD :: forall m a. (Monad m, Unboxed a) => Array a -> D.Stream m a
@ -367,9 +369,16 @@ toStreamKRev arr =
-- | Convert an 'Array' into a stream.
--
-- /Pre-release/
{-# INLINE_EARLY read #-}
read :: (Monad m, Unboxed a) => Array a -> Stream m a
read = Stream.fromStreamD . toStreamD
-- | Same as 'read'
--
{-# DEPRECATED toStream "Please use 'read' instead." #-}
{-# INLINE_EARLY toStream #-}
toStream :: (Monad m, Unboxed a) => Array a -> Stream m a
toStream = Stream.fromStreamD . toStreamD
toStream = read
-- XXX add fallback to StreamK rule
-- {-# RULES "Streamly.Array.read fallback to StreamK" [1]
-- forall a. S.readK (read a) = K.fromArray a #-}
@ -377,9 +386,16 @@ toStream = Stream.fromStreamD . toStreamD
-- | Convert an 'Array' into a stream in reverse order.
--
-- /Pre-release/
{-# INLINE_EARLY readRev #-}
readRev :: (Monad m, Unboxed a) => Array a -> Stream m a
readRev = Stream.fromStreamD . toStreamDRev
-- | Same as 'readRev'
--
{-# DEPRECATED toStreamRev "Please use 'readRev' instead." #-}
{-# INLINE_EARLY toStreamRev #-}
toStreamRev :: (Monad m, Unboxed a) => Array a -> Stream m a
toStreamRev = Stream.fromStreamD . toStreamDRev
toStreamRev = readRev
-- XXX add fallback to StreamK rule
-- {-# RULES "Streamly.Array.readRev fallback to StreamK" [1]

View File

@ -376,8 +376,8 @@ joinOuter eq s1 s =
where
leftOver inputArr foundArr =
let stream1 = Array.toStream inputArr
stream2 = Stream.unfold MA.read foundArr
let stream1 = Array.read inputArr
stream2 = Stream.unfold MA.reader foundArr
in Stream.filter
isJust
( Stream.zipWith (\x y ->
@ -399,7 +399,7 @@ joinOuter eq s1 s =
then Stream.nil
else Stream.fromPure Nothing
(i, b) <-
let stream = Array.toStream inputArr
let stream = Array.read inputArr
in Stream.indexed $ fmap Just (Stream.liftInner stream) <> final
case b of

View File

@ -332,12 +332,12 @@ chunkReader = UF.first defaultChunkSize chunkReaderWith
-- | Unfolds the tuple @(bufsize, handle)@ into a byte stream, read requests
-- to the IO device are performed using buffers of @bufsize@.
--
-- >>> readerWith = Unfold.many Array.read Handle.chunkReaderWith
-- >>> readerWith = Unfold.many Array.reader Handle.chunkReaderWith
--
-- @since 0.9.0
{-# INLINE readerWith #-}
readerWith :: MonadIO m => Unfold m (Int, Handle) Word8
readerWith = UF.many A.read chunkReaderWith
readerWith = UF.many A.reader chunkReaderWith
-- | Same as 'readerWith'
--
@ -350,7 +350,7 @@ readWithBufferOf = readerWith
-- | @readWith bufsize handle@ reads a byte stream from a file
-- handle, reads are performed in chunks of up to @bufsize@.
--
-- >>> readWith size h = Stream.unfoldMany Array.read $ Handle.readChunksWith size h
-- >>> readWith size h = Stream.unfoldMany Array.reader $ Handle.readChunksWith size h
--
-- /Pre-release/
{-# INLINE readWith #-}
@ -361,16 +361,16 @@ readWith size h = AS.concat $ readChunksWith size h
-- performed in sizes of
-- 'Streamly.Internal.Data.Array.Unboxed.Type.defaultChunkSize'.
--
-- >>> reader = Unfold.many Array.read chunkReader
-- >>> reader = Unfold.many Array.reader chunkReader
--
-- @since 0.9.0
{-# INLINE reader #-}
reader :: MonadIO m => Unfold m Handle Word8
reader = UF.many A.read chunkReader
reader = UF.many A.reader chunkReader
-- | Generate a byte stream from a file 'Handle'.
--
-- >>> read h = Stream.unfoldMany Array.read $ Handle.readChunks h
-- >>> read h = Stream.unfoldMany Array.reader $ Handle.readChunks h
--
-- /Pre-release/
{-# INLINE read #-}

View File

@ -72,7 +72,7 @@ words = S.words A.write
-- > unlines . lines /= id
{-# INLINE unlines #-}
unlines :: MonadIO m => Stream m (Array Char) -> Stream m Char
unlines = S.unlines A.read
unlines = S.unlines A.reader
-- | Flattens the stream of @Array Char@, after appending a separating
-- space to each string.
@ -89,4 +89,4 @@ unlines = S.unlines A.read
-- > unwords . words /= id
{-# INLINE unwords #-}
unwords :: MonadIO m => Stream m (Array Char) -> Stream m Char
unwords = S.unwords A.read
unwords = S.unwords A.reader

View File

@ -182,7 +182,7 @@ import Streamly.Internal.Data.Unboxed (Unboxed)
import qualified Data.Heap as H
import qualified Streamly.Data.Unfold as Unfold
import qualified Streamly.Internal.Data.Array.Unboxed.Type as A
(arraysOf, toStream)
(arraysOf, read)
import qualified Streamly.Internal.Data.Fold as FL
(Fold, Step(..), takeEndBy_, takeEndBy, catMaybes, take)
import qualified Streamly.Internal.Data.IsMap as IsMap
@ -843,7 +843,7 @@ splitBySeq
:: (IsStream t, MonadAsync m, Unboxed a, Enum a, Eq a)
=> Array a -> Fold m a b -> t m a -> t m b
splitBySeq patt f m =
intersperseM (fold f (A.toStream patt)) $ splitOnSeq patt f m
intersperseM (fold f (A.read patt)) $ splitOnSeq patt f m
-- | Like 'splitSuffixBy' but the separator is a sequence of elements, instead
-- of a predicate for a single element.

View File

@ -68,7 +68,7 @@ import Streamly.Internal.Data.Time.Units (NanoSecond64(..), toRelTime64)
import qualified Data.List as List
import qualified Data.Map.Strict as Map
import qualified Streamly.Internal.Data.Array as Array
(fromStream, length, toStream)
(fromStream, length, read)
import qualified Streamly.Data.Array.Unboxed.Mut as MA
import qualified Streamly.Internal.Data.Fold as Fold
(one, last, toStream, toStreamRev)
@ -498,8 +498,8 @@ joinOuter eq s1 s =
where
leftOver inputArr foundArr =
let stream1 = IsStream.fromSerial $ Array.toStream inputArr
stream2 = Stream.unfold MA.read foundArr
let stream1 = IsStream.fromSerial $ Array.read inputArr
stream2 = Stream.unfold MA.reader foundArr
in Stream.filter
isJust
( Stream.zipWith (\x y ->
@ -521,7 +521,7 @@ joinOuter eq s1 s =
then Stream.nil
else Stream.fromPure Nothing
(i, b) <-
let stream = IsStream.fromSerial $ Array.toStream inputArr
let stream = IsStream.fromSerial $ Array.read inputArr
in Stream.indexed $ fmap Just (Stream.liftInner stream) <> final
case b of

View File

@ -624,7 +624,7 @@ toUtf8 :: MonadIO m => String -> m (Array Word8)
toUtf8 = A.fromStream . U.encodeUtf8 . S.fromList
utf8ToString :: Array Word8 -> String
utf8ToString = runIdentity . S.fold FL.toList . U.decodeUtf8' . A.toStream
utf8ToString = runIdentity . S.fold FL.toList . U.decodeUtf8' . A.read
-- | Add a trailing "/" at the end of the path if there is none. Do not add a
-- "/" if the path is empty.

View File

@ -433,7 +433,7 @@ pathsToHandles paths cfg = do
-------------------------------------------------------------------------------
utf8ToString :: Array Word8 -> FilePath
utf8ToString = runIdentity . S.fold Fold.toList . U.decodeUtf8 . A.toStream
utf8ToString = runIdentity . S.fold Fold.toList . U.decodeUtf8 . A.read
utf8ToStringList :: NonEmpty (Array Word8) -> NonEmpty FilePath
utf8ToStringList = NonEmpty.map utf8ToString

View File

@ -295,7 +295,7 @@ readerWith = usingFile2 FH.readerWith
-- /Pre-release/
{-# INLINE reader #-}
reader :: (MonadCatch m, MonadAsync m) => Unfold m FilePath Word8
reader = UF.many A.read (usingFile FH.chunkReader)
reader = UF.many A.reader (usingFile FH.chunkReader)
-- | Generate a stream of bytes from a file specified by path. The stream ends
-- when EOF is encountered. File is locked using multiple reader and single

View File

@ -320,7 +320,7 @@ withConnection addr port =
{-# INLINE reader #-}
reader :: (MonadCatch m, MonadAsync m)
=> Unfold m ((Word8, Word8, Word8, Word8), PortNumber) Word8
reader = UF.many A.read (usingConnection ISK.chunkReader)
reader = UF.many A.reader (usingConnection ISK.chunkReader)
-- | Read a stream from the supplied IPv4 host address and port number.
--

View File

@ -95,7 +95,7 @@ import Streamly.Internal.Data.Unfold.Type (Unfold(..))
-- import Streamly.String (encodeUtf8, decodeUtf8, foldLines)
import Streamly.Internal.System.IO (defaultChunkSize)
import qualified Streamly.Data.Array.Unboxed as A (read, length, writeN)
import qualified Streamly.Data.Array.Unboxed as A (reader, length, writeN)
import qualified Streamly.Data.Fold as FL
import qualified Streamly.Internal.Data.Array.Unboxed.Type as A
(unsafeFreeze, asPtrUnsafe, byteLength, writeNUnsafe)
@ -433,7 +433,7 @@ read = readWith defaultChunkSize
-- @since 0.9.0
{-# INLINE readerWith #-}
readerWith :: MonadIO m => Unfold m (Int, Socket) Word8
readerWith = UF.many A.read chunkReaderWith
readerWith = UF.many A.reader chunkReaderWith
-- | Same as 'readWith'
--

View File

@ -27,7 +27,8 @@ import Data.Word (Word8)
import Streamly.Data.Array.Unboxed (Array)
import System.IO.Unsafe (unsafePerformIO)
import qualified Streamly.Internal.Data.Array.Unboxed as Array (fromStreamN, toStream)
import qualified Streamly.Internal.Data.Array.Unboxed as Array
(fromStreamN, read)
import qualified Streamly.Internal.Data.Stream.IsStream as Stream
import qualified Streamly.Internal.Unicode.Stream as Unicode
@ -64,4 +65,4 @@ pack s =
unpack :: Utf8 -> String
unpack u =
unsafePerformIO
$ Stream.toList $ Unicode.decodeUtf8' $ Array.toStream $ toArray u
$ Stream.toList $ Unicode.decodeUtf8' $ Array.read $ toArray u

View File

@ -20,10 +20,12 @@ moduleName = "Data.Array"
#include "Streamly/Test/Data/Array/Common.hs"
testFromStreamToStream :: Property
testFromStreamToStream = genericTestFromTo (const A.fromStream) A.toStream (==)
testFromStreamToStream =
genericTestFromTo (const A.fromStream) A.read (==)
testFoldUnfold :: Property
testFoldUnfold = genericTestFromTo (const (S.fold A.write)) (S.unfold A.read) (==)
testFoldUnfold =
genericTestFromTo (const (S.fold A.write)) (S.unfold A.reader) (==)
testFromList :: Property
testFromList =
@ -31,7 +33,7 @@ testFromList =
forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
monadicIO $ do
let arr = A.fromList list
xs <- run $ S.fold Fold.toList $ S.unfold A.read arr
xs <- run $ S.fold Fold.toList $ S.unfold A.reader arr
assert (xs == list)
testLengthFromStream :: Property

View File

@ -53,24 +53,25 @@ genericTestFromTo arrFold arrUnfold listEq =
testFoldNUnfold :: Property
testFoldNUnfold =
genericTestFromTo (S.fold . A.writeN) (S.unfold A.read) (==)
genericTestFromTo (S.fold . A.writeN) (S.unfold A.reader) (==)
testFoldNToStream :: Property
testFoldNToStream =
genericTestFromTo (S.fold . A.writeN) A.toStream (==)
genericTestFromTo (S.fold . A.writeN) A.read (==)
testFoldNToStreamRev :: Property
testFoldNToStreamRev =
genericTestFromTo
(S.fold . A.writeN)
A.toStreamRev
A.readRev
(\xs list -> xs == reverse list)
testFromStreamNUnfold :: Property
testFromStreamNUnfold = genericTestFromTo A.fromStreamN (S.unfold A.read) (==)
testFromStreamNUnfold =
genericTestFromTo A.fromStreamN (S.unfold A.reader) (==)
testFromStreamNToStream :: Property
testFromStreamNToStream = genericTestFromTo A.fromStreamN A.toStream (==)
testFromStreamNToStream = genericTestFromTo A.fromStreamN A.read (==)
testFromListN :: Property
testFromListN =
@ -79,7 +80,7 @@ testFromListN =
forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
monadicIO $ do
let arr = A.fromListN n list
xs <- run $ S.fold Fold.toList $ S.unfold A.read arr
xs <- run $ S.fold Fold.toList $ S.unfold A.reader arr
listEquals (==) xs (take n list)
foldManyWith :: (Int -> Fold IO Int (Array Int)) -> Property
@ -89,7 +90,7 @@ foldManyWith f =
monadicIO $ do
xs <- run
$ S.fold Fold.toList
$ S.unfoldMany A.read
$ S.unfoldMany A.reader
$ S.foldMany (f 240)
$ S.fromList list
assert (xs == list)

View File

@ -38,10 +38,11 @@ moduleName = "Data.Array.Unboxed"
#include "Streamly/Test/Data/Array/Common.hs"
testFromStreamToStream :: Property
testFromStreamToStream = genericTestFromTo (const A.fromStream) A.toStream (==)
testFromStreamToStream = genericTestFromTo (const A.fromStream) A.read (==)
testFoldUnfold :: Property
testFoldUnfold = genericTestFromTo (const (S.fold A.write)) (S.unfold A.read) (==)
testFoldUnfold =
genericTestFromTo (const (S.fold A.write)) (S.unfold A.reader) (==)
testFromList :: Property
testFromList =
@ -49,7 +50,7 @@ testFromList =
forAll (vectorOf len (arbitrary :: Gen Int)) $ \list ->
monadicIO $ do
let arr = A.fromList list
xs <- run $ S.fold Fold.toList $ S.unfold A.read arr
xs <- run $ S.fold Fold.toList $ S.unfold A.reader arr
assert (xs == list)
testLengthFromStream :: Property

View File

@ -104,7 +104,7 @@ eventMatches ev (expectedPath, f) =
utf8ToString :: Array Word8 -> String
utf8ToString =
runIdentity . Stream.toList . Unicode.decodeUtf8' . Array.toStream
runIdentity . Stream.toList . Unicode.decodeUtf8' . Array.read
evPath = utf8ToString (Event.getAbsPath ev)

View File

@ -56,7 +56,7 @@ toList = Stream.fold Fold.toList
utf8ToString :: Array.Array Word8 -> String
utf8ToString =
runIdentity . toList . Unicode.decodeUtf8' . Array.toStream
runIdentity . toList . Unicode.decodeUtf8' . Array.read
testData :: String
testData = "This is the test data for FileSystem.Handle ??`!@#$%^&*~~))`]"
@ -89,7 +89,7 @@ readWithBufferFromHandle =
readChunksFromHandle :: IO (Stream IO Char)
readChunksFromHandle =
let f = Unicode.decodeUtf8
. Stream.concatMap Array.toStream
. Stream.concatMap Array.read
. Stream.unfold Handle.chunkReader
in executor f
@ -98,7 +98,7 @@ readChunksWithBuffer =
let f1 = (\h -> (1024, h))
f2 =
Unicode.decodeUtf8
. Stream.concatMap Array.toStream
. Stream.concatMap Array.read
. Stream.unfold Handle.chunkReaderWith
. f1
in executor f2