>>> 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)
>>> :{ Stream.iterateM (\x -> print x >> return (x + 1)) (return 0) & Stream.take 3 & Stream.toList :} 0 1 [0,1,2]
>>> 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]
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]]
iterateMapLeftsWith combine f = iterateMapWith combine (either f (const nil))To traverse a directory tree:
iterateMapLeftsWith serial Dir.toEither (fromPure (Left "tmp"))Pre-release
iterateM f = iterateMapWith serial (fromEffect . f) . fromEffectIt can be used to traverse a tree structure. For example, to list a directory tree:
Stream.iterateMapWith Stream.serial (either Dir.toEither (const nil)) (fromPure (Left "tmp"))Pre-release
>>> import Data.Monoid (Sum(..)) >>> f x = return (Fold.take 2 (Fold.sconcat x)) >>> s = Stream.map Sum $ Stream.fromList [1..10] >>> Stream.toList $ Stream.map getSum $ Stream.foldIterateM f (pure 0) s [3,10,21,36,55,55]This is the streaming equivalent of monad like sequenced application of folds where next fold is dependent on the previous fold. Pre-release
>>> import Data.Monoid (Sum(..)) >>> f x = return (Fold.take 2 (Fold.sconcat x)) >>> s = fmap Sum $ Stream.fromList [1..10] >>> Stream.fold Fold.toList $ fmap getSum $ Stream.foldIterateM f (pure 0) s [3,10,21,36,55,55]This is the streaming equivalent of monad like sequenced application of folds where next fold is dependent on the previous fold. Pre-release