Skip to content

Enum.zip([], Stream.cycle(1..10)) returns error #14306

Closed
@pejrich

Description

@pejrich

Elixir and Erlang/OTP versions

OTP 27.2.1
Elixir 1.18.2-otp-27

Operating system

macOS

Current behavior

Not sure if this is intended but it seems a bit inconsistent to me.

# zip works with empty lists, as well as cycling over a list
iex> Enum.zip([], [])
[]
iex> Enum.zip([], Stream.cycle([1,2,3,4,5]))
[]
# The output of cycling a list, and cycling a range is as expected, equal.
iex> Stream.cycle([1,2,3,4,5]) |> Enum.take(10)
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
iex> Stream.cycle(1..5) |> Enum.take(10)
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
# But zipping an empty list against a cycle over a range throws an error
iex> Enum.zip([], Stream.cycle(1..5))
** (ArgumentError) cannot cycle over an empty enumerable
    (elixir 1.18.2) lib/stream.ex:1448: anonymous fn/2 in Stream.check_cycle_first_element/1
    (elixir 1.18.2) lib/stream.ex:1419: Stream.do_cycle/5
    (stdlib 6.2) lists.erl:2310: :lists.foreach_1/2
    (elixir 1.18.2) lib/stream/reducers.ex:74: Stream.Reducers.do_zip_enum/4
    (elixir 1.18.2) lib/enum.ex:4174: Enum.zip_reduce/3
    (elixir 1.18.2) lib/enum.ex:4028: Enum.zip/1
    iex:57: (file)

Expected behavior

Intuitively, based on the other results, I would expect the last one to return [] too, rather than throw an error, since I'm never actually needing to cycle that stream, and it's a valid stream to cycle, so I'm unsure why it would throw an error, so I'm not sure if this is a bug or intended.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions