-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Fix Enum.take for Stream.chunk_by #3042
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 == | ||
|
@@ -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 | ||
|
@@ -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]] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry about that, I just went through each test and add |
||
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) | ||
|
@@ -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 | ||
|
||
|
@@ -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 | ||
|
There was a problem hiding this comment.
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. :)