From 025ec8e42b4c96f6ec80b4def87bb1efa15f2109 Mon Sep 17 00:00:00 2001 From: sabiwara Date: Fri, 9 Aug 2024 07:31:15 +0900 Subject: [PATCH 1/2] Soft-deprecate List.zip/1 in favor of Enum.zip/1 --- CHANGELOG.md | 4 ++++ lib/elixir/lib/list.ex | 16 +--------------- lib/elixir/test/elixir/list_test.exs | 7 ------- 3 files changed, 5 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f39666540a8..235f36f6fdc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,10 @@ This release no longer supports WERL (a graphical user interface for the Erlang ### 3. Soft deprecations (no warnings emitted) +#### Elixir + + * [List] `List.zip/1` is deprecated in favor of `Enum.zip/1` + ### 4. Hard deprecations #### EEx diff --git a/lib/elixir/lib/list.ex b/lib/elixir/lib/list.ex index a8155deed40..8c5a89f262e 100644 --- a/lib/elixir/lib/list.ex +++ b/lib/elixir/lib/list.ex @@ -633,21 +633,7 @@ defmodule List do [other] end - @doc """ - Zips corresponding elements from each list in `list_of_lists`. - - The zipping finishes as soon as any list terminates. - - ## Examples - - iex> List.zip([[1, 2], [3, 4], [5, 6]]) - [{1, 3, 5}, {2, 4, 6}] - - iex> List.zip([[1, 2], [3], [5, 6]]) - [{1, 3, 5}] - - """ - @spec zip([list]) :: [tuple] + @doc deprecated: "Use Enum.zip/1 instead" def zip([]), do: [] def zip(list_of_lists) when is_list(list_of_lists) do diff --git a/lib/elixir/test/elixir/list_test.exs b/lib/elixir/test/elixir/list_test.exs index ee02a072a1b..368e0d6b9ea 100644 --- a/lib/elixir/test/elixir/list_test.exs +++ b/lib/elixir/test/elixir/list_test.exs @@ -84,13 +84,6 @@ defmodule ListTest do assert List.last([1, 2, 3]) == 3 end - test "zip/1" do - assert List.zip([[1, 4], [2, 5], [3, 6]]) == [{1, 2, 3}, {4, 5, 6}] - assert List.zip([[1, 4], [2, 5, 0], [3, 6]]) == [{1, 2, 3}, {4, 5, 6}] - assert List.zip([[1], [2, 5], [3, 6]]) == [{1, 2, 3}] - assert List.zip([[1, 4], [2, 5], []]) == [] - end - test "keyfind/4" do assert List.keyfind([a: 1, b: 2], :a, 0) == {:a, 1} assert List.keyfind([a: 1, b: 2], 2, 1) == {:b, 2} From 208f5c2bf43dfd7b897cc9ba4d4e8a5df31187ba Mon Sep 17 00:00:00 2001 From: sabiwara Date: Fri, 9 Aug 2024 07:53:03 +0900 Subject: [PATCH 2/2] Delegate implementation to Enum.zip/1 --- lib/elixir/lib/list.ex | 33 +-------------------------------- 1 file changed, 1 insertion(+), 32 deletions(-) diff --git a/lib/elixir/lib/list.ex b/lib/elixir/lib/list.ex index 8c5a89f262e..cc396213ef5 100644 --- a/lib/elixir/lib/list.ex +++ b/lib/elixir/lib/list.ex @@ -634,10 +634,8 @@ defmodule List do end @doc deprecated: "Use Enum.zip/1 instead" - def zip([]), do: [] - def zip(list_of_lists) when is_list(list_of_lists) do - do_zip(list_of_lists, []) + Enum.zip(list_of_lists) end @doc ~S""" @@ -1358,33 +1356,4 @@ defmodule List do defp do_pop_at([head | tail], index, default, acc) do do_pop_at(tail, index - 1, default, [head | acc]) end - - # zip - - defp do_zip(list, acc) do - converter = fn x, acc -> do_zip_each(to_list(x), acc) end - - case :lists.mapfoldl(converter, [], list) do - {_, nil} -> - :lists.reverse(acc) - - {mlist, heads} -> - do_zip(mlist, [to_tuple(:lists.reverse(heads)) | acc]) - end - end - - defp do_zip_each(_, nil) do - {nil, nil} - end - - defp do_zip_each([head | tail], acc) do - {tail, [head | acc]} - end - - defp do_zip_each([], _) do - {nil, nil} - end - - defp to_list(tuple) when is_tuple(tuple), do: Tuple.to_list(tuple) - defp to_list(list) when is_list(list), do: list end