Skip to content

Commit

Permalink
Do not raise when Stream.cycle is explicitly halted, closes #14307
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Mar 3, 2025
1 parent c63aeb9 commit 8493f19
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/elixir/lib/stream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1444,7 +1444,7 @@ defmodule Stream do
defp check_cycle_first_element(reduce) do
fn acc ->
case reduce.(acc) do
{state, []} when state in [:done, :halted] ->
{state, []} when state in [:done, :halted] and elem(acc, 0) != :halt ->
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 @@ -1517,6 +1517,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

0 comments on commit 8493f19

Please sign in to comment.