on master help sign in
Add toArrays fold
a2fd0dcHarendra Kumar
src/Streamly/Fold.hs+2−2
@@ -375,7 +375,7 @@ import qualified Data.Map.Strict as Map
375
375
376import Streamly (MonadAsync, parallel)
376import Streamly (MonadAsync, parallel)
377import Streamly.Fold.Types (Fold(..))
377import Streamly.Fold.Types (Fold(..))
378−import Streamly.Mem.Array (Array)
378+import Streamly.Mem.Array.Types (Array)
379-- import Streamly.Mem.Ring (Ring)
379-- import Streamly.Mem.Ring (Ring)
380import Streamly.Streams.Serial (SerialT)
380import Streamly.Streams.Serial (SerialT)
381import Streamly.Streams.StreamK (IsStream())
381import Streamly.Streams.StreamK (IsStream())
@@ -385,7 +385,7 @@ import Streamly.Time.Units
385
385
386import Streamly.Strict
386import Streamly.Strict
387
387
388−import qualified Streamly.Mem.Array as A
388+import qualified Streamly.Mem.Array.Types as A
389import qualified Streamly.Prelude as S
389import qualified Streamly.Prelude as S
390import qualified Streamly.Streams.StreamD as D
390import qualified Streamly.Streams.StreamD as D
391import qualified Streamly.Streams.StreamK as K
391import qualified Streamly.Streams.StreamK as K
src/Streamly/Mem/Array.hs+8−10
@@ -132,6 +132,7 @@ module Streamly.Mem.Array
132where
132where
133
133
134import Control.Monad.IO.Class (MonadIO(..))
134import Control.Monad.IO.Class (MonadIO(..))
135+import Data.Functor.Identity
135import Foreign.ForeignPtr (withForeignPtr)
136import Foreign.ForeignPtr (withForeignPtr)
136import Foreign.Ptr (minusPtr, plusPtr, castPtr)
137import Foreign.Ptr (minusPtr, plusPtr, castPtr)
137import Foreign.Storable (Storable(..))
138import Foreign.Storable (Storable(..))
@@ -140,6 +141,7 @@ import Prelude hiding (length, null, last, map, (!!), read)
140import Streamly.Mem.Array.Types hiding (flattenArrays, newArray)
141import Streamly.Mem.Array.Types hiding (flattenArrays, newArray)
141import Streamly.Streams.Serial (SerialT)
142import Streamly.Streams.Serial (SerialT)
142import Streamly.Streams.StreamK.Type (IsStream)
143import Streamly.Streams.StreamK.Type (IsStream)
144+import Streamly.Fold (Fold, lchunksOf, toStream)
143
145
144import qualified Streamly.Mem.Array.Types as A
146import qualified Streamly.Mem.Array.Types as A
145import qualified Streamly.Prelude as S
147import qualified Streamly.Prelude as S
@@ -170,16 +172,6 @@ writeN n m = fromStreamDN n $ D.toStreamD m
170-- Elimination
172-- Elimination
171-------------------------------------------------------------------------------
173-------------------------------------------------------------------------------
172
174
173−-- | Convert an 'Array' into a stream.
174−--
175−-- @since 0.7.0
176−{-# INLINE_EARLY read #-}
177−read :: (Monad m, IsStream t, Storable a) => Array a -> t m a
178−read = D.fromStreamD . toStreamD
179−-- XXX add fallback to StreamK rule
180−-- {-# RULES "Streamly.Array.read fallback to StreamK" [1]
181−-- forall a. S.readK (read a) = K.fromArray a #-}
182−
183-- | Convert an 'Array' into a stream in reverse order.
175-- | Convert an 'Array' into a stream in reverse order.
184--
176--
185-- @since 0.7.0
177-- @since 0.7.0
@@ -361,6 +353,12 @@ writeSliceRev arr i len s = undefined
361-- Streams of Arrays
353-- Streams of Arrays
362-------------------------------------------------------------------------------
354-------------------------------------------------------------------------------
363
355
356+-- | Fold the input to a pure buffered stream (List) of arrays.
357+{-# INLINE _toArrays #-}
358+_toArrays :: (MonadIO m, Storable a)
359+ => Int -> Fold m a (SerialT Identity (Array a))
360+_toArrays n = lchunksOf n (toArrayN n) toStream
361+
364-- | Convert a stream of arrays into a stream of their elements.
362-- | Convert a stream of arrays into a stream of their elements.
365--
363--
366-- @since 0.7.0
364-- @since 0.7.0
src/Streamly/Mem/Array/Types.hs+12−6
@@ -50,6 +50,7 @@ module Streamly.Mem.Array.Types
50 , toStreamDRev
50 , toStreamDRev
51 , toList
51 , toList
52 , toArrayN
52 , toArrayN
53+ , read
53
54
54 -- * Utilities
55 -- * Utilities
55 , defaultChunkSize
56 , defaultChunkSize
@@ -70,7 +71,7 @@ import Foreign.ForeignPtr
70import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
71import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
71import Foreign.Ptr (plusPtr, minusPtr, castPtr)
72import Foreign.Ptr (plusPtr, minusPtr, castPtr)
72import Foreign.Storable (Storable(..))
73import Foreign.Storable (Storable(..))
73−import Prelude hiding (length, foldr)
74+import Prelude hiding (length, foldr, read)
74import Text.Read (readPrec, readListPrec, readListPrecDefault)
75import Text.Read (readPrec, readListPrec, readListPrecDefault)
75
76
76import GHC.Base (Addr#, realWorld#)
77import GHC.Base (Addr#, realWorld#)
@@ -369,11 +370,6 @@ toArrayN n = Fold step initial extract
369 extract = return
370 extract = return
370
371
371{-
372{-
372−-- | Fold the input to a pure buffered stream (List) of arrays.
373−{-# INLINE toArrays #-}
374−toArrays :: (Monad m, Storable a) => Int -> Fold m a (List (Array a))
375−toArrays n = Fold step initial extract
376−
377-- This can be implemented by combining the List of arrays from toArrays into a
373-- This can be implemented by combining the List of arrays from toArrays into a
378-- single array.
374-- single array.
379-- | Fold the whole input to a single array.
375-- | Fold the whole input to a single array.
@@ -620,6 +616,16 @@ instance Foldable Array where
620 foldr = _foldr
616 foldr = _foldr
621#endif
617#endif
622
618
619+-- | Convert an 'Array' into a stream.
620+--
621+-- @since 0.7.0
622+{-# INLINE_EARLY read #-}
623+read :: (Monad m, K.IsStream t, Storable a) => Array a -> t m a
624+read = D.fromStreamD . toStreamD
625+-- XXX add fallback to StreamK rule
626+-- {-# RULES "Streamly.Array.read fallback to StreamK" [1]
627+-- forall a. S.readK (read a) = K.fromArray a #-}
628+
623-------------------------------------------------------------------------------
629-------------------------------------------------------------------------------
624-- Semigroup and Monoid
630-- Semigroup and Monoid
625-------------------------------------------------------------------------------
631-------------------------------------------------------------------------------
src/Streamly/Streams/StreamD.hs−8
@@ -1667,14 +1667,6 @@ stripPrefix (Stream stepa ta) (Stream stepb tb) = go (ta, tb, Nothing)
1667mapM_ :: Monad m => (a -> m b) -> Stream m a -> m ()
1667mapM_ :: Monad m => (a -> m b) -> Stream m a -> m ()
1668mapM_ m = drain . mapM m
1668mapM_ m = drain . mapM m
1669
1669
1670−------------------------------------------------------------------------------
1671−-- Converting folds
1672−------------------------------------------------------------------------------
1673−
1674−{-# INLINE fromStreamD #-}
1675−fromStreamD :: (K.IsStream t, Monad m) => Stream m a -> t m a
1676−fromStreamD = K.fromStream . toStreamK
1677−
1678------------------------------------------------------------------------------
1670------------------------------------------------------------------------------
1679-- Exceptions
1671-- Exceptions
1680------------------------------------------------------------------------------
1672------------------------------------------------------------------------------
src/Streamly/Streams/StreamD/Type.hs+9
@@ -35,6 +35,7 @@ module Streamly.Streams.StreamD.Type
35#endif
35#endif
36 , fromStreamK
36 , fromStreamK
37 , toStreamK
37 , toStreamK
38+ , fromStreamD
38 , map
39 , map
39 , mapM
40 , mapM
40 , yield
41 , yield
@@ -136,6 +137,14 @@ toStreamK (Stream step state) = go state
136 forall s. fromStreamK (toStreamK s) = s #-}
137 forall s. fromStreamK (toStreamK s) = s #-}
137#endif
138#endif
138
139
140+------------------------------------------------------------------------------
141+-- Converting folds
142+------------------------------------------------------------------------------
143+
144+{-# INLINE fromStreamD #-}
145+fromStreamD :: (K.IsStream t, Monad m) => Stream m a -> t m a
146+fromStreamD = K.fromStream . toStreamK
147+
139------------------------------------------------------------------------------
148------------------------------------------------------------------------------
140-- Instances
149-- Instances
141------------------------------------------------------------------------------
150------------------------------------------------------------------------------