unfoldr package:streamly

>>> :{
unfoldr step s =
case step s of
Nothing -> Stream.nil
Just (a, b) -> a `Stream.cons` unfoldr step b
:}
Build a stream by unfolding a pure step function step starting from a seed s. The step function returns the next element in the stream and the next seed value. When it is done it returns Nothing and the stream ends. For example,
>>> :{
let f b =
if b > 2
then Nothing
else Just (b, b + 1)
in Stream.toList $ Stream.unfoldr f 0
:}
[0,1,2]
Build a stream by unfolding a monadic step function starting from a seed. The step function returns the next element in the stream and the next seed value. When it is done it returns Nothing and the stream ends. For example,
>>> :{
let f b =
if b > 2
then return Nothing
else return (Just (b, b + 1))
in Stream.toList $ Stream.unfoldrM f 0
:}
[0,1,2]
When run concurrently, the next unfold step can run concurrently with the processing of the output of the previous step. Note that more than one step cannot run concurrently as the next step depends on the output of the previous step.
>>> :{
let f b =
if b > 2
then return Nothing
else threadDelay 1000000 >> return (Just (b, b + 1))
in Stream.toList $ Stream.delay 1 $ Stream.fromAsync $ Stream.unfoldrM f 0
:}
[0,1,2]
Concurrent Since: 0.1.0
Build a stream by unfolding a monadic step function starting from a seed. The step function returns the next element in the stream and the next seed value. When it is done it returns Nothing and the stream ends. For example,
let f b =
if b > 3
then return Nothing
else print b >> return (Just (b, b + 1))
in drain $ unfoldrM f 0
0
1
2
3
Pre-release