Skip to content

Commit 71da1a2

Browse files
committed
Do not raise when Stream.cycle is explicitly halted, closes #14307
1 parent ba43db7 commit 71da1a2

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/elixir/lib/stream.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -1447,7 +1447,7 @@ defmodule Stream do
14471447
defp check_cycle_first_element(reduce) do
14481448
fn acc ->
14491449
case reduce.(acc) do
1450-
{state, []} when state in [:done, :halted] ->
1450+
{state, []} when state in [:done, :halted] and elem(acc, 0) != :halt ->
14511451
raise ArgumentError, "cannot cycle over an empty enumerable"
14521452

14531453
other ->

lib/elixir/test/elixir/enum_test.exs

+12
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,18 @@ defmodule EnumTest do
15211521
assert Enum.zip([], []) == []
15221522
end
15231523

1524+
test "zip/2 with infinite streams" do
1525+
assert Enum.zip([], Stream.cycle([1, 2])) == []
1526+
assert Enum.zip([], Stream.cycle(1..2)) == []
1527+
assert Enum.zip(.., Stream.cycle([1, 2])) == []
1528+
assert Enum.zip(.., Stream.cycle(1..2)) == []
1529+
1530+
assert Enum.zip(Stream.cycle([1, 2]), ..) == []
1531+
assert Enum.zip(Stream.cycle(1..2), ..) == []
1532+
assert Enum.zip(Stream.cycle([1, 2]), ..) == []
1533+
assert Enum.zip(Stream.cycle(1..2), ..) == []
1534+
end
1535+
15241536
test "zip/1" do
15251537
assert Enum.zip([[:a, :b], [1, 2], ["foo", "bar"]]) == [{:a, 1, "foo"}, {:b, 2, "bar"}]
15261538

0 commit comments

Comments
 (0)