@@ -103,6 +103,12 @@ module Streamly.Streams.StreamD
103103 -- ** By folding (scans)
104104 , scanlM'
105105 , scanl'
106+ , scanlM
107+ , scanl
108+ , scanl1M'
109+ , scanl1'
110+ , scanl1M
111+ , scanl1
106112
107113 -- * Filtering
108114 , filter
@@ -148,7 +154,7 @@ import Prelude
148154 hiding (map, mapM, mapM_, repeat, foldr, last, take, filter,
149155 takeWhile, drop, dropWhile, all, any, maximum, minimum, elem,
150156 notElem, null, head, tail, zipWith, lookup, foldr1, sequence,
151− (!!))
157+ (!!), scanl, scanl1)
152158
153159import Streamly.SVar (MonadAsync, State(..), defState, rstState)
154160
@@ -630,6 +636,20 @@ postscanlM' fstep begin (Stream step state) =
630636 Skip s -> return $ Skip (s, acc)
631637 Stop -> return Stop
632638
639+{-# INLINE_NORMAL postscanlM #-}
640+postscanlM :: Monad m => (b -> a -> m b) -> b -> Stream m a -> Stream m b
641+postscanlM fstep begin (Stream step state) = Stream step' (state, begin)
642+ where
643+ {-# INLINE_LATE step' #-}
644+ step' gst (st, acc) = do
645+ r <- step (rstState gst) st
646+ case r of
647+ Yield x s -> do
648+ y <- fstep acc x
649+ return (Yield y (s, y))
650+ Skip s -> return $ Skip (s, acc)
651+ Stop -> return Stop
652+
633653{-# INLINE_NORMAL scanlM' #-}
634654scanlM' :: Monad m => (b -> a -> m b) -> b -> Stream m a -> Stream m b
635655scanlM' fstep begin s = begin `seq` (begin `cons` postscanlM' fstep begin s)
@@ -638,6 +658,64 @@ scanlM' fstep begin s = begin `seq` (begin `cons` postscanlM' fstep begin s)
638658scanl' :: Monad m => (b -> a -> b) -> b -> Stream m a -> Stream m b
639659scanl' f = scanlM' (\a b -> return (f a b))
640660
661+{-# INLINE_NORMAL scanlM #-}
662+scanlM :: Monad m => (b -> a -> m b) -> b -> Stream m a -> Stream m b
663+scanlM fstep begin s = begin `cons` postscanlM fstep begin s
664+
665+{-# INLINE scanl #-}
666+scanl :: Monad m => (b -> a -> b) -> b -> Stream m a -> Stream m b
667+scanl f = scanlM (\a b -> return (f a b))
668+
669+{-# INLINE_NORMAL scanl1M #-}
670+scanl1M :: Monad m => (a -> a -> m a) -> Stream m a -> Stream m a
671+scanl1M fstep (Stream step state) = Stream step' (state, Nothing)
672+ where
673+ {-# INLINE_LATE step' #-}
674+ step' gst (st, Nothing) = do
675+ r <- step (rstState gst) st
676+ case r of
677+ Yield x s -> return $ Yield x (s, Just x)
678+ Skip s -> return $ Skip (s, Nothing)
679+ Stop -> return Stop
680+
681+ step' gst (st, Just acc) = do
682+ r <- step (rstState gst) st
683+ case r of
684+ Yield y s -> do
685+ z <- fstep acc y
686+ return $ Yield z (s, Just z)
687+ Skip s -> return $ Skip (s, Just acc)
688+ Stop -> return Stop
689+
690+{-# INLINE scanl1 #-}
691+scanl1 :: Monad m => (a -> a -> a) -> Stream m a -> Stream m a
692+scanl1 f = scanl1M (\x y -> return (f x y))
693+
694+{-# INLINE_NORMAL scanl1M' #-}
695+scanl1M' :: Monad m => (a -> a -> m a) -> Stream m a -> Stream m a
696+scanl1M' fstep (Stream step state) = Stream step' (state, Nothing)
697+ where
698+ {-# INLINE_LATE step' #-}
699+ step' gst (st, Nothing) = do
700+ r <- step (rstState gst) st
701+ case r of
702+ Yield x s -> x `seq` return $ Yield x (s, Just x)
703+ Skip s -> return $ Skip (s, Nothing)
704+ Stop -> return Stop
705+
706+ step' gst (st, Just acc) = acc `seq` do
707+ r <- step (rstState gst) st
708+ case r of
709+ Yield y s -> do
710+ z <- fstep acc y
711+ z `seq` return $ Yield z (s, Just z)
712+ Skip s -> return $ Skip (s, Just acc)
713+ Stop -> return Stop
714+
715+{-# INLINE scanl1' #-}
716+scanl1' :: Monad m => (a -> a -> a) -> Stream m a -> Stream m a
717+scanl1' f = scanl1M' (\x y -> return (f x y))
718+
641719-------------------------------------------------------------------------------
642720-- Filtering
643721-------------------------------------------------------------------------------