Skip to content

Fix zipping on cycle stream #14307

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion lib/elixir/lib/stream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,7 @@ defmodule Stream do
defp check_cycle_first_element(reduce) do
fn acc ->
case reduce.(acc) do
{state, []} when state in [:done, :halted] ->
{:done, []} ->
raise ArgumentError, "cannot cycle over an empty enumerable"

other ->
Expand Down
12 changes: 12 additions & 0 deletions lib/elixir/test/elixir/enum_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,18 @@ defmodule EnumTest do
assert Enum.zip([], []) == []
end

test "zip/2 with infinite streams" do
assert Enum.zip([], Stream.cycle([1, 2])) == []
assert Enum.zip([], Stream.cycle(1..2)) == []
assert Enum.zip(.., Stream.cycle([1, 2])) == []
assert Enum.zip(.., Stream.cycle(1..2)) == []

assert Enum.zip(Stream.cycle([1, 2]), ..) == []
assert Enum.zip(Stream.cycle(1..2), ..) == []
assert Enum.zip(Stream.cycle([1, 2]), ..) == []
assert Enum.zip(Stream.cycle(1..2), ..) == []
end

test "zip/1" do
assert Enum.zip([[:a, :b], [1, 2], ["foo", "bar"]]) == [{:a, 1, "foo"}, {:b, 2, "bar"}]

Expand Down
4 changes: 4 additions & 0 deletions lib/elixir/test/elixir/stream_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ defmodule StreamTest do
Stream.cycle(%{}) |> Enum.to_list()
end

assert_raise ArgumentError, "cannot cycle over an empty enumerable", fn ->
Stream.cycle(..) |> Enum.to_list()
end

assert Stream.cycle([1, 2, 3]) |> Stream.take(5) |> Enum.to_list() == [1, 2, 3, 1, 2]
assert Enum.take(stream, 5) == [1, 2, 3, 1, 2]
end
Expand Down
Loading