@@ -136,6 +136,7 @@ module Streamly.Internal.Data.Stream.StreamD.Nesting
136 , wordsBy
136 , wordsBy
137 , splitOnSeq
137 , splitOnSeq
138 , splitOnSuffixSeq
138 , splitOnSuffixSeq
140 -- * Transform (Nested Containers)
141 -- * Transform (Nested Containers)
141 -- | Opposite to compact in ArrayStream
142 -- | Opposite to compact in ArrayStream
@@ -159,14 +160,16 @@ import Fusion.Plugin.Types (Fuse(..))
159import GHC.Types (SPEC(..))
160import GHC.Types (SPEC(..))
161import Streamly.Internal.Data.Array.Foreign.Type (Array(..))
162import Streamly.Internal.Data.Array.Foreign.Type (Array(..))
162−import Streamly.Internal.Data.Refold.Type (Refold(..))
163+import Streamly.Internal.Data.Fold.Step (Step(..))
163import Streamly.Internal.Data.Fold.Type (Fold(..))
164import Streamly.Internal.Data.Fold.Type (Fold(..))
164import Streamly.Internal.Data.Parser (ParseError(..))
165import Streamly.Internal.Data.Parser (ParseError(..))
166+import Streamly.Internal.Data.Refold.Type (Refold(..))
165import Streamly.Internal.Data.SVar.Type (adaptState)
167import Streamly.Internal.Data.SVar.Type (adaptState)
168+import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
166import Streamly.Internal.Data.Unfold.Type (Unfold(..))
169import Streamly.Internal.Data.Unfold.Type (Unfold(..))
168import qualified Streamly.Internal.Data.Array.Foreign.Type as A
171import qualified Streamly.Internal.Data.Array.Foreign.Type as A
169−import qualified Streamly.Internal.Data.Fold.Type as FL
172+import qualified Streamly.Internal.Data.Fold as FL
170import qualified Streamly.Internal.Data.Parser as PR
173import qualified Streamly.Internal.Data.Parser as PR
171import qualified Streamly.Internal.Data.Parser.ParserD as PRD
174import qualified Streamly.Internal.Data.Parser.ParserD as PRD
172import qualified Streamly.Internal.Ring.Foreign as RB
175import qualified Streamly.Internal.Ring.Foreign as RB
@@ -1067,6 +1070,35 @@ refoldIterateM (Refold fstep finject fextract) initial (Stream step state) =
1067 stepOuter _ (CIterYield a next) = return $ Yield a next
1070 stepOuter _ (CIterYield a next) = return $ Yield a next
1068 stepOuter _ CIterStop = return Stop
1071 stepOuter _ CIterStop = return Stop
1073+-- "n" elements at the end are dropped by the fold.
1074+{-# INLINE sliceBy #-}
1075+sliceBy :: Monad m => Fold m a Int -> Int -> Refold m (Int, Int) a (Int, Int)
1076+sliceBy (Fold step1 initial1 extract1) n = Refold step inject extract
1080+ inject (i, len) = do
1083+ Partial s -> Partial $ Tuple' (i + len + n) s
1084+ Done l -> Done (i, l)
1086+ step (Tuple' i s) x = do
1089+ Partial s1 -> Partial $ Tuple' i s1
1090+ Done len -> Done (i, len)
1092+ extract (Tuple' i s) = (i,) <$> extract1 s
1094+{-# INLINE sliceOnSuffix #-}
1095+sliceOnSuffix :: Monad m => (a -> Bool) -> Stream m a -> Stream m (Int, Int)
1096+sliceOnSuffix predicate =
1097+ -- Scan the stream with the given refold
1099+ (sliceBy (FL.takeEndBy_ predicate FL.length) 1)
1070------------------------------------------------------------------------------
1102------------------------------------------------------------------------------
1071-- Parsing
1103-- Parsing
1072------------------------------------------------------------------------------
1104------------------------------------------------------------------------------