liveBook · Manning

Lazy enumerable

As explained in Elixir in Action, Third Edition

A lazy enumerable is an enumerable that computes and produces its values only when a consumer requests them.

A lazy enumerable computes values on demand instead of producing the result immediately. In Elixir, Stream functions build a computation structure; an Enum function triggers iteration. section 3.4.5

This changes both performance and control flow:

  • Compose transformations without creating intermediate lists, allowing multiple operations in one pass. section 3.4.5
  • Let a consumer such as Enum.to_list, Enum.each, or Enum.take request values and determine how far iteration proceeds. section 3.4.5

Streaming a file pipeline

File.stream! can feed lines through trimming and filtering while avoiding an eagerly built intermediate result. The same pattern supports filtering, mapping, indexing, and printing in a single pipeline.

How Elixir in Action, Third Edition covers it

How deep it goes
Explains deferred structures, eager consumers, one-by-one production, and early termination, without descending into Stream internals.
Examples
Demonstrates list transformation, employee indexing, file-line filtering, infinite natural numbers, and repeated console input.
What it assumes
Assumes familiarity with enumerable collections and Enum operations while clarifying how streams defer their work.
What you can do after
Apply lazy pipelines with Stream and materialize or limit them through named Enum functions.
How it connects
Connects streams with enumerables, Enum functions, eager operations, file streams, and infinite sequences.

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.