@@ -45,6 +45,7 @@ module Streamly.Prelude
4545 -- generation function.
4646 , once
4747 , replicateM
48+ , repeatM
4849 , iterate
4950 , iterateM
5051 , fromFoldable
@@ -167,13 +168,41 @@ fromFoldable = Prelude.foldr cons nil
167168each :: (IsStream t, Foldable f) => f a -> t m a
168169each = fromFoldable
169170
170−-- | Iterate a pure function from a seed value, streaming the results forever
171+-- | Create a singleton stream by executing a monadic action once. Same as
172+-- @m \`consM` nil@ but more efficient.
173+--
174+-- @
175+-- > toList $ once getLine
176+-- hello
177+-- ["hello"]
178+-- @
179+once :: (IsStream t, Monad m) => m a -> t m a
180+once = fromStream . S.once
181+
182+-- | Generate a stream by performing a monadic action @n@ times.
183+replicateM :: (IsStream t, Monad m) => Int -> m a -> t m a
184+replicateM n m = fromStream $ go n
185+ where
186+ go cnt = Stream $ \_ stp _ yld ->
187+ if cnt <= 0
188+ then stp
189+ else m >>= \a -> yld a (go (cnt - 1))
190+
191+-- | Generate a stream by repeatedly executing a monadic action forever.
192+repeatM :: (IsStream t, Monad m) => m a -> t m a
193+repeatM = fromStream . go
194+ where
195+ go m = Stream $ \_ _ _ yld ->
196+ m >>= \a -> yld a (go m)
197+
198+-- | Iterate a pure function from a seed value, streaming the results forever.
171199iterate :: IsStream t => (a -> a) -> a -> t m a
172200iterate step = fromStream . go
173201 where
174202 go s = S.cons s (go (step s))
175203
176−-- | Iterate a monadic function from a seed value, streaming the results forever
204+-- | Iterate a monadic function from a seed value, streaming the results
205+-- forever.
177206iterateM :: (IsStream t, Monad m) => (a -> m a) -> a -> t m a
178207iterateM step = fromStream . go
179208 where
@@ -559,15 +588,6 @@ sequence m = fromStream $ go (toStream m)
559588 yield ma r = ma >>= \b -> yld b (go r)
560589 in (S.runStream m1) Nothing stp single yield
561590
562−-- | Generate a stream by performing an action @n@ times.
563−replicateM :: (IsStream t, Monad m) => Int -> m a -> t m a
564−replicateM n m = fromStream $ go n
565− where
566− go cnt = Stream $ \_ stp _ yld ->
567− if cnt <= 0
568− then stp
569− else m >>= \a -> yld a (go (cnt - 1))
570−
571591------------------------------------------------------------------------------
572592-- Serially Zipping Streams
573593------------------------------------------------------------------------------