Skip to content

Commit e81e9f7

Browse files
sabiwarajosevalim
authored andcommitted
Fix bug: Enum.slice selecting extra element (#12043)
1 parent db7c28f commit e81e9f7

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

lib/elixir/lib/enum.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4436,7 +4436,10 @@ defmodule Enum do
44364436
end
44374437

44384438
entry, {start, amount, to_drop, list} ->
4439-
{:halt, {start, amount, to_drop, [entry | list]}}
4439+
case to_drop do
4440+
1 -> {:halt, {start, amount, to_drop, [entry | list]}}
4441+
_ -> {:halt, {start, amount, to_drop, list}}
4442+
end
44404443
end)
44414444

44424445
:lists.reverse(slice)

lib/elixir/test/elixir/enum_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,7 @@ defmodule EnumTest do
10771077
assert [1, 2, 3] |> Stream.cycle() |> Enum.slice(0..1) == [1, 2]
10781078
assert [1, 2, 3] |> Stream.cycle() |> Enum.slice(0..4) == [1, 2, 3, 1, 2]
10791079
assert [1, 2, 3] |> Stream.cycle() |> Enum.slice(0..4//2) == [1, 3, 2]
1080+
assert [1, 2, 3] |> Stream.cycle() |> Enum.slice(0..5//2) == [1, 3, 2]
10801081
end
10811082

10821083
test "slice on pruned infinite streams" do

0 commit comments

Comments
 (0)