on master help sign in
Deprecate fromStreamD and fromStreamDN in Array
commit6a5674002fe9e0e1bb1c60603ebf013b7daf7768 authorHarendra Kumar <harendra@composewell.com> dateSat 30 Dec 2023, 09:36 (3 years ago) parent023fe25
2 files changed+38−40
core/src/Streamly/Internal/Data/Array.hs−34 core/src/Streamly/Internal/Data/Array/Type.hs+38−6
core/src/Streamly/Internal/Data/Array.hs−34
@@ -20,10 +20,6 @@ module Streamly.Internal.Data.Array
2020 module Streamly.Internal.Data.Array.Type
2121
2222 -- * Construction
23− -- Stream Folds
24− , fromStreamN
25− , fromStream
26−
2723 -- Monadic Folds
2824 , writeLastN
2925
@@ -86,7 +82,6 @@ where
8682#include "inline.hs"
8783#include "ArrayMacros.h"
8884
89−import Control.Monad (when)
9085import Control.Monad.IO.Class (MonadIO(..))
9186import Data.Functor.Identity (Identity)
9287import Data.Proxy (Proxy(..))
@@ -145,35 +140,6 @@ import Streamly.Internal.Data.Array.Type
145140-- if we use one array for each chunk. This is still significant to add
146141-- pressure to GC.
147142
148−-------------------------------------------------------------------------------
149−-- Construction
150−-------------------------------------------------------------------------------
151−
152−-- | Create an 'Array' from the first N elements of a stream. The array is
153−-- allocated to size N, if the stream terminates before N elements then the
154−-- array may hold less than N elements.
155−--
156−-- /Pre-release/
157−{-# INLINE fromStreamN #-}
158−fromStreamN :: (MonadIO m, Unbox a) => Int -> Stream m a -> m (Array a)
159−fromStreamN n m = do
160− when (n < 0) $ error "writeN: negative write count specified"
161− A.fromStreamDN n m
162−
163−-- | Create an 'Array' from a stream. This is useful when we want to create a
164−-- single array from a stream of unknown size. 'writeN' is at least twice
165−-- as efficient when the size is already known.
166−--
167−-- Note that if the input stream is too large memory allocation for the array
168−-- may fail. When the stream size is not known, `chunksOf` followed by
169−-- processing of indvidual arrays in the resulting stream should be preferred.
170−--
171−-- /Pre-release/
172−{-# INLINE fromStream #-}
173−fromStream :: (MonadIO m, Unbox a) => Stream m a -> m (Array a)
174−fromStream = Stream.fold A.write
175−-- write m = A.fromStreamD $ D.fromStreamK m
176−
177143-------------------------------------------------------------------------------
178144-- Elimination
179145-------------------------------------------------------------------------------
core/src/Streamly/Internal/Data/Array/Type.hs+38−6
@@ -37,8 +37,8 @@ module Streamly.Internal.Data.Array.Type
3737 , pinnedFromListN
3838 , fromListRev
3939 , fromListRevN
40− , fromStreamDN
41− , fromStreamD
40+ , fromStreamN
41+ , fromStream
4242 , fromPureStreamN
4343 , fromPureStream
4444 , fromByteStr#
@@ -101,6 +101,8 @@ module Streamly.Internal.Data.Array.Type
101101 , flattenArrays
102102 , flattenArraysRev
103103 , fromArrayStreamK
104+ , fromStreamDN
105+ , fromStreamD
104106 )
105107where
106108
@@ -108,7 +110,7 @@ where
108110#include "inline.hs"
109111
110112import Control.Exception (assert)
111−import Control.Monad (replicateM)
113+import Control.Monad (replicateM, when)
112114import Control.Monad.IO.Class (MonadIO(..))
113115import Data.Functor.Identity (Identity(..))
114116import Data.Int (Int8, Int16, Int32, Int64)
@@ -314,15 +316,45 @@ pinnedFromList xs = unsafePerformIO $ unsafeFreeze <$> MA.pinnedFromList xs
314316fromListRev :: Unbox a => [a] -> Array a
315317fromListRev xs = unsafePerformIO $ unsafeFreeze <$> MA.fromListRev xs
316318
317−{-# INLINE_NORMAL fromStreamDN #-}
319+-- | Create an 'Array' from the first N elements of a stream. The array is
320+-- allocated to size N, if the stream terminates before N elements then the
321+-- array may hold less than N elements.
322+--
323+-- >>> fromStreamN n = Stream.fold (Array.writeN n)
324+--
325+-- /Pre-release/
326+{-# INLINE_NORMAL fromStreamN #-}
327+fromStreamN :: (MonadIO m, Unbox a) => Int -> Stream m a -> m (Array a)
328+fromStreamN n m = do
329+ when (n < 0) $ error "writeN: negative write count specified"
330+ unsafeFreeze <$> MA.fromStreamN n m
331+-- fromStreamN n = D.fold (writeN n)
332+
333+{-# DEPRECATED fromStreamDN "Please use fromStreamN instead." #-}
318334fromStreamDN :: forall m a. (MonadIO m, Unbox a)
319335 => Int -> D.Stream m a -> m (Array a)
320−fromStreamDN limit str = unsafeFreeze <$> MA.fromStreamDN limit str
336+fromStreamDN = fromStreamN
321337
338+-- | Create an 'Array' from a stream. This is useful when we want to create a
339+-- single array from a stream of unknown size. 'writeN' is at least twice
340+-- as efficient when the size is already known.
341+--
342+-- >>> fromStream = Stream.fold Array.write
343+--
344+-- Note that if the input stream is too large memory allocation for the array
345+-- may fail. When the stream size is not known, `chunksOf` followed by
346+-- processing of indvidual arrays in the resulting stream should be preferred.
347+--
348+-- /Pre-release/
322349{-# INLINE_NORMAL fromStreamD #-}
350+fromStream :: (MonadIO m, Unbox a) => Stream m a -> m (Array a)
351+fromStream = D.fold write
352+-- fromStreamD str = unsafeFreeze <$> MA.fromStream str
353+
354+{-# DEPRECATED fromStreamD "Please use fromStream instead." #-}
323355fromStreamD :: forall m a. (MonadIO m, Unbox a)
324356 => D.Stream m a -> m (Array a)
325−fromStreamD str = unsafeFreeze <$> MA.fromStreamD str
357+fromStreamD = fromStream
326358
327359-------------------------------------------------------------------------------
328360-- Cloning