使用 Stream 模块进行惰性求值
使用 Elixir 流高效处理大型或无限集合;流会按需惰性地计算值。
使用 Stream 模块进行惰性求值 是 CoddyKit 上的免费 Elixir & Phoenix: Scalable Backend Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Elixir & Phoenix: Scalable Backend Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Elixir & Phoenix: Scalable Backend Development 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Eager vs Lazy
Enum is eager: each step builds a full intermediate list. Stream is lazy: it composes operations and computes only when needed.
The Cost of Eager Pipelines
Chaining several Enum calls walks the whole collection multiple times and allocates a list at each stage.
1..1_000_000
|> Enum.map(&(&1 * 2))
|> Enum.filter(&(rem(&1, 3) == 0))
|> Enum.take(5)
|> IO.inspect()Building a Stream
Swap Enum for Stream to defer work. Nothing runs until a consuming function is called.
stream =
1..1_000_000
|> Stream.map(&(&1 * 2))
|> Stream.filter(&(rem(&1, 3) == 0))
IO.inspect(stream)Forcing Evaluation
A stream stays inert until an Enum function (like take or to_list) pulls values through it.
result =
1..1_000_000
|> Stream.map(&(&1 * 2))
|> Enum.take(5)
IO.inspect(result)Why Streams Are Efficient
Lazy pipelines fuse the steps and stop as soon as enough elements are produced, avoiding huge intermediate lists.
Infinite Streams
Stream.iterate/2 creates endless sequences that are safe because evaluation is lazy.
Stream.iterate(0, &(&1 + 1))
|> Enum.take(5)
|> IO.inspect()Stream.cycle for Repetition
Stream.cycle repeats a finite list forever, useful for assigning rotating values.
Stream.cycle(["a", "b", "c"])
|> Enum.take(7)
|> IO.inspect()Generating with Stream.unfold
Stream.unfold/2 builds a stream from a state and a function returning the next value and new state.
Stream.unfold(1, fn n -> {n, n * 2} end)
|> Enum.take(5)
|> IO.inspect()Streaming Files Line by Line
File.stream!/1 reads a file lazily, one line at a time, so you can process huge files with constant memory.
# File.stream!("big.log")
# |> Stream.filter(&String.contains?(&1, "ERROR"))
# |> Enum.count()Stream.chunk_every
Group a stream into batches lazily, handy for paginating work without loading everything.
1..10
|> Stream.chunk_every(3)
|> Enum.to_list()
|> IO.inspect()When to Choose Stream
Use Stream for large datasets, multi-step pipelines, infinite sequences, or IO. Use Enum for small collections where simplicity wins.
Quick Check
Test your lazy evaluation knowledge.
Recap
You learned lazy evaluation:
Enumis eager;Streamis lazy and composable- Streams compute only when consumed by an
Enumfunction - They enable infinite sequences and constant-memory file processing
- Prefer streams for large/multi-step pipelines, Enum for small data
用 AI 导师学习 Elixir — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「使用 Stream 模块进行惰性求值」课时是免费的吗?
是的 — 「使用 Stream 模块进行惰性求值」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Elixir & Phoenix: Scalable Backend Development 课程的其余内容,请升级到 CoddyKit PRO。 Elixir & Phoenix: Scalable Backend Development 课程共包含 4 节课。
「使用 Stream 模块进行惰性求值」这节课中我会学到什么?
使用 Elixir 流高效处理大型或无限集合;流会按需惰性地计算值。 你通过在浏览器中直接运行的动手代码来练习 Elixir & Phoenix: Scalable Backend Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Elixir & Phoenix: Scalable Backend Development 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Elixir & Phoenix: Scalable Backend Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用 Stream 模块进行惰性求值」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Elixir & Phoenix: Scalable Backend Development 课中编写并运行代码吗?
能。每节 Elixir & Phoenix: Scalable Backend Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 函数、模块与管道操作
- 处理可枚举集合
- 递归与高阶函数
- 使用 Stream 模块进行惰性求值