So Python has a nice concept of generators - functions that become iterators when called:
def step(x, s):
while True:
yield x
x = x + s
This is called like so:
s = step(1, 2)
s.next() # 1
s.next() # 3
s.next() # 5
And it implements the iterator protocol so you can pass it wherever an iterator is expected.
What’s the idiomatic way in Elixir to do this? We can get partway there with:
s = Stream.unfold(1, fn(x) -> {x, x+2} end)
Enum.take(s, 3) # [1, 3, 5]
But obviously this is not stateful. To get stateful, a process would be needed… But even if we don’t maintain state, I couldn’t find an easy way to do this:
{[1, 3], s} = Stream.split(s, 2)
{[5], s} = Stream.split(s, 1)
Using Enum.split
blocks, probably trying to reach the end of the Stream.
Thoughts?