Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions lib/elixir/lib/enum.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3924,8 +3924,9 @@ defmodule Enum do
If an integer offset is given as `fun_or_offset`, it will index from the given
offset instead of from zero.

If a function is given as `fun_or_offset`, it will index by invoking the function
for each element and index (zero-based) of the enumerable.
If a 2-arity function is given as `fun_or_offset`, the function will be invoked
for each element in `enumerable` as the first argument and with a zero-based
index as the second. The result of the invocation will be listed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"will be listed" is not really clear what it means in this context.

Copy link
Contributor Author

@dmitrykleymenov dmitrykleymenov May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is will be collected better? Or will be collected in the result list?
The return value of the anonimous function will be collected in the resulting list?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"It returns a list with the result of each invocation." ?

Copy link
Contributor Author

@dmitrykleymenov dmitrykleymenov May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the whole paragraph is about the anonimous function, and it returns may lead to misunderstanding fn returns the list

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So perhaps we can say "This function" or "Enum.zip/2 returns"


## Examples

Expand Down Expand Up @@ -4036,10 +4037,10 @@ defmodule Enum do
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}}]
iex> left = %{:a => 1, 1 => 3}
iex> right = %{:a => 1, :b => :c}
iex> Enum.zip(left, right)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I'm not sure if we need to change it to doctests, since it's example of antipattern, but here we have expression and concreete result, so may be it should be doctested too.
  2. This important note about zipping maps is only in docs for zip_with. May be we should move it to much more widely used zip/2, or even copy it to zip/1 and zip/2?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Order is not guaranteed for maps, as per comment above, so there is no guarantee the result below is what you would get, which is why it is not a doctest. The doctest will fail from time to time.

[{{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`.
Expand Down Expand Up @@ -4109,11 +4110,11 @@ defmodule Enum do

## Examples

iex> Enum.zip_reduce([1, 2], [3, 4], 0, fn x, y, acc -> x + y + acc end)
iex> Enum.zip_reduce([1, 2], [3, 4, 5], 0, fn x, y, acc -> x + y + acc end)
10

iex> Enum.zip_reduce([1, 2], [3, 4], [], fn x, y, acc -> [x + y | acc] end)
[6, 4]
iex> Enum.zip_reduce([1, 2, 3], [4, 5], [], fn x, y, acc -> [x + y | acc] end)
[7, 5]
"""
@doc since: "1.12.0"
@spec zip_reduce(t, t, acc, (enum1_elem :: term, enum2_elem :: term, acc -> acc)) :: acc
Expand Down Expand Up @@ -4143,13 +4144,13 @@ defmodule Enum do

## Examples

iex> enums = [[1, 1], [2, 2], [3, 3]]
iex> enums = [[1, 1, 1, 1], [2, 2, 2], [3, 3]]
...> Enum.zip_reduce(enums, [], fn elements, acc ->
...> [List.to_tuple(elements) | acc]
...> end)
[{1, 2, 3}, {1, 2, 3}]

iex> enums = [[1, 2], [a: 3, b: 4], [5, 6]]
iex> enums = [[1, 2], [a: 3, b: 4], [5, 6, 7]]
...> Enum.zip_reduce(enums, [], fn elements, acc ->
...> [List.to_tuple(elements) | acc]
...> end)
Expand Down