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

Filter Values By Type

exercises/filter_values_by_type.livemd

Filter Values By Type

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 Built-In Elixir ModulesTic-tac-toe

Filter Values By Type

Given a list of random items that could be any type, you are going to find all of the items that match a particular type.

Filter.floats([1, 2, 3, 4.0])
[4.0]

Filter.integers([1, 2, 3, 4.0])
[1, 2, 3]

Filter.maps(["hello", 4.0, %{key: "value"}, %{}])
[%{key: "value"}, %{}]

Implement the Filter module as documented.

Example Solution

defmodule Filter do
  def integers(list) do
    Enum.filter(list, &is_integer/1)
  end

  def floats(list) do
    Enum.filter(list, &is_float/1)
  end

  def numbers(list) do
    Enum.filter(list, fn number -> is_number(number) end)
  end

  def atoms(list) do
    Enum.filter(list, &is_atom/1)
  end

  def lists(list) do
    Enum.filter(list, fn item -> is_list(item) and item != [] end)
  end

  def maps(list) do
    Enum.filter(list, &is_map/1)
  end

  def keyword_lists(list) do
    Enum.filter(list, &Keyword.keyword?/1)
  end
end

Enter your solution below.

defmodule Filter do
  @doc """
  Filter in integers in list.

  ## Examples

    iex> Filter.integers([1, 2, %{}, {}, []])
    [1, 2]
  """
  def integers(list) do
  end

  @doc """
  Filter in floats in list.

  ## Examples

    iex> Filter.floats([1, 2, %{}, {}, [], 1.2, 3.2])
    [1.2, 3.2]
  """
  def floats(list) do
  end

  @doc """
  Filter in numbers and floats in list.

  ## Examples

    iex> Filter.numbers([1, 2, %{}, {}, [], 1.2, 3.2])
    [1, 2, 1.2, 3.2]
  """
  def numbers(list) do
  end

  @doc """
  Filter in atoms in list.

  ## Examples

    iex> Filter.atoms([1, 2, %{}, {}, [], :first_atom, 1.2, 3.2, :second_atom])
    [:first_atom, :second_atom]
  """
  def atoms(list) do
  end

  @doc """
  Filter in lists and keyword lists in list.

  ## Examples

    iex> Filter.lists([1, 2, %{}, {}, [], 1.2, 3.2, :atom, [1, 2], [4, 5, 6]])
    [[], [1, 2], [4, 5, 6]]
  """
  def lists(list) do
  end

  @doc """
  Filter in maps in list.

  ## Examples

    iex> Filter.maps([1, 2, %{}, {}, [], 1.2, 3.2, :atom, %{key: "value"}])
    [%{}, %{key: "value"}]
  """
  def maps(list) do
  end

  @doc """
  Filter in keyword lists.

  ## Examples

    iex> Filter.keyword_lists([1, 2, %{}, {}, [], 1.2, 3.2, :atom, %{key: "value"}, [key: "value"]])
    [[], [key: "value"]]
  """
  def keyword_lists(list) do
  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 Filter Values By Type 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 Built-In Elixir ModulesTic-tac-toe