Understanding the lazy list in Elixir?

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 piece of code deletes my program, taking up too much memory.

I want to apply a filter, map and reduce by my_list.

+5
source share
1 answer

Understanding is a flat map. So your code is equivalent:

 Stream.flat_map 1..1000000, fn i -> Stream.flat_map 1..1000000, fn j -> [{i, j}] end end 

I suggested "stream for" and "parallel" for future versions of Elixir, however it expects some other language improvements.

+19
source

All Articles