@@ -3616,23 +3616,27 @@ scanlx' fstep begin done s =
3616------------------------------------------------------------------------------
3616------------------------------------------------------------------------------
3618{-# INLINE_NORMAL postscanlM' #-}
3618{-# INLINE_NORMAL postscanlM' #-}
3619−postscanlM' :: Monad m => (b -> a -> m b) -> b -> Stream m a -> Stream m b
3619+postscanlM' :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> Stream m b
3620postscanlM' fstep begin (Stream step state) =
3620postscanlM' fstep begin (Stream step state) =
3621− begin `seq` Stream step' (state, begin)
3621+ Stream step' Nothing
3623 {-# INLINE_LATE step' #-}
3623 {-# INLINE_LATE step' #-}
3624+ step' _ Nothing = do
3626+ return $ Skip (Just (state, x))
3624− step' gst (st, acc) = acc `seq` do
3628+ step' gst (Just (st, acc)) = do
3625 r <- step (adaptState gst) st
3629 r <- step (adaptState gst) st
3626 case r of
3630 case r of
3627 Yield x s -> do
3631 Yield x s -> do
3628− y <- fstep acc x
3632+ !y <- fstep acc x
3629− y `seq` return (Yield y (s, y))
3633+ return $ Yield y (Just (s, y))
3630− Skip s -> return $ Skip (s, acc)
3634+ Skip s -> return $ Skip (Just (s, acc))
3631 Stop -> return Stop
3635 Stop -> return Stop
3633{-# INLINE_NORMAL postscanl' #-}
3637{-# INLINE_NORMAL postscanl' #-}
3634postscanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
3638postscanl' :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
3635−postscanl' f = postscanlM' (\a b -> return (f a b))
3639+postscanl' f seed = postscanlM' (\a b -> return (f a b)) (return seed)
3637-- We can possibly have the "done" function as a Maybe to provide an option to
3641-- We can possibly have the "done" function as a Maybe to provide an option to
3638-- emit or not emit the accumulator when the stream stops.
3642-- emit or not emit the accumulator when the stream stops.
@@ -3660,26 +3664,43 @@ postscanlMAfter' fstep initial done (Stream step1 state1) = do
3660 step _ Nothing = return Stop
3664 step _ Nothing = return Stop
3662{-# INLINE_NORMAL postscanlM #-}
3666{-# INLINE_NORMAL postscanlM #-}
3663−postscanlM :: Monad m => (b -> a -> m b) -> b -> Stream m a -> Stream m b
3667+postscanlM :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> Stream m b
3664−postscanlM fstep begin (Stream step state) = Stream step' (state, begin)
3668+postscanlM fstep begin (Stream step state) = Stream step' Nothing
3666 {-# INLINE_LATE step' #-}
3670 {-# INLINE_LATE step' #-}
3671+ step' _ Nothing = do
3673+ return $ Skip (Just (state, r))
3667− step' gst (st, acc) = do
3675+ step' gst (Just (st, acc)) = do
3668 r <- step (adaptState gst) st
3676 r <- step (adaptState gst) st
3669 case r of
3677 case r of
3670 Yield x s -> do
3678 Yield x s -> do
3671 y <- fstep acc x
3679 y <- fstep acc x
3672− return (Yield y (s, y))
3680+ return (Yield y (Just (s, y)))
3673− Skip s -> return $ Skip (s, acc)
3681+ Skip s -> return $ Skip (Just (s, acc))
3674 Stop -> return Stop
3682 Stop -> return Stop
3676{-# INLINE_NORMAL postscanl #-}
3684{-# INLINE_NORMAL postscanl #-}
3677postscanl :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
3685postscanl :: Monad m => (a -> b -> a) -> a -> Stream m b -> Stream m a
3678−postscanl f = postscanlM (\a b -> return (f a b))
3686+postscanl f seed = postscanlM (\a b -> return (f a b)) (return seed)
3680{-# INLINE_NORMAL scanlM' #-}
3688{-# INLINE_NORMAL scanlM' #-}
3681−scanlM' :: Monad m => (b -> a -> m b) -> b -> Stream m a -> Stream m b
3689+scanlM' :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> Stream m b
3682−scanlM' fstep begin s = begin `seq` (begin `cons` postscanlM' fstep begin s)
3690+scanlM' fstep begin (Stream step state) = Stream step' Nothing
3692+ {-# INLINE_LATE step' #-}
3693+ step' _ Nothing = do
3695+ return $ Yield x (Just (state, x))
3696+ step' gst (Just (st, acc)) = do
3697+ r <- step (adaptState gst) st
3701+ return $ Yield y (Just (s, y))
3702+ Skip s -> return $ Skip (Just (s, acc))
3703+ Stop -> return Stop
3684{-# INLINE scanlMAfter' #-}
3705{-# INLINE scanlMAfter' #-}
3685scanlMAfter' :: Monad m
3706scanlMAfter' :: Monad m
@@ -3690,15 +3711,28 @@ scanlMAfter' fstep initial done s =
3691{-# INLINE scanl' #-}
3712{-# INLINE scanl' #-}
3692scanl' :: Monad m => (b -> a -> b) -> b -> Stream m a -> Stream m b
3713scanl' :: Monad m => (b -> a -> b) -> b -> Stream m a -> Stream m b
3693−scanl' f = scanlM' (\a b -> return (f a b))
3714+scanl' f seed = scanlM' (\a b -> return (f a b)) (return seed)
3695{-# INLINE_NORMAL scanlM #-}
3716{-# INLINE_NORMAL scanlM #-}
3696−scanlM :: Monad m => (b -> a -> m b) -> b -> Stream m a -> Stream m b
3717+scanlM :: Monad m => (b -> a -> m b) -> m b -> Stream m a -> Stream m b
3697−scanlM fstep begin s = begin `cons` postscanlM fstep begin s
3718+scanlM fstep begin (Stream step state) = Stream step' Nothing
3720+ {-# INLINE_LATE step' #-}
3721+ step' _ Nothing = do
3723+ return $ Yield x (Just (state, x))
3724+ step' gst (Just (st, acc)) = do
3725+ r <- step (adaptState gst) st
3729+ return $ Yield y (Just (s, y))
3730+ Skip s -> return $ Skip (Just (s, acc))
3731+ Stop -> return $ Stop
3699{-# INLINE scanl #-}
3733{-# INLINE scanl #-}
3700scanl :: Monad m => (b -> a -> b) -> b -> Stream m a -> Stream m b
3734scanl :: Monad m => (b -> a -> b) -> b -> Stream m a -> Stream m b
3701−scanl f = scanlM (\a b -> return (f a b))
3735+scanl f seed = scanlM (\a b -> return (f a b)) (return seed)
3703{-# INLINE_NORMAL scanl1M #-}
3737{-# INLINE_NORMAL scanl1M #-}
3704scanl1M :: Monad m => (a -> a -> m a) -> Stream m a -> Stream m a
3738scanl1M :: Monad m => (a -> a -> m a) -> Stream m a -> Stream m a