Skip to content

Files

Latest commit

 

History

History
196 lines (140 loc) · 6.11 KB

custom_enum_with_recursion.livemd

File metadata and controls

196 lines (140 loc) · 6.11 KB

Custom Enum With Recursion

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.8.0", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"},
  {:benchee, "~> 1.1"}
])

Navigation

Return Home Report An Issue

Custom 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.

Mark As Completed

file_name = Path.basename(Regex.replace(~r/#.+/, __ENV__.file, ""), ".livemd")

progress_path = __DIR__ <> "/../progress.json"
existing_progress = File.read!(progress_path) |> Jason.decode!()

default = Map.get(existing_progress, file_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, file_name, completed)))
  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 solutions
$ git checkout -b custom-enum-with-recursion-exercise
$ git add .
$ git commit -m "finish custom enum with recursion exercise"
$ git push origin custom-enum-with-recursion-exercise

Create a pull request from your custom-enum-with-recursion-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
Games: Benchmarking Streams