Lazy list comprehension in Elixir?

后端 未结 1 1035
清酒与你
清酒与你 2021-02-20 08:38

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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-20 09:06

    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.

    0 讨论(0)
提交回复
热议问题