Skip to content

Commit

Permalink
adds more examples #21
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcarlisle committed Feb 10, 2017
1 parent 9bcd303 commit ccb00fd
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
33 changes: 31 additions & 2 deletions README.md
Expand Up @@ -550,18 +550,47 @@ find out if our zoo contains an animal:
```elixir
@doc """
contains? takes a list of zoo animals and a single animal and returns a boolean
as to whether or not the list contains the given animal
as to whether or not the list contains the given animal.
## Examples
iex> zoo = Animals.create_zoo
iex> Animals.contains(zoo, "gorilla")
iex> Animals.contains?(zoo, "gorilla")
true
"""
def contains?(zoo, animal) do
Enum.member?(zoo, animal)
end
```
**NOTE: It's convention when writing a function that returns a boolean to add a question
mark after the name of the method.**

6. Pattern matching example: Let's create a function that takes a list of animals and
the number of animals you've seen and then returns the list of animals you have
still to see.

```elixir
@doc """
remaining takes a list of zoo animals and the number of animals that
have already been viewed and returns a list of animals that you have left
to see
## Examples
iex> zoo = Animals.create_zoo
iex> Animals.remaining(zoo, 2)
["gorilla", "elephant", "monkey", "giraffe"]
"""
def remaining(zoo, count) do
# Enum.split returns a tuple so we have to pattern match on the result
# to get the value we want out
# The underscore before the "seen" means that we don't care about the value
# If this was left out then elixir would give a warning saying "seen" has
# not been used
{_seen, to_see} = Enum.split(zoo, count)
to_see
end
```

## Resources

Expand Down
24 changes: 22 additions & 2 deletions examples/animals/animals.ex
Expand Up @@ -32,15 +32,35 @@ defmodule Animals do

@doc """
contains? takes a list of zoo animals and a single animal and returns a boolean
as to whether or not the list contains the given animal
as to whether or not the list contains the given animal.
## Examples
iex> zoo = Animals.create_zoo
iex> Animals.contains(zoo, "gorilla")
iex> Animals.contains?(zoo, "gorilla")
true
"""
def contains?(zoo, animal) do
Enum.member?(zoo, animal)
end



@doc """
remaining takes a list of zoo animals and the number of animals that
have already been viewed and returns a list of animals that you have left
to see
## Examples
iex> zoo = Animals.create_zoo
iex> Animals.remaining(zoo, 2)
["gorilla", "elephant", "monkey", "giraffe"]
"""
def remaining(zoo, count) do
# Enum.split returns a tuple so we have to pattern match on the result
# to get the value we want out
{_seen, to_see} = Enum.split(zoo, count)
to_see
end
end

0 comments on commit ccb00fd

Please sign in to comment.