@@ -174,6 +174,13 @@ module Streamly.Internal.Data.Parser.ParserD
174 -- , retryMax -- try N times
174 -- , retryMax -- try N times
175 -- , retryUntil -- try until successful
175 -- , retryUntil -- try until successful
176 -- , retryUntilN -- try until successful n times
176 -- , retryUntilN -- try until successful n times
@@ -187,9 +194,11 @@ import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
188import qualified Streamly.Internal.Data.Fold.Type as FL
195import qualified Streamly.Internal.Data.Fold.Type as FL
189import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
196import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
197+import qualified Streamly.Internal.Data.Stream.StreamD.Generate as D
191import Prelude hiding
199import Prelude hiding
192− (any, all, take, takeWhile, sequence, concatMap, maybe, either, span)
200+ (any, all, take, takeWhile, sequence, concatMap, maybe, either, span
193import Streamly.Internal.Data.Parser.ParserD.Tee
202import Streamly.Internal.Data.Parser.ParserD.Tee
194import Streamly.Internal.Data.Parser.ParserD.Type
203import Streamly.Internal.Data.Parser.ParserD.Type
@@ -975,6 +984,102 @@ matchBy cmp (D.Stream sstep state) = Parser step initial extract
976 extract _ = throwM $ ParseError "match: end of input"
985 extract _ = throwM $ ParseError "match: end of input"
987+{-# INLINE zipWithM #-}
988+zipWithM :: MonadThrow m =>
989+ (a -> b -> m c) -> D.Stream m a -> Fold m c x -> Parser m b x
990+zipWithM zf (D.Stream sstep state) (Fold fstep finitial fextract) =
991+ Parser step initial extract
999+ r <- sstep defState state
1001+ D.Yield x s -> return $ IPartial (Just x, s, fs)
1005+ -- Need Skip/Continue in initial to loop right here
1006+ D.Skip s -> return $ IPartial (Nothing, s, fs)
1007+ FL.Done x -> return $ IDone x
1009+ step (Just a, st, fs) b = do
1013+ FL.Partial fs1 -> do
1014+ r <- sstep defState st
1016+ D.Yield x1 s -> return $ Continue 0 (Just x1, s, fs1)
1020+ D.Skip s -> return $ Continue 1 (Nothing, s, fs1)
1021+ FL.Done x -> return $ Done 0 x
1022+ step (Nothing, st, fs) b = do
1023+ r <- sstep defState st
1029+ FL.Partial fs1 -> return $ Continue 0 (Nothing, s, fs1)
1030+ FL.Done x -> return $ Done 0 x
1034+ D.Skip s -> return $ Continue 1 (Nothing, s, fs)
1036+ extract _ = throwM $ ParseError "zipWithM: end of input"
1038+-- | Zip the input of a fold with a stream.
1043+zip :: MonadThrow m => D.Stream m a -> Fold m (a, b) x -> Parser m b x
1044+zip = zipWithM (curry return)
1046+-- | Pair each element of a fold input with its index, starting from index 0.
1049+{-# INLINE indexed #-}
1050+indexed :: forall m a b. MonadThrow m => Fold m (Int, a) b -> Parser m a b
1051+indexed = zip (D.enumerateFromIntegral 0 :: D.Stream m Int)
1053+-- | @makeIndexFilter indexer filter predicate@ generates a fold filtering
1054+-- function using a fold indexing function that attaches an index to each input
1055+-- element and a filtering function that filters using @(index, element) ->
1056+-- Bool) as predicate.
1061+-- filterWithIndex = makeIndexFilter indexed filter
1062+-- filterWithAbsTime = makeIndexFilter timestamped filter
1063+-- filterWithRelTime = makeIndexFilter timeIndexed filter
1067+{-# INLINE makeIndexFilter #-}
1069+ (Fold m (s, a) b -> Parser m a b)
1070+ -> (((s, a) -> Bool) -> Fold m (s, a) b -> Fold m (s, a) b)
1071+ -> (((s, a) -> Bool) -> Fold m a b -> Parser m a b)
1072+makeIndexFilter f comb g = f . comb g . FL.lmap snd
1074+-- | @sampleFromthen offset stride@ samples the element at @offset@ index and
1075+-- then every element at strides of @stride@.
1078+{-# INLINE sampleFromthen #-}
1079+sampleFromthen :: MonadThrow m => Int -> Int -> Fold m a b -> Parser m a b
1080+sampleFromthen offset size =
1081+ makeIndexFilter indexed FL.filter (\(i, _) -> (i + offset) `mod` size == 0)
978--------------------------------------------------------------------------------
1083--------------------------------------------------------------------------------
979--- Spanning
1084--- Spanning
980--------------------------------------------------------------------------------
1085--------------------------------------------------------------------------------