@@ -24,6 +24,8 @@ module Streamly.Prelude
25 , unfoldrM
25 , unfoldrM
28 -- * Elimination
30 -- * Elimination
29 -- ** General Folds
31 -- ** General Folds
@@ -85,7 +87,8 @@ import Prelude hiding (filter, drop, dropWhile, take,
85 mapM, mapM_, sequence, all, any,
87 mapM, mapM_, sequence, all, any,
86 sum, product, elem, notElem,
88 sum, product, elem, notElem,
87 maximum, minimum, head, last,
89 maximum, minimum, head, last,
88− tail, length, null, reverse)
90+ tail, length, null, reverse,
89import qualified Prelude
92import qualified Prelude
90import qualified System.IO as IO
93import qualified System.IO as IO
@@ -120,6 +123,20 @@ unfoldrM step = fromStream . go
120each :: (Streaming t, Foldable f) => f a -> t m a
123each :: (Streaming t, Foldable f) => f a -> t m a
121each = Prelude.foldr cons nil
124each = Prelude.foldr cons nil
126+-- | Iterate a pure function from a seed value, streaming the results forever
127+iterate :: Streaming t => (a -> a) -> a -> t m a
128+iterate step = fromStream . go
130+ go s = scons s (Just (go (step s)))
132+-- | Iterate a monadic function from a seed value, streaming the results forever
133+iterateM :: (Streaming t, Monad m) => (a -> m a) -> a -> t m a
134+iterateM step = fromStream . go
136+ go s = Stream $ \_ _ yld -> do
123-- | Read lines from an IO Handle into a stream of Strings.
140-- | Read lines from an IO Handle into a stream of Strings.
124fromHandle :: (Streaming t, MonadIO m) => IO.Handle -> t m String
141fromHandle :: (Streaming t, MonadIO m) => IO.Handle -> t m String
125fromHandle h = fromStream go
142fromHandle h = fromStream go