@@ -113,6 +113,7 @@ module Streamly.Internal.Data.Stream.StreamD.Transform
113113 -- | Map using the previous element.
114114 , rollingMap
115115 , rollingMapM
116+ , rollingMap2
116117
117118 -- * Maybe Streams
118119 , mapMaybe
@@ -1200,32 +1201,36 @@ slicesBy p (Stream step1 state1) = Stream step (Just (state1, 0, 0))
12001201-- Rolling map
12011202------------------------------------------------------------------------------
12021203
1203−data RollingMapState s a = RollingMapInit s | RollingMapGo s a
1204+data RollingMapState s a = RollingMapGo s a
12041205
12051206{-# INLINE rollingMapM #-}
1206−rollingMapM :: Monad m => (a -> a -> m b) -> Stream m a -> Stream m b
1207−rollingMapM f (Stream step1 state1) = Stream step (RollingMapInit state1)
1207+rollingMapM :: Monad m => (Maybe a -> a -> m b) -> Stream m a -> Stream m b
1208+rollingMapM f (Stream step1 state1) = Stream step (RollingMapGo state1 Nothing)
1209+
12081210 where
1209− step gst (RollingMapInit st) = do
1210− r <- step1 (adaptState gst) st
1211− return $ case r of
1212− Yield x s -> Skip $ RollingMapGo s x
1213− Skip s -> Skip $ RollingMapInit s
1214− Stop -> Stop
12151211
1216− step gst (RollingMapGo s1 x1) = do
1212+ step gst (RollingMapGo s1 curr) = do
12171213 r <- step1 (adaptState gst) s1
12181214 case r of
12191215 Yield x s -> do
1220− !res <- f x x1
1221− return $ Yield res $ RollingMapGo s x
1222− Skip s -> return $ Skip $ RollingMapGo s x1
1216+ !res <- f curr x
1217+ return $ Yield res $ RollingMapGo s (Just x)
1218+ Skip s -> return $ Skip $ RollingMapGo s curr
12231219 Stop -> return Stop
12241220
12251221{-# INLINE rollingMap #-}
1226−rollingMap :: Monad m => (a -> a -> b) -> Stream m a -> Stream m b
1222+rollingMap :: Monad m => (Maybe a -> a -> b) -> Stream m a -> Stream m b
12271223rollingMap f = rollingMapM (\x y -> return $ f x y)
12281224
1225+{-# INLINE rollingMap2 #-}
1226+rollingMap2 :: Monad m => (a -> a -> b) -> Stream m a -> Stream m b
1227+rollingMap2 f = catMaybes . rollingMap g
1228+
1229+ where
1230+
1231+ g Nothing _ = Nothing
1232+ g (Just x) y = Just (f x y)
1233+
12291234------------------------------------------------------------------------------
12301235-- Maybe Streams
12311236------------------------------------------------------------------------------
@@ -1238,3 +1243,7 @@ mapMaybe f = fmap fromJust . filter isJust . map f
12381243{-# INLINE_NORMAL mapMaybeM #-}
12391244mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Stream m a -> Stream m b
12401245mapMaybeM f = fmap fromJust . filter isJust . mapM f
1246+
1247+{-# INLINE catMaybes #-}
1248+catMaybes :: (Monad m) => Stream m (Maybe a) -> Stream m a
1249+catMaybes = fmap fromJust . filter isJust