Stream.zip_with
zip_with
, go back to Stream module for more information.
Specs
zip_with(enumerables, (Enumerable.t() -> term())) :: Enumerable.t() when enumerables: [Enumerable.t()] | Enumerable.t()
Lazily zips corresponding elements from a finite collection of enumerables into a new
enumerable, transforming them with the zip_fun
function as it goes.
The first element from each of the enums in enumerables
will be put into a list which is then passed to
the 1-arity zip_fun
function. Then, the second elements from each of the enums are put into a list and passed to
zip_fun
, and so on until any one of the enums in enumerables
completes.
Returns a new enumerable with the results of calling zip_fun
.
Examples
iex> concat = Stream.concat(1..3, 4..6)
iex> Stream.zip_with([concat, concat], fn [a, b] -> a + b end) |> Enum.to_list()
[2, 4, 6, 8, 10, 12]
iex> concat = Stream.concat(1..3, 4..6)
iex> Stream.zip_with([concat, concat, 1..3], fn [a, b, c] -> a + b + c end) |> Enum.to_list()
[3, 6, 9]
Specs
zip_with(Enumerable.t(), Enumerable.t(), (term(), term() -> term())) :: Enumerable.t()
Lazily zips corresponding elements from two enumerables into a new one, transforming them with
the zip_fun
function as it goes.
The zip_fun
will be called with the first element from enumerable1
and the first
element from enumerable2
, then with the second element from each, and so on until
either one of the enumerables completes.
Examples
iex> concat = Stream.concat(1..3, 4..6)
iex> Stream.zip_with(concat, concat, fn a, b -> a + b end) |> Enum.to_list()
[2, 4, 6, 8, 10, 12]