Coder Social home page Coder Social logo

basketball_website_elixir's Introduction

Basketball Website

Welcome to Basketball Website on Exercism's Elixir Track. If you need help running the tests or submitting your code, check out HELP.md. If you get stuck on the exercise, check out HINTS.md, but try and solve it without using those first :)

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"

Source

Created by

  • @neenjaw

Contributed to by

  • @angelikatyborska
  • @NobbZ

basketball_website_elixir's People

Watchers

Dr. Alena avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.