Skip to content

Fix bug: Enum.slice selecting extra element #12043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2022

Conversation

sabiwara
Copy link
Contributor

@sabiwara sabiwara commented Aug 2, 2022

I found one last bug in Enum.slice with stepped ranges on streams, which might be including an extra element:

iex(3)>  1..10 |> Stream.take(10) |> Enum.slice(2..5//2)
[3, 5, 6]
iex(4)>  1..10 |> Enum.take(10) |> Enum.slice(2..5//2)
[3, 5]

Copy link
Member

@whatyouhide whatyouhide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, good catch!

Comment on lines +4439 to +4442
case to_drop do
1 -> {:halt, {start, amount, to_drop, [entry | list]}}
_ -> {:halt, {start, amount, to_drop, list}}
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My 2c:

Suggested change
case to_drop do
1 -> {:halt, {start, amount, to_drop, [entry | list]}}
_ -> {:halt, {start, amount, to_drop, list}}
end
if to_drop == 1 do
{:halt, {start, amount, to_drop, [entry | list]}}
else
{:halt, {start, amount, to_drop, list}}
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the comment. The idea here was to keep consistency with the code just above:

case to_drop do
   1 -> {:cont, {start, amount - 1, step, [entry | list]}}
   _ -> {:cont, {start, amount - 1, to_drop - 1, list}}     
end

Do you think both should be changed? Or is there a performance implication behind the original code?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Performance is the same. Both snippets are fine, so let’s keep it as is. :)

@josevalim josevalim merged commit a15e548 into elixir-lang:main Aug 2, 2022
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

@sabiwara sabiwara deleted the bug-slice-step2 branch August 3, 2022 00:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants