From 5ac58b68195ca5b3b150ccf9419d5eb7199ed0a6 Mon Sep 17 00:00:00 2001 From: dmitrykleymenov Date: Wed, 21 May 2025 13:58:27 +0300 Subject: [PATCH 1/7] Add clarity to docs and doctests --- lib/elixir/lib/enum.ex | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/elixir/lib/enum.ex b/lib/elixir/lib/enum.ex index c61dd8ca17f..f54c33029d7 100644 --- a/lib/elixir/lib/enum.ex +++ b/lib/elixir/lib/enum.ex @@ -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. ## Examples @@ -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) + [{{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`. @@ -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 @@ -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) From cc1a4dfdf4db46605edae549f9af4a268600da82 Mon Sep 17 00:00:00 2001 From: Dmitry Kleymenov Date: Wed, 21 May 2025 15:20:18 +0300 Subject: [PATCH 2/7] Update lib/elixir/lib/enum.ex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: José Valim --- lib/elixir/lib/enum.ex | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/elixir/lib/enum.ex b/lib/elixir/lib/enum.ex index f54c33029d7..46bf5c58352 100644 --- a/lib/elixir/lib/enum.ex +++ b/lib/elixir/lib/enum.ex @@ -4113,6 +4113,9 @@ defmodule Enum do iex> Enum.zip_reduce([1, 2], [3, 4, 5], 0, fn x, y, acc -> x + y + acc end) 10 + If one of the lists has more entries than the others, + those entries are discarded: + iex> Enum.zip_reduce([1, 2, 3], [4, 5], [], fn x, y, acc -> [x + y | acc] end) [7, 5] """ From 3ccd1def7845c9c378b2344ed261a3d9c49b9e32 Mon Sep 17 00:00:00 2001 From: Dmitry Kleymenov Date: Wed, 21 May 2025 15:20:27 +0300 Subject: [PATCH 3/7] Update lib/elixir/lib/enum.ex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: José Valim --- lib/elixir/lib/enum.ex | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/elixir/lib/enum.ex b/lib/elixir/lib/enum.ex index 46bf5c58352..b59b2d8c61c 100644 --- a/lib/elixir/lib/enum.ex +++ b/lib/elixir/lib/enum.ex @@ -4153,6 +4153,9 @@ defmodule Enum do ...> end) [{1, 2, 3}, {1, 2, 3}] + If one of the lists has more entries than the others, + those entries are discarded: + iex> enums = [[1, 2], [a: 3, b: 4], [5, 6, 7]] ...> Enum.zip_reduce(enums, [], fn elements, acc -> ...> [List.to_tuple(elements) | acc] From 5259f9256dc66b4fcb8f16da21ac5c5c8ba4436e Mon Sep 17 00:00:00 2001 From: Dmitry Kleymenov Date: Wed, 21 May 2025 15:20:54 +0300 Subject: [PATCH 4/7] Update lib/elixir/lib/enum.ex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: José Valim --- lib/elixir/lib/enum.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/elixir/lib/enum.ex b/lib/elixir/lib/enum.ex index b59b2d8c61c..b39181a9f53 100644 --- a/lib/elixir/lib/enum.ex +++ b/lib/elixir/lib/enum.ex @@ -4147,7 +4147,7 @@ defmodule Enum do ## Examples - iex> enums = [[1, 1, 1, 1], [2, 2, 2], [3, 3]] + iex> enums = [[1, 1], [2, 2], [3, 3]] ...> Enum.zip_reduce(enums, [], fn elements, acc -> ...> [List.to_tuple(elements) | acc] ...> end) From cf9e53aa7199fd834c2619afa586dbbc150ea980 Mon Sep 17 00:00:00 2001 From: Dmitry Kleymenov Date: Wed, 21 May 2025 15:21:14 +0300 Subject: [PATCH 5/7] Update lib/elixir/lib/enum.ex MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: José Valim --- lib/elixir/lib/enum.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/elixir/lib/enum.ex b/lib/elixir/lib/enum.ex index b39181a9f53..87ab84d96d5 100644 --- a/lib/elixir/lib/enum.ex +++ b/lib/elixir/lib/enum.ex @@ -4110,7 +4110,7 @@ defmodule Enum do ## Examples - iex> Enum.zip_reduce([1, 2], [3, 4, 5], 0, fn x, y, acc -> x + y + acc end) + iex> Enum.zip_reduce([1, 2], [3, 4], 0, fn x, y, acc -> x + y + acc end) 10 If one of the lists has more entries than the others, From e95c5271b50a0a44e5185f9131d883f68f66fd68 Mon Sep 17 00:00:00 2001 From: dmitrykleymenov Date: Wed, 21 May 2025 15:25:10 +0300 Subject: [PATCH 6/7] Remove zipping maps doctest --- lib/elixir/lib/enum.ex | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/elixir/lib/enum.ex b/lib/elixir/lib/enum.ex index 87ab84d96d5..35b6d2d68b8 100644 --- a/lib/elixir/lib/enum.ex +++ b/lib/elixir/lib/enum.ex @@ -4037,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: - iex> left = %{:a => 1, 1 => 3} - iex> right = %{:a => 1, :b => :c} - iex> Enum.zip(left, right) - [{{1, 3}, {:a, 1}}, {{:a, 1}, {:b, :c}}] + 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`. From 5edb6e9078c73c23719337f9b56050fce6596dc3 Mon Sep 17 00:00:00 2001 From: dmitrykleymenov Date: Wed, 21 May 2025 17:09:28 +0300 Subject: [PATCH 7/7] Polish docs for `Enum.with_index/2` --- lib/elixir/lib/enum.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/elixir/lib/enum.ex b/lib/elixir/lib/enum.ex index 35b6d2d68b8..4e23ff0a519 100644 --- a/lib/elixir/lib/enum.ex +++ b/lib/elixir/lib/enum.ex @@ -3926,7 +3926,7 @@ defmodule Enum do 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. + index as the second. `with_index/2` returns a list with the result of each invocation. ## Examples