Is there a way to make list comprehension lazy in Elixir? If not, is there a way to turn this into a Stream
?
my_list = for i <- (1..1000000), j <- (1..1000000), do: {i, j}
This code snippet blows my program by taking too much memory.
I want to apply a filter, map and reduce on my_list.
A comprehension is a flat map. So your code is equivalent to:
Stream.flat_map 1..1000000, fn i ->
Stream.flat_map 1..1000000, fn j ->
[{i, j}]
end
end
I have proposed a "stream for" and "parallel for" for future Elixir versions, however it is pending some other improvements to the language.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With