Powered by AppSignal & Oban Pro

Filter Values By Type

exercises/filter_values_by_type.livemd

Filter Values By Type

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

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 ```elixir 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 and empty lists in list.

  ## 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

Mark As Completed

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

save_name =
  case Path.basename(__DIR__) do
    "reading" -> "filter_values_by_type_reading"
    "exercises" -> "filter_values_by_type_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 filter-values-by-type-exercise
$ git add .
$ git commit -m "finish filter values by type exercise"
$ git push origin filter-values-by-type-exercise

Create a pull request from your filter-values-by-type-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
Built-In Modules Tic-tac-toe