Custom Enum With Recursion
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.9", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"},
{:benchee, "~> 1.1"}
])
Navigation
Home Report An Issue BenchmarkingEcto ChangesetsCustom Enum With Recursion
We’re going to use recursion to re-implement several of the Enum module’s functions. For this exercise you are not allowed to use the Enum module.
Implement the following Enum functions in this CustomEnum
function. Each should use recursion
to accomplish the same functionality as the Enum module does.
Keep in mind you may need to delegate to another function if an accumulator is required.
For example,
defp recursive_map(list, function, acc) do
# ...
end
def map(list, function) do
recursive_map(list, function, [])
end
You only need to handle lists, not all collections.
defmodule CustomEnum do
@moduledoc """
Documentation for `CustomEnum`.
Re-implement common [Enum](https://hexdocs.pm/elixir/Enum.html) functions using recursion.
"""
@doc """
Reverse a list
## Examples
iex> CustomEnum.reverse([1, 2, 3])
[3, 2, 1]
iex> CustomEnum.reverse([4, 5, 6, 7])
[7, 6, 5, 4]
"""
def reverse(list) do
end
@doc """
Map over a list
## Examples
iex> CustomEnum.map([1, 2, 3], fn integer -> integer * 2 end)
[2, 4, 6]
iex> CustomEnum.map([5, 4, 3], fn integer -> is_integer(integer) end)
[true, true, true]
"""
def map(list, callback_function) do
end
@doc """
Filter elements in a list. Keep elements that return `true` when called with the
provided callback function.
## Examples
iex> CustomEnum.filter([1, 2, "3"], fn int -> is_integer(int) end)
[1, 2]
iex> CustomEnum.filter([1, "2", "3"], fn char -> is_bitstring(char) end)
["2", "3"]
"""
def filter(list, callback_function) do
end
@doc """
Sum a list of integers.
## Examples
iex> CustomEnum.sum([1, 2, 3])
6
iex> CustomEnum.sum([1, 1, 1])
3
"""
def sum(list_of_integers) do
end
@doc """
Join a list of strings together.
## Examples
iex> CustomEnum.join(["A", "B", "C"])
"ABC"
iex> CustomEnum.join(["Hello", ",", " ", "World", "!"])
"Hello, World!"
"""
def join(list_of_strings) do
end
end
Benchmarking
We’ve installed the Benchee project in this livebook.
Benchmark your solution against the existing Enum module. Is yours faster or slower? Keep in mind that the Enum module contains implementations for all enumerables, not just lists.
Commit Your Progress
DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.
Run git status
to ensure there are no undesirable changes.
Then run the following in your command line from the curriculum
folder to commit your progress.
$ git add .
$ git commit -m "finish Custom Enum With Recursion exercise"
$ git push
We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.
We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.