简体   繁体   English

Elixir中的惰性列表理解?

[英]Lazy list comprehension in Elixir?

Is there a way to make list comprehension lazy in Elixir? 有没有一种方法可以使Elixir中的列表理解变得懒惰? If not, is there a way to turn this into a Stream ? 如果不是,是否有办法将其转换为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. 我想应用一个过滤器,映射并减少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. 我已经为将来的Elixir版本提出了“针对”和“针对”的建议,但仍在对该语言进行其他一些改进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM