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

Custom Enum With Reduce

exercises/custom_enum_with_reduce.livemd

Custom Enum With Reduce

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

Navigation

Home Report An Issue Number FinderData Traversal

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

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
    Enum.reduce(list, [], fn int, acc -> [int | acc] end)
  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
    Enum.reduce(list, [], fn each, acc ->
      [callback_function.(each) | acc]
    end)
    |> reverse()
  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
    list
    |> Enum.reduce([], fn each, acc ->
      if callback_function.(each) do
        [each | acc]
      else
        acc
      end
    end)
    |> reverse()
  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
    Enum.reduce(list_of_integers, fn each, acc -> each + acc end)
  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
    Enum.reduce(list_of_strings, fn each, acc -> acc <> each end)
  end
end

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

Navigation

Home Report An Issue Number FinderData Traversal