@@ -25,6 +25,7 @@ module Streamly.Prelude
2525 , unfoldrM
2626 , each
2727 , iterate
28+ , iterateM
2829
2930 -- * Elimination
3031 -- ** General Folds
@@ -126,8 +127,15 @@ each = Prelude.foldr cons nil
126127iterate :: Streaming t => (a -> a) -> a -> t m a
127128iterate step = fromStream . go
128129 where
129− go s = scons s (Just (go (step s)))
130+ go s = scons s (Just (go (step s)))
130131
132+-- | Iterate a monadic function from a seed value, streaming the results forever
133+iterateM :: (Streaming t, Monad m) => (a -> m a) -> m a -> t m a
134+iterateM step = fromStream . go
135+ where
136+ go ms = Stream $ \_ _ yld -> do
137+ s <- ms
138+ yld s (Just (go (step s)))
131139
132140-- | Read lines from an IO Handle into a stream of Strings.
133141fromHandle :: (Streaming t, MonadIO m) => IO.Handle -> t m String