Streams & Laziness
Stream builds lazy, composable enumerables — computed only when consumed.
Stream builds lazy, composable enumerables — computed only when consumed.
Infinite streams:
Resource-backed streams:
Other functions: Stream.chunk_every, Stream.with_index, Stream.take_while, Stream.transform, Stream.zip, Stream.dedup, Stream.flat_map, Stream.run.
Rule of thumb: use Enum by default; use Stream for large/infinite data or I/O.
1..10_000_000
|> Stream.map(&(&1 * 3))
|> Stream.filter(&(rem(&1, 2) == 0))
|> Enum.take(5) # only computes what is needed
# [6, 12, 18, 24, 30]Stream.iterate(1, &(&1 * 2)) |> Enum.take(10)
Stream.cycle([:red, :green]) |> Enum.take(5)
Stream.repeatedly(fn -> :rand.uniform(6) end) |> Enum.take(3)
Stream.unfold({0, 1}, fn {a, b} -> {a, {b, a + b}} end) |> Enum.take(10) # FibonacciFile.stream!("big.log")
|> Stream.filter(&String.contains?(&1, "ERROR"))
|> Stream.map(&String.trim/1)
|> Enum.each(&IO.puts/1)
Stream.resource(
fn -> open_connection() end,
fn conn -> case fetch_batch(conn) do [] -> {:halt, conn}; rows -> {rows, conn} end end,
fn conn -> close(conn) end
)