@@ -27,6 +27,7 @@ module Streamly.Internal.Data.Array.Generic.Mut.Type
28 , writeWith
28 , writeWith
31 -- , writeRevN
32 -- , writeRevN
32 -- , writeRev
33 -- , writeRev
@@ -91,7 +92,7 @@ module Streamly.Internal.Data.Array.Generic.Mut.Type
91 , producer -- experimental
92 , producer -- experimental
93 -- ** To containers
94 -- ** To containers
94− , toStreamD
95+ , read
96 , toStreamK
97 , toStreamK
97 -- , toStreamKRev
98 -- , toStreamKRev
@@ -179,6 +180,7 @@ import GHC.Int (Int(..))
179import Streamly.Internal.Data.Fold.Type (Fold(..))
180import Streamly.Internal.Data.Fold.Type (Fold(..))
180import Streamly.Internal.Data.Producer.Type (Producer (..))
181import Streamly.Internal.Data.Producer.Type (Producer (..))
181import Streamly.Internal.Data.Unfold.Type (Unfold(..))
182import Streamly.Internal.Data.Unfold.Type (Unfold(..))
183+import Streamly.Internal.Data.Stream.StreamD.Type (Stream)
183import qualified Streamly.Internal.Data.Fold.Type as FL
185import qualified Streamly.Internal.Data.Fold.Type as FL
184import qualified Streamly.Internal.Data.Producer as Producer
186import qualified Streamly.Internal.Data.Producer as Producer
@@ -527,14 +529,13 @@ getSlice index len arr@MutArray{..} =
527toList :: MonadIO m => MutArray a -> m [a]
529toList :: MonadIO m => MutArray a -> m [a]
528toList arr@MutArray{..} = mapM (`getIndexUnsafe` arr) [0 .. (arrLen - 1)]
530toList arr@MutArray{..} = mapM (`getIndexUnsafe` arr) [0 .. (arrLen - 1)]
530−-- | Use the 'read' unfold instead.
532+-- | Generates a stream from the elements of @MutArray@
532−-- @toStreamD = D.unfold read@
534+-- @read = D.unfold reader@
533−--
535+-- /Pre-release/
534−-- We can try this if the unfold has any performance issues.
535−{-# INLINE_NORMAL toStreamD #-}
536+{-# INLINE_NORMAL read #-}
536−toStreamD :: MonadIO m => MutArray a -> D.Stream m a
537+read :: MonadIO m => MutArray a -> D.Stream m a
537−toStreamD arr@MutArray{..} =
538+read arr@MutArray{..} =
538 D.mapM (`getIndexUnsafe` arr) $ D.enumerateFromToIntegral 0 (arrLen - 1)
539 D.mapM (`getIndexUnsafe` arr) $ D.enumerateFromToIntegral 0 (arrLen - 1)
540-- Check equivalence with StreamK.fromStream . toStreamD and remove
541-- Check equivalence with StreamK.fromStream . toStreamD and remove
@@ -794,3 +795,11 @@ strip p arr = liftIO $ do
795 then getIndexL (idx + 1)
796 then getIndexL (idx + 1)
796 else return idx
797 else return idx
799+-- | Create a 'MutArray' from the first @n@ elements of a stream. The
800+-- array is allocated to size @n@, if the stream terminates before @n@
801+-- elements then the array may hold less than @n@ elements.
803+{-# INLINE fromStreamN #-}
804+fromStreamN :: MonadIO m => Int -> Stream m a -> m (MutArray a)
805+fromStreamN n = D.fold (writeN n)