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

Streams

livebook-sessions/streams.livemd

Streams

import IEx.Helpers

Overview

:functions
|> Stream.__info__()
|> Enum.each(fn {name, arity} ->
  IO.puts "#{name}/#{arity}"
end)

Generators (constructors)

one_value = fn -> 1..2 |> Enum.random() end # [1, 2]
multiple_flips = Stream.repeatedly(one_value)
# this will not stop, would hang
# multiple_flips|> Enum.map(& &1)
multiple_flips|> Enum.take(10) |> Enum.map(& &1)

Section

head_or_tail = [:head, :tail] |> Stream.cycle() |> Enum.take(10)

Stream.iterate/2

count_stream = Stream.iterate(1, fn x -> x + 1 end) |> Stream.map(&IO.inspect/1)
count_stream |> Enum.take(10)
count_stream |> Enum.take(4)
# to take the next numbers after 10
count_stream |> Stream.drop(10) 
count_stream |> Stream.drop(10)  |> Enum.take(4)

Stream Transformations

Stream.repeatedly(fn -> :random.uniform(8) end) |> Enum.take(20)
Stream.repeatedly(fn -> 8 end)
|> Stream.map(fn x -> 1..x end)
|> Enum.take(20)
Stream.repeatedly(fn -> 8 end)
|> Stream.flat_map(fn x -> Enum.shuffle(1..x) end)
|> Enum.take(20)
random_groupings = 
  Stream.repeatedly(fn -> 8 end)
  |> Stream.flat_map(fn x -> Enum.shuffle(1..x) end)
random_groupings |> Enum.take(20)
line_numbers = Stream.iterate(1, fn n -> n + 1 end)
Stream.zip(random_groupings, line_numbers) |> Enum.take(20)
random_groupings |> Stream.with_index(1) |> Enum.take(20) 
striping = ~w(white grey)a |> Stream.cycle() 
Stream.zip(striping, Stream.with_index(random_groupings, 1)) 
|> Stream.map(fn {color, {n, i}} -> {i, n, color} end)
|> Enum.take(20)
range = 1..1_000_000
stream = Stream.map(range, &(&1 * 3))
IO.puts Enum.sum(stream)