Skip to content
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

Implement Enum.member? #992

Merged
merged 1 commit into from
Apr 29, 2013
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
36 changes: 36 additions & 0 deletions lib/elixir/lib/enum.ex
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,28 @@ defmodule Enum do
end
end

@doc """
Checks if the given `term` is included in the `collection`.

## Examples

iex> Enum.member?(1 .. 3, 1)
true
iex> Enum.member?([1, 2, 3, 4], 10)
false

"""
@spec member?(t, term) :: boolean
def member?(collection, term) do
case I.iterator(collection) do
{ iterator, pointer } ->
do_member?(pointer, iterator, term)

list when is_list(list) ->
List.member?(list, term)
end
end

@doc """
Partitions `collection` into two where the first one contains elements
for which `fun` returns a truthy value, and the second one -- for which `fun`
Expand Down Expand Up @@ -1549,6 +1571,20 @@ defmodule Enum do
{ :lists.reverse(list_acc), acc }
end

## member?

def do_member?(:stop, _, _) do
Copy link

Choose a reason for hiding this comment

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

Shouldn't these funcs be defined as private (defp) like the others?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, Please send a PR of they are still public!

On Wednesday, March 2, 2016, Oskar Boethius Lissheim <
[email protected]> wrote:

In lib/elixir/lib/enum.ex
#992 (comment):

@@ -1549,6 +1571,20 @@ defmodule Enum do
{ :lists.reverse(list_acc), acc }
end

  • member?

  • def do_member?(:stop, _, _) do

Shouldn't these funcs be defined as private (defp) like the others?


Reply to this email directly or view it on GitHub
https://github.com/elixir-lang/elixir/pull/992/files#r54798954.

José Valimwww.plataformatec.com.br
http://www.plataformatec.com.br/Founder and Director of R&D

false
end

def do_member?({ h, _ }, _, term) when h == term do
true
end

def do_member?({ h, next }, iterator, term) do
do_member?(iterator.(next), iterator, term)
end

## partition

defp do_partition([h|t], fun, acc1, acc2) do
Expand Down
6 changes: 6 additions & 0 deletions lib/elixir/test/elixir/enum_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ defmodule EnumTest.List do
assert Enum.map_reduce([1,2,3], 1, fn(x, acc) -> { x * 2, x + acc } end) == { [2,4,6], 7 }
end

test :member? do
refute Enum.member?([], 0)
assert Enum.member?([1, 2, 3], 2)
assert Enum.member?(1 .. 3, 1)
end

test :partition do
assert Enum.partition([1,2,3], fn(x) -> rem(x, 2) == 0 end) == { [2], [1,3] }
assert Enum.partition([2,4,6], fn(x) -> rem(x, 2) == 0 end) == { [2,4,6], [] }
Expand Down