Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Custom Enum With Recursion

custom_enum_with_recursion.livemd

Custom Enum With Recursion

Mix.install([
  {:kino, github: "livebook-dev/kino", override: true},
  {:kino_lab, "~> 0.1.0-dev", github: "jonatanklosko/kino_lab"},
  {:vega_lite, "~> 0.1.4"},
  {:kino_vega_lite, "~> 0.1.1"},
  {:benchee, "~> 0.1"},
  {:ecto, "~> 3.7"},
  {:math, "~> 0.7.0"},
  {:faker, "~> 0.17.0"},
  {:utils, path: "#{__DIR__}/../utils"},
  {:tested_cell, git: "https://github.com/BrooklinJazz/tested_cell"}
])

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.

  • reverse/2
  • each/2
  • map/2
  • filter/2
  • sum/2

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
  def map(list, function) do
  end

  def reverse(list) do
  end

  def reduce(list, acc, fun) do
  end

  def each(list, function) do
  end

  def filter(list, function) do
  end

  def sum(list, function) do
  end
end

Utils.feedback(:enum_recursion, CustomEnum)

Commit Your Progress

Run the following in your command line from the project folder to track and save your progress in a Git commit.

$ git add .
$ git commit -m "finish custom enum with recursion exercise"