Skip to content
Merged
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
8 changes: 8 additions & 0 deletions lib/elixir/lib/enum.ex
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,10 @@ defmodule Enum do
end
end

def count_until(_enumerable, limit) do
raise ArgumentError, "expected limit to be greater than 0, got: #{limit}"
end

@doc """
Counts the elements in the enumerable for which `fun` returns a truthy value, stopping at `limit`.

Expand All @@ -801,6 +805,10 @@ defmodule Enum do
end
end

def count_until(_enumerable, _fun, limit) do
raise ArgumentError, "expected limit to be greater than 0, got: #{limit}"
end

@doc """
Enumerates the `enumerable`, returning a list where all consecutive
duplicate elements are collapsed to a single element.
Expand Down
20 changes: 20 additions & 0 deletions lib/elixir/test/elixir/enum_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ defmodule EnumTest do
assert count_until_stream.([1, 2], 2) == 2
end

test "count_until/2 with invalid limit" do
assert_raise ArgumentError, "expected limit to be greater than 0, got: 0", fn ->
Enum.count_until([1, 2, 3], 0)
end

assert_raise ArgumentError, "expected limit to be greater than 0, got: -22", fn ->
Enum.count_until([1, 2, 3], -22)
end
end

test "count_until/3" do
assert Enum.count_until([1, 2, 3, 4, 5, 6], fn x -> rem(x, 2) == 0 end, 2) == 2
assert Enum.count_until([1, 2], fn x -> rem(x, 2) == 0 end, 2) == 1
Expand All @@ -243,6 +253,16 @@ defmodule EnumTest do
assert count_until_stream.([], fn x -> rem(x, 2) == 0 end, 2) == 0
end

test "count_until/3 with invalid limit" do
assert_raise ArgumentError, "expected limit to be greater than 0, got: 0", fn ->
Enum.count_until([1, 2, 3], fn x -> rem(x, 2) == 0 end, 0)
end

assert_raise ArgumentError, "expected limit to be greater than 0, got: -22", fn ->
Enum.count_until([1, 2, 3], fn x -> rem(x, 2) == 0 end, -22)
end
end

test "dedup/1" do
assert Enum.dedup([1, 1, 2, 1, 1, 2, 1]) == [1, 2, 1, 2, 1]
assert Enum.dedup([2, 1, 1, 2, 1]) == [2, 1, 2, 1]
Expand Down
Loading