Tracks
/
Elixir
Elixir
/
Exercises
/
Basketball Website
Basketball Website

Basketball Website

Learning Exercise

Introduction

Access Behaviour

Elixir uses code Behaviours to provide common generic interfaces while facilitating specific implementations for each module which implements it. One such common example is the Access Behaviour.

The Access Behaviour provides a common interface for retrieving data from a key-based data structure. The Access Behaviour is implemented for maps and keyword lists, but let's look at its use for maps to get a feel for it. Access Behaviour specifies that when you have a map, you may follow it with square brackets and then use the key to retrieve the value associated with that key.

# Suppose we have these two maps defined (note the difference in the key type)
my_map = %{key: "my value"}
your_map = %{"key" => "your value"}

# Obtain the value using the Access Behaviour
my_map[:key] == "my value"
your_map[:key] == nil
your_map["key"] == "your value"

If the key does not exist in the data structure, then nil is returned. This can be a source of unintended behavior, because it does not raise an error. Note that nil itself implements the Access Behaviour and always returns nil for any key.

Instructions

You are working with a web development team to maintain a website for a local basketball team. The web development team is less familiar with Elixir and is asking for a function to be able to extract data from a series of nested maps to facilitate rapid development.

1. Extract data from a nested map structure

Implement the extract_from_path/2 function to take two arguments:

  • data: a nested map structure with data about the basketball team.
  • path: a string consisting of period-delimited keys to obtain the value associated with the last key.

If the value or the key does not exist at any point in the path, nil should be returned

data = %{
  "team_mascot" => %{
    "animal" => "bear",
    "actor" => %{
      "first_name" => "Noel"
    }
  }
}

BasketballWebsite.extract_from_path(data, "team_mascot.animal")
# => "bear"
BasketballWebsite.extract_from_path(data, "team_mascot.colors")
# => nil

Use the Access Behaviour when implementing this function.

Do not use any Map or Kernel module functions for working with the nested map data structure.

2. Refactor using included functions

Your coworker reviewing your code tells you about a Kernel module function which does something very similar to your implementation.

Implement get_in_path/2 to use this Kernel module function.

The arguments expected are the same as part 1.

BasketballWebsite.get_in_path(data, "team_mascot.actor.first_name")
# => "Noel"
Edit via GitHub The link opens in a new window or tab
Elixir Exercism

Ready to start Basketball Website?

Sign up to Exercism to learn and master Elixir with 57 concepts, 158 exercises, and real human mentoring, all for free.