Custom Enum With Reduce
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.8.0", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
Return Home<span style=”padding: 0 30px”></span> Report An Issue
Custom Enum With Reduce
You’re going to use Enum.reduce/2 and/or Enum.reduce/3 to re-implement several of the Enum module’s functions. Only implement these functions for lists.
Example Solution ```elixir defmodule CustomEnum do def reverse(list) do Enum.reduce(list, [], fn each, acc -> [each | acc] end) end def map(list, callback_function) do Enum.reduce(list, [], fn each, acc -> [callback_function.(each) | acc] end) |> Enum.reverse() end def filter(list, callback_function) do Enum.reduce(list, [], fn each, acc -> if callback_function.(each) do [each | acc] else acc end end) |> Enum.reverse() end def sum(list) do Enum.reduce(list, 0, fn each, acc -> acc + each end) end def join(list_of_strings) do Enum.reduce(list_of_strings, "", fn str, acc -> acc <> str end) end end ```
Implement the CustomEnum module according to the documentation below.
defmodule CustomEnum do
@moduledoc """
Documentation for `CustomEnum`.
Re-implement common [Enum](https://hexdocs.pm/elixir/Enum.html) functions using [Enum.reduce/2](https://hexdocs.pm/elixir/Enum.html#reduce/2) or [Enum.reduce/3](https://hexdocs.pm/elixir/Enum.html#reduce/3).
"""
@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
Mark As Completed
file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")
save_name =
case Path.basename(__DIR__) do
"reading" -> "custom_enum_with_reduce_reading"
"exercises" -> "custom_enum_with_reduce_exercise"
end
progress_path = __DIR__ <> "/../progress.json"
existing_progress = File.read!(progress_path) |> Jason.decode!()
default = Map.get(existing_progress, save_name, false)
form =
Kino.Control.form(
[
completed: input = Kino.Input.checkbox("Mark As Completed", default: default)
],
report_changes: true
)
Task.async(fn ->
for %{data: %{completed: completed}} <- Kino.Control.stream(form) do
File.write!(
progress_path,
Jason.encode!(Map.put(existing_progress, save_name, completed), pretty: true)
)
end
end)
form
Commit Your Progress
Run the following in your command line from the curriculum folder to track and save your progress in a Git commit.
Ensure that you do not already have undesired or unrelated changes by running git status or by checking the source control tab in Visual Studio Code.
$ git checkout -b custom-enum-with-reduce-exercise
$ git add .
$ git commit -m "finish custom enum with reduce exercise"
$ git push origin custom-enum-with-reduce-exercise
Create a pull request from your custom-enum-with-reduce-exercise branch to your solutions branch.
Please do not create a pull request to the DockYard Academy repository as this will spam our PR tracker.
DockYard Academy Students Only:
Notify your teacher by including @BrooklinJazz in your PR description to get feedback.
You (or your teacher) may merge your PR into your solutions branch after review.
If you are interested in joining the next academy cohort, sign up here to receive more news when it is available.
Up Next
| Previous | Next |
|---|---|
| Weighted Voting | Dates and Times |