Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/elixir/lib/stream.ex
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ defmodule Stream do
{:cont, acc}
end

defp do_chunk_by(acc({_, 1}, _, _) = acc, _f1) do
Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately this fix is not correct. We cannot access the accumulator state because the take value can be nested inside other states or it could just mean whatever. :)

{:cont, acc}
end

defp do_chunk_by(acc(h, {buffer, _}, t), f1) do
cont_with_acc(f1, :lists.reverse(buffer), h, nil, t)
end
Expand Down
27 changes: 16 additions & 11 deletions lib/elixir/test/elixir/stream_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ defmodule StreamTest do
end

test "chunk/2, chunk/3 and chunk/4" do
assert Stream.chunk([1, 2, 3, 4, 5], 2) |> Enum.to_list ==
[[1, 2], [3, 4]]
assert Stream.chunk([1, 2, 3, 4, 5], 2) |> Enum.to_list == [[1, 2], [3, 4]]
assert Stream.chunk([1, 2, 3, 4, 5], 2, 2, [6]) |> Enum.to_list ==
[[1, 2], [3, 4], [5, 6]]
assert Stream.chunk([1, 2, 3, 4, 5, 6], 3, 2) |> Enum.to_list ==
Expand All @@ -55,6 +54,7 @@ defmodule StreamTest do
[[1, 2, 3], [4, 5, 6]]
assert Stream.chunk([1, 2, 3, 4, 5], 4, 4, 6..10) |> Enum.to_list ==
[[1, 2, 3, 4], [5, 6, 7, 8]]
assert 1..10 |> Stream.chunk(2) |> Enum.take(2) == [[1, 2], [3, 4]]
end

test "chunk/4 is zippable" do
Expand All @@ -80,18 +80,23 @@ defmodule StreamTest do
stream = Stream.chunk_by([1, 2, 2, 3, 4, 4, 6, 7, 7], &(rem(&1, 2) == 1))

assert is_lazy(stream)
assert Enum.to_list(stream) ==
[[1], [2, 2], [3], [4, 4, 6], [7, 7]]
assert stream |> Stream.take(3) |> Enum.to_list ==
[[1], [2, 2], [3]]
assert Enum.to_list(stream) == [[1], [2, 2], [3], [4, 4, 6], [7, 7]]
Copy link
Member

Choose a reason for hiding this comment

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

Please avoid changing style of other assertions as it makes it hard to see what is being changed as part of this change. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry about that, I just went through each test and add take assertion where it missed, to prevent such things in future. Below I added line 85.

assert stream |> Stream.take(3) |> Enum.to_list == [[1], [2, 2], [3]]
assert stream |> Enum.take(2) == [[1], [2, 2]]
end

test "chunk_by/2 is zippable" do
stream = Stream.chunk_by([1, 2, 2, 3], &(rem(&1, 2) == 1))
list = Enum.to_list(stream)

assert Enum.zip(list, list) == Enum.zip(stream, stream)
end

test "chunk_by/4 is haltable" do
assert 1..10 |> Stream.take(6) |> Stream.chunk_by(&(rem(&1, 3) == 0))
|> Stream.take(2) |> Enum.to_list == [[1, 2], [3]]
end

test "concat/1" do
stream = Stream.concat([1..3, [], [4, 5, 6], [], 7..9])
assert is_function(stream)
Expand Down Expand Up @@ -527,6 +532,7 @@ defmodule StreamTest do
stream = Stream.scan(1..5, &(&1 + &2))
assert is_lazy(stream)
assert Enum.to_list(stream) == [1,3,6,10,15]
assert Enum.take(stream, 3) == [1,3,6]
assert Stream.scan([], &(&1 + &2)) |> Enum.to_list == []
end

Expand Down Expand Up @@ -650,11 +656,10 @@ defmodule StreamTest do
end

test "uniq/1" do
assert Stream.uniq([1, 2, 3, 2, 1]) |> Enum.to_list ==
[1, 2, 3]

assert Stream.uniq([{1, :x}, {2, :y}, {1, :z}], fn {x, _} -> x end) |> Enum.to_list ==
[{1,:x}, {2,:y}]
assert Stream.uniq([1, 2, 3, 2, 1]) |> Enum.to_list == [1, 2, 3]
assert Stream.uniq([1, 2, 3, 2, 1]) |> Enum.take(2) == [1, 2]
assert Stream.uniq([{1, :x}, {2, :y}, {1, :z}], fn {x, _} -> x end)
|> Enum.to_list == [{1,:x}, {2,:y}]
end

test "zip/2" do
Expand Down