Skip to content

Soft-deprecate List.zip/1 in favor of Enum.zip/1 #13767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 2 additions & 47 deletions lib/elixir/lib/list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -633,25 +633,9 @@ 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]
def zip([]), do: []

@doc deprecated: "Use Enum.zip/1 instead"
Copy link
Contributor

@v0idpwn v0idpwn Aug 8, 2024

Choose a reason for hiding this comment

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

Deprecated or not, I think it could be nice to delete the existing implementation and delegate to Enum.zip/1 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, I was hesitant since it will now support a list of enumerables and not just a list of lists... but that's not breaking, will go for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in 208f5c2

Copy link
Contributor

Choose a reason for hiding this comment

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

That's a good point, maybe it wasn't a great idea. Let's see what people think

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"""
Expand Down Expand Up @@ -1372,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
7 changes: 0 additions & 7 deletions lib/elixir/test/elixir/list_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
Loading