on master help sign in
Add a rollingMapM fold
6d24791Harendra Kumar
core/src/Streamly/Internal/Data/Fold.hs+24
@@ -65,6 +65,7 @@ module Streamly.Internal.Data.Fold
65 , rollingHashWithSalt
65 , rollingHashWithSalt
66 , rollingHashFirstN
66 , rollingHashFirstN
67 -- , rollingHashLastN
67 -- , rollingHashLastN
68+ , rollingMapM
68
69
69 -- *** Saturating Reducers
70 -- *** Saturating Reducers
70 -- | 'product' terminates if it becomes 0. Other folds can theoretically
71 -- | 'product' terminates if it becomes 0. Other folds can theoretically
@@ -845,6 +846,29 @@ rollingHash = rollingHashWithSalt defaultSalt
845rollingHashFirstN :: (Monad m, Enum a) => Int -> Fold m a Int64
846rollingHashFirstN :: (Monad m, Enum a) => Int -> Fold m a Int64
846rollingHashFirstN n = take n rollingHash
847rollingHashFirstN n = take n rollingHash
847
848
849+-- | Apply a function on every two successive elements of a stream. The first
850+-- argument of the map function is the previous element and the second argument
851+-- is the current element. When processing the very first element in the
852+-- stream, the previous element is 'Nothing'.
853+--
854+-- /Pre-release/
855+--
856+{-# INLINE rollingMapM #-}
857+rollingMapM :: Monad m => (Maybe a -> a -> m b) -> Fold m a b
858+rollingMapM f = Fold step initial extract
859+
860+ where
861+
862+ -- XXX We need just a postscan. We do not need an initial result here.
863+ -- Or we can supply a default initial result as an argument to rollingMapM.
864+ initial = return $ Partial (Nothing, error "Empty stream")
865+
866+ step (prev, _) cur = do
867+ x <- f prev cur
868+ return $ Partial (Just cur, x)
869+
870+ extract = return . snd
871+
848------------------------------------------------------------------------------
872------------------------------------------------------------------------------
849-- Monoidal left folds
873-- Monoidal left folds
850------------------------------------------------------------------------------
874------------------------------------------------------------------------------