@@ -128,6 +128,7 @@ module Streamly.Internal.Data.Stream.StreamD
128128
129129 -- ** Specialized Folds
130130 , tap
131+ , tapOffsetEvery
131132 , tapAsync
132133 , tapRate
133134 , pollCounts
@@ -3279,6 +3280,39 @@ tap (Fold fstep initial extract) (Stream step state) = Stream step' Nothing
32793280 void $ extract acc
32803281 return $ Stop
32813282
3283+{-# INLINE_NORMAL tapOffsetEvery #-}
3284+tapOffsetEvery :: Monad m
3285+ => Int -> Int -> Fold m a b -> Stream m a -> Stream m a
3286+tapOffsetEvery offset n (Fold fstep initial extract) (Stream step state) =
3287+ Stream step' Nothing
3288+
3289+ where
3290+
3291+ {-# INLINE_LATE step' #-}
3292+ step' _ Nothing = do
3293+ r <- initial
3294+ return $ Skip (Just (r, state, offset `mod` n))
3295+
3296+ step' gst (Just (acc, st, count)) | count <= 0 = do
3297+ r <- step gst st
3298+ case r of
3299+ Yield x s -> do
3300+ !acc' <- fstep acc x
3301+ return $ Yield x (Just (acc', s, n - 1))
3302+ Skip s -> return $ Skip (Just (acc, s, count))
3303+ Stop -> do
3304+ void $ extract acc
3305+ return $ Stop
3306+
3307+ step' gst (Just (acc, st, count)) = do
3308+ r <- step gst st
3309+ case r of
3310+ Yield x s -> return $ Yield x (Just (acc, s, count - 1))
3311+ Skip s -> return $ Skip (Just (acc, s, count))
3312+ Stop -> do
3313+ void $ extract acc
3314+ return $ Stop
3315+
32823316{-# INLINE_NORMAL pollCounts #-}
32833317pollCounts
32843318 :: MonadAsync m