So I know this problem can be solved using recursion so I’m just asking it out of curiosity. I’ve been reading about implementing loop-like behaviours using Stream in Elixir and I’m trying to use
Stream.cycle/1 to do this:
Generate random numbers between 1 and 20, and stop generating them after the value 20 has been generated. This is how I wanted to solve it:
numbers = for _ <- 1..20, do: :rand.uniform(20) Stream.cycle(numbers) |> Enum.take_while(fn x -> x != 20) |> Enum.to_list
But this never returns. Is there a way to accomplish this with Stream ?