Tracks
/
Elixir
Elixir
/
Exercises
/
Need For Speed
Need For Speed

Need For Speed

Learning Exercise

Introduction

Alias

To share code between different Elixir modules within the same project, you need to reference the outside module by its full name. But what if that name is too long or confusing?

The special form alias allows you to shorten or change the name by which you reference an outside module. When used without any arguments, it trims down the module name to its last segment, e.g. MyApp.Logger.Settings becomes Settings. A custom name can be specified with the :as option.

Usually aliases are added at the beginning of the module definition.

defmodule Square do
  alias Integer, as: I

  def area(a), do: I.pow(a, 2)
end

Import

The special form import allows you to use functions from an outside module without using the module's name.

Importing a whole outside module might create conflicts with existing local functions. To avoid this, two options are available: :except and :only. Both expect a keyword list, where the key is the function name, and the value is the function's arity.

Usually imports are added at the beginning of the module definition.

defmodule Square do
  import Integer, only: [pow: 2]

  def area(a), do: pow(a, 2)
end

Instructions

That remote controlled car that you bought recently has turned into a whole new hobby. You have been organizing remote control car races.

You were almost finished writing a program that would allow to run race simulations when your cat jumped at your keyboard and deleted a few lines of code. Now your program doesn't compile anymore...

1. Fix compilation error Race.__struct__/0 is undefined

Add an alias so that the module NeedForSpeed.Race can be referenced by the shorter name Race.

2. Fix compilation error Car.__struct__/0 is undefined

Add an alias so that the module NeedForSpeed.RemoteControlCar can be referenced by the shorter name Car.

3. Fix compilation error undefined function puts/1

The function puts/1 comes from the module IO. Import it to be able to use it without referencing the module.

4. Fix compilation error undefined function default_color/0

The functions default_color/0, red/0, cyan/0, and green/0 all come from the module IO.ANSI. You're planning to add support for other car colors, so you want to import the whole module. Unfortunately, the function color/1 from the module IO.ANSI conflicts with one of your local functions. Import the whole IO.ANSI module except that one function.

Edit via GitHub The link opens in a new window or tab
Elixir Exercism

Ready to start Need For Speed?

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