@@ -69,9 +69,9 @@ import Streamly.Data.Stream (Stream)
69import Streamly.Internal.Data.Array.Unboxed.Type (Array(..))
69import Streamly.Internal.Data.Array.Unboxed.Type (Array(..))
70import Streamly.Internal.Data.Array.Stream.Fold.Foreign (ArrayFold(..))
70import Streamly.Internal.Data.Array.Stream.Fold.Foreign (ArrayFold(..))
71import Streamly.Internal.Data.Parser (ParseError(..))
71import Streamly.Internal.Data.Parser (ParseError(..))
72−import Streamly.Internal.Data.Stream.IsStream.Type
72+import Streamly.Internal.Data.Stream
73− (IsStream, fromStreamD, toStreamD)
73+ (fromStreamD, fromStreamK, toStreamD, toStreamK)
74−import Streamly.Internal.Data.SVar (adaptState, defState)
74+import Streamly.Internal.Data.SVar.Type (adaptState, defState)
75import Streamly.Internal.Data.Array.Unboxed.Mut.Type
75import Streamly.Internal.Data.Array.Unboxed.Mut.Type
76 (allocBytesToElemCount)
76 (allocBytesToElemCount)
77import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
77import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
@@ -79,27 +79,22 @@ import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
79import qualified Streamly.Data.Fold as FL
79import qualified Streamly.Data.Fold as FL
80import qualified Streamly.Internal.Data.Array.Unboxed as A
80import qualified Streamly.Internal.Data.Array.Unboxed as A
81import qualified Streamly.Internal.Data.Array.Unboxed as Array
81import qualified Streamly.Internal.Data.Array.Unboxed as Array
82−-- import qualified Streamly.Internal.Data.Array.Stream.Fold.Foreign
84import qualified Streamly.Internal.Data.Array.Unboxed.Type as A
82import qualified Streamly.Internal.Data.Array.Unboxed.Type as A
85import qualified Streamly.Internal.Data.Array.Unboxed.Mut.Type as MA
83import qualified Streamly.Internal.Data.Array.Unboxed.Mut.Type as MA
86import qualified Streamly.Internal.Data.Array.Stream.Mut.Foreign as AS
84import qualified Streamly.Internal.Data.Array.Stream.Mut.Foreign as AS
87−import qualified Streamly.Internal.Data.Fold.Type as FL
85+import qualified Streamly.Internal.Data.Fold.Type as FL (Fold(..), Step(..))
89import qualified Streamly.Internal.Data.Parser as PR
86import qualified Streamly.Internal.Data.Parser as PR
90import qualified Streamly.Internal.Data.Parser.ParserD as PRD
87import qualified Streamly.Internal.Data.Parser.ParserD as PRD
91 (Parser(..), Initial(..), fromParserK)
88 (Parser(..), Initial(..), fromParserK)
92−import qualified Streamly.Internal.Data.Stream.IsStream as S
89+import qualified Streamly.Internal.Data.Stream as S
93import qualified Streamly.Internal.Data.Stream.StreamD as D
90import qualified Streamly.Internal.Data.Stream.StreamD as D
94 ( fromList, nil, cons, map
91 ( fromList, nil, cons, map
95− , unfoldMany, append, splitInnerBy, splitInnerBySuffix
92+ , unfoldMany, append, splitInnerBy, splitInnerBySuffix, foldlM'
97import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
94import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
98 (Step(Yield, Stop, Skip), Stream(Stream))
95 (Step(Yield, Stop, Skip), Stream(Stream))
99import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
96import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
100 (Stream, cons, nil, fromPure, foldStream)
97 (Stream, cons, nil, fromPure, foldStream)
101−import qualified Streamly.Internal.Data.Stream as Stream
102− (fromStreamK, toStreamK)
104-- XXX Since these are immutable arrays MonadIO constraint can be removed from
99-- XXX Since these are immutable arrays MonadIO constraint can be removed from
105-- most places.
100-- most places.
@@ -115,8 +110,8 @@ import qualified Streamly.Internal.Data.Stream as Stream
116-- /Pre-release/
111-- /Pre-release/
117{-# INLINE arraysOf #-}
112{-# INLINE arraysOf #-}
118−arraysOf :: (IsStream t, MonadIO m, Unboxed a)
113+arraysOf :: (MonadIO m, Unboxed a)
119− => Int -> t m a -> t m (Array a)
114+ => Int -> Stream m a -> Stream m (Array a)
120arraysOf n str = fromStreamD $ A.arraysOf n (toStreamD str)
115arraysOf n str = fromStreamD $ A.arraysOf n (toStreamD str)
122-------------------------------------------------------------------------------
117-------------------------------------------------------------------------------
@@ -135,7 +130,7 @@ arraysOf n str = fromStreamD $ A.arraysOf n (toStreamD str)
136-- @since 0.7.0
131-- @since 0.7.0
137{-# INLINE concat #-}
132{-# INLINE concat #-}
138−concat :: (IsStream t, Monad m, Unboxed a) => t m (Array a) -> t m a
133+concat :: (Monad m, Unboxed a) => Stream m (Array a) -> Stream m a
139-- concat m = fromStreamD $ A.flattenArrays (toStreamD m)
134-- concat m = fromStreamD $ A.flattenArrays (toStreamD m)
140-- concat m = fromStreamD $ D.concatMap A.toStreamD (toStreamD m)
135-- concat m = fromStreamD $ D.concatMap A.toStreamD (toStreamD m)
141concat m = fromStreamD $ D.unfoldMany A.read (toStreamD m)
136concat m = fromStreamD $ D.unfoldMany A.read (toStreamD m)
@@ -147,7 +142,7 @@ concat m = fromStreamD $ D.unfoldMany A.read (toStreamD m)
148-- @since 0.7.0
143-- @since 0.7.0
149{-# INLINE concatRev #-}
144{-# INLINE concatRev #-}
150−concatRev :: (IsStream t, Monad m, Unboxed a) => t m (Array a) -> t m a
145+concatRev :: (Monad m, Unboxed a) => Stream m (Array a) -> Stream m a
151-- concatRev m = fromStreamD $ A.flattenArraysRev (toStreamD m)
146-- concatRev m = fromStreamD $ A.flattenArraysRev (toStreamD m)
152concatRev m = fromStreamD $ D.unfoldMany A.readRev (toStreamD m)
147concatRev m = fromStreamD $ D.unfoldMany A.readRev (toStreamD m)
@@ -160,12 +155,12 @@ concatRev m = fromStreamD $ D.unfoldMany A.readRev (toStreamD m)
161-- /Pre-release/
156-- /Pre-release/
162{-# INLINE interpose #-}
157{-# INLINE interpose #-}
163−interpose :: (Monad m, IsStream t, Unboxed a) => a -> t m (Array a) -> t m a
158+interpose :: (Monad m, Unboxed a) => a -> Stream m (Array a) -> Stream m a
164interpose x = S.interpose x A.read
159interpose x = S.interpose x A.read
166{-# INLINE intercalateSuffix #-}
161{-# INLINE intercalateSuffix #-}
167−intercalateSuffix :: (Monad m, IsStream t, Unboxed a)
162+intercalateSuffix :: (Monad m, Unboxed a)
168− => Array a -> t m (Array a) -> t m a
163+ => Array a -> Stream m (Array a) -> Stream m a
169intercalateSuffix = S.intercalateSuffix A.read
164intercalateSuffix = S.intercalateSuffix A.read
171-- | Flatten a stream of arrays appending the given element after each
166-- | Flatten a stream of arrays appending the given element after each
@@ -173,8 +168,8 @@ intercalateSuffix = S.intercalateSuffix A.read
174-- @since 0.7.0
169-- @since 0.7.0
175{-# INLINE interposeSuffix #-}
170{-# INLINE interposeSuffix #-}
176−interposeSuffix :: (Monad m, IsStream t, Unboxed a)
171+interposeSuffix :: (Monad m, Unboxed a)
177− => a -> t m (Array a) -> t m a
172+ => a -> Stream m (Array a) -> Stream m a
178-- interposeSuffix x = fromStreamD . A.unlines x . toStreamD
173-- interposeSuffix x = fromStreamD . A.unlines x . toStreamD
179interposeSuffix x = S.interposeSuffix x A.read
174interposeSuffix x = S.interposeSuffix x A.read
@@ -302,19 +297,19 @@ _splitOn byte (D.Stream step state) = D.Stream step' (Initial state)
302-- @since 0.7.0
297-- @since 0.7.0
303{-# INLINE splitOn #-}
298{-# INLINE splitOn #-}
305− :: (IsStream t, MonadIO m)
300+ :: (MonadIO m)
307− -> t m (Array Word8)
302+ -> Stream m (Array Word8)
308− -> t m (Array Word8)
303+ -> Stream m (Array Word8)
309splitOn byte s =
304splitOn byte s =
310 fromStreamD $ D.splitInnerBy (A.breakOn byte) A.splice $ toStreamD s
305 fromStreamD $ D.splitInnerBy (A.breakOn byte) A.splice $ toStreamD s
312{-# INLINE splitOnSuffix #-}
307{-# INLINE splitOnSuffix #-}
313splitOnSuffix
308splitOnSuffix
314− :: (IsStream t, MonadIO m)
309+ :: (MonadIO m)
316− -> t m (Array Word8)
311+ -> Stream m (Array Word8)
317− -> t m (Array Word8)
312+ -> Stream m (Array Word8)
318-- splitOn byte s = fromStreamD $ A.splitOn byte $ toStreamD s
313-- splitOn byte s = fromStreamD $ A.splitOn byte $ toStreamD s
319splitOnSuffix byte s =
314splitOnSuffix byte s =
320 fromStreamD $ D.splitInnerBySuffix (A.breakOn byte) A.splice $ toStreamD s
315 fromStreamD $ D.splitInnerBySuffix (A.breakOn byte) A.splice $ toStreamD s
@@ -410,9 +405,9 @@ foldBreak ::
410 -> m (b, Stream m (A.Array a))
405 -> m (b, Stream m (A.Array a))
411-- foldBreak f s = fmap fromStreamD <$> foldBreakD f (toStreamD s)
406-- foldBreak f s = fmap fromStreamD <$> foldBreakD f (toStreamD s)
412foldBreak f =
407foldBreak f =
413− fmap (fmap Stream.fromStreamK)
408+ fmap (fmap fromStreamK)
414 . foldBreakK f
409 . foldBreakK f
415− . Stream.toStreamK
410+ . toStreamK
416-- If foldBreak performs better than runArrayFoldBreak we can use a rewrite
411-- If foldBreak performs better than runArrayFoldBreak we can use a rewrite
417-- rule to rewrite runArrayFoldBreak to fold.
412-- rule to rewrite runArrayFoldBreak to fold.
418-- foldBreak f = runArrayFoldBreak (ArrayFold.fromFold f)
413-- foldBreak f = runArrayFoldBreak (ArrayFold.fromFold f)
@@ -468,6 +463,10 @@ splitAtArrayListRev n ls
468-- Fold to a single Array
463-- Fold to a single Array
469-------------------------------------------------------------------------------
464-------------------------------------------------------------------------------
466+{-# INLINE foldlM' #-}
467+foldlM' :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> m b
468+foldlM' step begin = D.foldlM' step begin . S.toStreamD
471-- XXX Both of these implementations of splicing seem to perform equally well.
470-- XXX Both of these implementations of splicing seem to perform equally well.
472-- We need to perform benchmarks over a range of sizes though.
471-- We need to perform benchmarks over a range of sizes though.
@@ -478,16 +477,16 @@ spliceArraysLenUnsafe :: (MonadIO m, Unboxed a)
478 => Int -> Stream m (MA.Array a) -> m (MA.Array a)
477 => Int -> Stream m (MA.Array a) -> m (MA.Array a)
479spliceArraysLenUnsafe len buffered = do
478spliceArraysLenUnsafe len buffered = do
480 arr <- liftIO $ MA.newArray len
479 arr <- liftIO $ MA.newArray len
481− S.foldlM' MA.spliceUnsafe (return arr) buffered
480+ foldlM' MA.spliceUnsafe (return arr) buffered
483{-# INLINE _spliceArrays #-}
482{-# INLINE _spliceArrays #-}
484_spliceArrays :: (MonadIO m, Unboxed a)
483_spliceArrays :: (MonadIO m, Unboxed a)
485 => Stream m (Array a) -> m (Array a)
484 => Stream m (Array a) -> m (Array a)
486_spliceArrays s = do
485_spliceArrays s = do
487 buffered <- S.foldr S.cons S.nil s
486 buffered <- S.foldr S.cons S.nil s
488− len <- S.sum (S.map Array.length buffered)
487+ len <- S.fold FL.sum (fmap Array.length buffered)
489 arr <- liftIO $ MA.newArray len
488 arr <- liftIO $ MA.newArray len
490− final <- S.foldlM' writeArr (return arr) s
489+ final <- foldlM' writeArr (return arr) s
491 return $ A.unsafeFreeze final
490 return $ A.unsafeFreeze final
@@ -499,8 +498,8 @@ _spliceArraysBuffered :: (MonadIO m, Unboxed a)
499 => Stream m (Array a) -> m (Array a)
498 => Stream m (Array a) -> m (Array a)
500_spliceArraysBuffered s = do
499_spliceArraysBuffered s = do
501 buffered <- S.foldr S.cons S.nil s
500 buffered <- S.foldr S.cons S.nil s
502− len <- S.sum (S.map Array.length buffered)
501+ len <- S.fold FL.sum (fmap Array.length buffered)
503− A.unsafeFreeze <$> spliceArraysLenUnsafe len (S.map A.unsafeThaw s)
502+ A.unsafeFreeze <$> spliceArraysLenUnsafe len (fmap A.unsafeThaw s)
505{-# INLINE spliceArraysRealloced #-}
504{-# INLINE spliceArraysRealloced #-}
506spliceArraysRealloced :: forall m a. (MonadIO m, Unboxed a)
505spliceArraysRealloced :: forall m a. (MonadIO m, Unboxed a)
@@ -509,7 +508,7 @@ spliceArraysRealloced s = do
509 let n = allocBytesToElemCount (undefined :: a) (4 * 1024)
508 let n = allocBytesToElemCount (undefined :: a) (4 * 1024)
510 idst = liftIO $ MA.newArray n
509 idst = liftIO $ MA.newArray n
512− arr <- S.foldlM' MA.spliceExp idst (S.map A.unsafeThaw s)
511+ arr <- foldlM' MA.spliceExp idst (fmap A.unsafeThaw s)
513 liftIO $ A.unsafeFreeze <$> MA.rightSize arr
512 liftIO $ A.unsafeFreeze <$> MA.rightSize arr
515-- XXX This should just be "fold A.write"
514-- XXX This should just be "fold A.write"
@@ -530,7 +529,7 @@ toArray = spliceArraysRealloced
532{-# INLINE toArraysInRange #-}
531{-# INLINE toArraysInRange #-}
533−toArraysInRange :: (IsStream t, MonadIO m, Unboxed a)
532+toArraysInRange :: (MonadIO m, Unboxed a)
534 => Int -> Int -> Fold m (Array a) b -> Fold m a b
533 => Int -> Int -> Fold m (Array a) b -> Fold m a b
535toArraysInRange low high (Fold step initial extract) =
534toArraysInRange low high (Fold step initial extract) =
@@ -785,9 +784,9 @@ parseBreak p s =
785 fmap fromStreamD <$> parseBreakD (PRD.fromParserK p) (toStreamD s)
784 fmap fromStreamD <$> parseBreakD (PRD.fromParserK p) (toStreamD s)
787parseBreak p =
786parseBreak p =
788− fmap (fmap Stream.fromStreamK)
787+ fmap (fmap fromStreamK)
789 . parseBreakK (PRD.fromParserK p)
788 . parseBreakK (PRD.fromParserK p)
790− . Stream.toStreamK
789+ . toStreamK
792-------------------------------------------------------------------------------
791-------------------------------------------------------------------------------
793-- Elimination - Running Array Folds and parsers
792-- Elimination - Running Array Folds and parsers
@@ -1155,8 +1154,8 @@ runArrayFoldManyD
1155-- /Pre-release/
1154-- /Pre-release/
1156{-# INLINE runArrayFoldMany #-}
1155{-# INLINE runArrayFoldMany #-}
1157runArrayFoldMany
1156runArrayFoldMany
1158− :: (IsStream t, MonadThrow m, Unboxed a)
1157+ :: (MonadThrow m, Unboxed a)
1159 => ArrayFold m a b
1158 => ArrayFold m a b
1160− -> t m (Array a)
1159+ -> Stream m (Array a)
1161− -> t m b
1160+ -> Stream m b
1162runArrayFoldMany p m = fromStreamD $ runArrayFoldManyD p (toStreamD m)
1161runArrayFoldMany p m = fromStreamD $ runArrayFoldManyD p (toStreamD m)