Enum.zip_with

You're seeing just the function zip_with, go back to Enum module for more information.
Link to this function

zip_with(enumerables, zip_fun)

View Source (since 1.12.0)

Specs

zip_with(t(), ([term()] -> term())) :: [term()]

Zips corresponding elements from a finite collection of enumerables into list, 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 runs out of elements.

Returns a list with all the results of calling zip_fun.

Examples

iex> Enum.zip_with([[1, 2], [3, 4], [5, 6]], fn [x, y, z] -> x + y + z end)
[9, 12]

iex> Enum.zip_with([[1, 2], [3, 4]], fn [x, y] -> x + y end)
[4, 6]
Link to this function

zip_with(enumerable1, enumerable2, zip_fun)

View Source (since 1.12.0)

Specs

zip_with(t(), t(), (enum1_elem :: term(), enum2_elem :: term() -> term())) :: [
  term()
]

Zips corresponding elements from two enumerables into a list, transforming them with the zip_fun function as it goes.

The corresponding elements from each collection are passed to the provided 2-arity zip_fun function in turn. Returns a list that contains the result of calling zip_fun for each pair of elements.

The zipping finishes as soon as either enumerable runs out of elements.

Zipping Maps

It's important to remember that zipping inherently relies on order. If you zip two lists you get the element at the index from each list in turn. If we zip two maps together it's tempting to think that you will get the given key in the left map and the matching key in the right map, but there is no such guarantee because map keys are not ordered! Consider the following:

left =  %{:a => 1, 1 => 3}
right = %{:a => 1, :b => :c}
Enum.zip(left, right)
# [{{1, 3}, {:a, 1}}, {{:a, 1}, {:b, :c}}]

As you can see :a does not get paired with :a. If this is what you want, you should use Map.merge/3.

Examples

iex> Enum.zip_with([1, 2], [3, 4], fn x, y -> x + y end)
[4, 6]

iex> Enum.zip_with([1, 2], [3, 4, 5, 6], fn x, y -> x + y end)
[4, 6]

iex> Enum.zip_with([1, 2, 5, 6], [3, 4], fn x, y -> x + y end)
[4, 6]