Skip to content

Commit 38a9513

Browse files
author
José Valim
committed
Fix boundary in Enum.take/2, closes #3042
Signed-off-by: José Valim <[email protected]>
1 parent 578f798 commit 38a9513

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

lib/elixir/lib/enum.ex

+4-4
Original file line numberDiff line numberDiff line change
@@ -1738,10 +1738,10 @@ defmodule Enum do
17381738
def take(collection, count) when count > 0 do
17391739
{_, {res, _}} =
17401740
Enumerable.reduce(collection, {:cont, {[], count}}, fn(entry, {list, count}) ->
1741-
if count > 1 do
1742-
{:cont, {[entry|list], count - 1}}
1743-
else
1744-
{:halt, {[entry|list], count}}
1741+
case count do
1742+
0 -> {:halt, {list, count}}
1743+
1 -> {:halt, {[entry|list], count - 1}}
1744+
_ -> {:cont, {[entry|list], count - 1}}
17451745
end
17461746
end)
17471747
:lists.reverse(res)

lib/elixir/test/elixir/stream_test.exs

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ defmodule StreamTest do
8888
[[1], [2, 2], [3], [4, 4, 6], [7, 7]]
8989
assert stream |> Stream.take(3) |> Enum.to_list ==
9090
[[1], [2, 2], [3]]
91+
assert 1..10 |> Stream.chunk(2) |> Enum.take(2) ==
92+
[[1, 2], [3, 4]]
9193
end
9294

9395
test "chunk_by/2 is zippable" do

0 commit comments

Comments
 (0)