Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Streams

stream.livemd

Streams

Lazy Evaluation

stream =
  1..10
  |> Stream.map(fn int -> int * 2 end)
  |> Stream.map(fn int -> int * 2 end)
  |> Stream.map(fn int -> int * 2 end)
  |> Stream.map(fn int -> int * 2 end)
Enum.take(stream, 5)

Generators

Stream.iterate(0, fn
  acc -> acc + 1
end)
|> Enum.take()
3 ** 3
Stream.unfold(1, fn acc ->
  element = acc ** 2
  IO.inspect(acc, label: "ACC")
  IO.inspect(element, label: "EL")
  {element, acc + 1}
end)
|> Enum.take(5)

# acc = 1
# 1 + 1
# 2 + 1
# 3 + 1
# 4 + 1
# 5

# 1 ** 2
# 2 ** 2
# 3 ** 2
# 4 ** 2
# 5 ** 2