Skip to content

Commit 8493f19

Browse files
committed
Do not raise when Stream.cycle is explicitly halted, closes #14307
1 parent c63aeb9 commit 8493f19

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
@@ -1444,7 +1444,7 @@ defmodule Stream do
14441444
defp check_cycle_first_element(reduce) do
14451445
fn acc ->
14461446
case reduce.(acc) do
1447-
{state, []} when state in [:done, :halted] ->
1447+
{state, []} when state in [:done, :halted] and elem(acc, 0) != :halt ->
14481448
raise ArgumentError, "cannot cycle over an empty enumerable"
14491449

14501450
other ->

lib/elixir/test/elixir/enum_test.exs

+12
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,18 @@ defmodule EnumTest do
15171517
assert Enum.zip([], []) == []
15181518
end
15191519

1520+
test "zip/2 with infinite streams" do
1521+
assert Enum.zip([], Stream.cycle([1, 2])) == []
1522+
assert Enum.zip([], Stream.cycle(1..2)) == []
1523+
assert Enum.zip(.., Stream.cycle([1, 2])) == []
1524+
assert Enum.zip(.., Stream.cycle(1..2)) == []
1525+
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+
assert Enum.zip(Stream.cycle(1..2), ..) == []
1530+
end
1531+
15201532
test "zip/1" do
15211533
assert Enum.zip([[:a, :b], [1, 2], ["foo", "bar"]]) == [{:a, 1, "foo"}, {:b, 2, "bar"}]
15221534

0 commit comments

Comments
 (0)