@@ -113,6 +113,7 @@ module Streamly.Internal.Data.Stream.StreamD.Transform
113 -- | Map using the previous element.
113 -- | Map using the previous element.
114 , rollingMap
114 , rollingMap
115 , rollingMapM
115 , rollingMapM
117 -- * Maybe Streams
118 -- * Maybe Streams
118 , mapMaybe
119 , mapMaybe
@@ -1200,32 +1201,36 @@ slicesBy p (Stream step1 state1) = Stream step (Just (state1, 0, 0))
1200-- Rolling map
1201-- Rolling map
1201------------------------------------------------------------------------------
1202------------------------------------------------------------------------------
1203−data RollingMapState s a = RollingMapInit s | RollingMapGo s a
1204+data RollingMapState s a = RollingMapGo s a
1205{-# INLINE rollingMapM #-}
1206{-# INLINE rollingMapM #-}
1206−rollingMapM :: Monad m => (a -> a -> m b) -> Stream m a -> Stream m b
1207+rollingMapM :: Monad m => (Maybe a -> a -> m b) -> Stream m a -> Stream m b
1207−rollingMapM f (Stream step1 state1) = Stream step (RollingMapInit state1)
1208+rollingMapM f (Stream step1 state1) = Stream step (RollingMapGo state1 Nothing)
1209− step gst (RollingMapInit st) = do
1210− r <- step1 (adaptState gst) st
1212− Yield x s -> Skip $ RollingMapGo s x
1213− Skip s -> Skip $ RollingMapInit s
1216− step gst (RollingMapGo s1 x1) = do
1212+ step gst (RollingMapGo s1 curr) = do
1217 r <- step1 (adaptState gst) s1
1213 r <- step1 (adaptState gst) s1
1218 case r of
1214 case r of
1219 Yield x s -> do
1215 Yield x s -> do
1220− !res <- f x x1
1216+ !res <- f curr x
1221− return $ Yield res $ RollingMapGo s x
1217+ return $ Yield res $ RollingMapGo s (Just x)
1222− Skip s -> return $ Skip $ RollingMapGo s x1
1218+ Skip s -> return $ Skip $ RollingMapGo s curr
1223 Stop -> return Stop
1219 Stop -> return Stop
1225{-# INLINE rollingMap #-}
1221{-# 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
1227rollingMap f = rollingMapM (\x y -> return $ f x y)
1223rollingMap f = rollingMapM (\x y -> return $ f x y)
1225+{-# INLINE rollingMap2 #-}
1226+rollingMap2 :: Monad m => (a -> a -> b) -> Stream m a -> Stream m b
1227+rollingMap2 f = catMaybes . rollingMap g
1231+ g Nothing _ = Nothing
1232+ g (Just x) y = Just (f x y)
1229------------------------------------------------------------------------------
1234------------------------------------------------------------------------------
1230-- Maybe Streams
1235-- Maybe Streams
1231------------------------------------------------------------------------------
1236------------------------------------------------------------------------------
@@ -1238,3 +1243,7 @@ mapMaybe f = fmap fromJust . filter isJust . map f
1238{-# INLINE_NORMAL mapMaybeM #-}
1243{-# INLINE_NORMAL mapMaybeM #-}
1239mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Stream m a -> Stream m b
1244mapMaybeM :: Monad m => (a -> m (Maybe b)) -> Stream m a -> Stream m b
1240mapMaybeM f = fmap fromJust . filter isJust . mapM f
1245mapMaybeM f = fmap fromJust . filter isJust . mapM f
1247+{-# INLINE catMaybes #-}
1248+catMaybes :: (Monad m) => Stream m (Maybe a) -> Stream m a
1249+catMaybes = fmap fromJust . filter isJust