liveBook · Manning

Stream

As explained in Elixir in Action, Third Edition

Stream is the Elixir standard-library module that provides lazy operations and stream constructors for enumerable data.

Stream is Elixir’s lazy enumerable for composing transformations without immediately traversing or materializing the input. Stream.map/2, filter/2, and with_index/2 build a computation; an Enum function such as each/2, take/2, or to_list/1 realizes it. This can combine indexing and output in one pass instead of creating an intermediate list. section 3.4.5

Use streams when a pipeline benefits from deferred, composable iteration:

  • Compose Stream.filter/2, Stream.map/2, and Stream.with_index/2, then terminate with Enum.each/2 or another Enum function. section 3.4.5

Import and transform a CSV file

A file-import pipeline can stream lines, remove trailing newlines, split each line into date and title, convert the date, and construct to-do entries before passing the resulting enumerable to TodoList.new/1. Build it incrementally and inspect each intermediate result; once the eager version works, replace suitable Enum operations with Stream functions. section 4.2.5

How Elixir in Action, Third Edition covers it

How deep it goes
Explains deferred execution, composition, terminal consumption, file processing, and infinite generators without detailing internal implementation.
Examples
Demonstrates indexed employee output, numeric filtering, large-line detection, infinite numbers, interactive input, and CSV import.
What it assumes
Assumes familiarity with enumerable data, pipelines, anonymous functions, and Enum operations while introducing lazy iteration progressively.
What you can do after
Apply it via Stream and Enum to build composable pipelines over lists, files, infinite sequences, and interactive input.
How it connects
Connects lazy iteration with enumerables, Enum, comprehensions, file processing, and data pipelines.

Related concepts

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.