elixir-queue icon indicating copy to clipboard operation
elixir-queue copied to clipboard

Make Qex.split/2 match the behavior of Enum.split/2 rather than :queue.split/2

Open s3cur3 opened this issue 2 years ago • 1 comments

This changes the behavior of Qex.split/2 to be less surprising to Elixir devs.

When you ask for more elements than your queue currently contains, it will silently return the complete queue as the first item in the tuple, rather than throwing an ArgumentError like :queue.split/2 does when you ask for too many items.

Thus, instead of matching this behavior:

iex(1)> :queue.split(:queue.new(), 3)
** (ArgumentError) argument error
    (stdlib 3.14.1) queue.erl:336: :queue.split({[], []}, 3)

...it behaves like this:

iex(2)> Enum.split(Qex.new(), 3)
{[], []}

...giving you:

iex(3)> Qex.split(Qex.new(), 3)
{#Qex<[]>, #Qex<[]>}

This is a potentially breaking change, in the sense that somebody could conceivably be depending on an ArgumentError to tell them they asked for more than :queue.len/1 elements. (One alternative might be to call this version safe_split/2 or something and leave the existing split/2 behavior in place.) I suspect there are a great many more folks new to working with queues on the BEAM who are bitten by this unexpectedly raising an error, though.

This might controversial, so again, no hard feelings if you don't want it. ☺️

s3cur3 avatar Nov 01 '21 14:11 s3cur3