iterateM is:exact

Iterate a monadic function from a seed value, streaming the results forever
>>> iterateM f m = m >>= \a -> return a `Stream.consM` iterateM f (f a)
Generate an infinite stream with the first element generated by the action m and each successive element derived by applying the monadic function f on the previous element.
>>> pr n = threadDelay 1000000 >> print n

>>> :{
Stream.iterateM (\x -> pr x >> return (x + 1)) (return 0)
& Stream.take 3
& Stream.fromSerial
& Stream.toList
:}
0
1
[0,1,2]
When run concurrently, the next iteration can run concurrently with the processing of the previous iteration. Note that more than one iteration cannot run concurrently as the next iteration depends on the output of the previous iteration.
>>> :{
Stream.iterateM (\x -> pr x >> return (x + 1)) (return 0)
& Stream.delay 1
& Stream.take 3
& Stream.fromAsync
& Stream.toList
:}
0
1
...
Concurrent Since: 0.1.2 Since: 0.7.0 (signature change)
Generate an infinite stream with the first element generated by the action m and each successive element derived by applying the monadic function f on the previous element.
>>> :{
Stream.iterateM (\x -> print x >> return (x + 1)) (return 0)
& Stream.take 3
& Stream.toList
:}
0
1
[0,1,2]
Generates an infinite stream starting with the given seed and applying the given function repeatedly.
>>> iterateM f m = m >>= \a -> return a `StreamK.consM` iterateM f (f a)
Generate an infinite stream with the first element generated by the action m and each successive element derived by applying the monadic function f on the previous element.
>>> :{
StreamK.iterateM (\x -> print x >> return (x + 1)) (return 0)
& StreamK.take 3
& StreamK.toList
:}
0
1
[0,1,2]
Monadic equivalent to iterate. Note that it will not terminate, but may still be useful in the main event loop of a program, for example.
Monadic version of iterate. Can be used to produce trees given a children of node function.
import Data.List.Tree (bfsLayers)
take 3 $ bfsLayers (iterateM (\i -> [i*2, i*2+1]) [1] :: ListT [] Int)
[[1],[2,3],[4,5,6,7]]
Monadic version of iterate.