Elixir basics · Lesson 03 (open contents)
01 · Let the code speak02 · Meet six kinds of values03 · Put values together04 · Build a new list05 · Make the shapes match06 · Choose with case07 · Parse a number safely08 · Unpack &1 and the pipe09 · Put code in a project10 · Ask precise true-or-false questions11 · Read and update nested data12 · Count visible text and bytes13 · Let function clauses choose14 · Solve a list twice15 · Choose the smallest clear control form16 · Give domain data a shape17 · Put one tested module in Mix18 · Make success and failure predictable19 · Read and write one real file20 · Take only the data you need21 · Give different data one shared action22 · Write a callback contract23 · Write down the data contract24 · Test behavior and boundaries25 · Turn a project into a command26 · Write the project promise27 · Parse one real line28 · Keep the public API small29 · Make the promise executable30 · Package the edge and leave clues31 · Prove the project is done
ELIXIR · SCRATCH · LESSON 0325 minutes

Put values together

Use lists, tuples, and maps to hold groups of data.

01 · Start with the whole example

Three collections for three jobs

All three hold several values, but you read them in different ways.

Elixir
# A list keeps its order
hd(["peach", "plum", "apricot"])

# A tuple keeps a status and data together
elem({:ok, 42}, 1)

# A map finds a value by name
%{name: "Robin", score: 92}.score
Check the result first
  • hd(["peach", "plum", "apricot"]) returns the first value, "peach".
  • elem({:ok, 42}, 1) returns 42. Positions start at 0.
  • .score reads 92 from the map.
02 · Take the code apart

Read from the first line down

  1. hd(["peach", "plum", "apricot"]) reads the first item of a non-empty list. Do not use it with an empty list yet.
  2. The 1 in elem({:ok, 42}, 1) is a position. A tuple's first position is numbered 0.
  3. When a map key is an atom, you can read it with dot notation such as .score.
03 · Meet the new symbols

Symbols are not secret signs

[ ]

Square brackets wrap a list.

{ }

Braces wrap a tuple.

%{key: value}

A percent sign and braces make a map. The key is on the left of the colon.

04 · Ideas inside the code

Match each name to its meaning

01

list

An ordered collection in square brackets, such as ["peach", "plum"]. It is useful when values are handled in order.

02

tuple

A fixed group in braces, such as {:ok, 200}. It often holds several parts of one result.

03

map

A collection that finds a value by key, such as %{name: "Robin"}.

05 · Make the idea clear

Why these forms are useful

When one value is not enough, a collection can hold several. A shopping list has an order, so a list fits. A result and its status often travel together in a tuple.

A map finds values by name. In a student record, :name points to the name and :score points to the score. A reader does not need to guess what each position means.

First learn to read collections and take values out. You can learn how to update them later.

06 · Change it yourself

Close the answer and try

Make a map for a book. Store its title and number of pages, then read the page count.

Practice starting point
# Add two keys, then read the page count with a dot
%{____: "Journey to the West", ____: 100}.____

Target result: The result should be 100.

Stuck? Read one hint

You can use the keys title and pages.

After you run it, see one answer
One answer
# A map stores facts under named keys
# An atom key can be read with a dot
%{title: "Journey to the West", pages: 100}.pages
Think about it: Should a name and score go in `["Robin", 92]` or `%{name: "Robin", score: 92}`?

Prefer the map while you are learning. name and score make the meaning of each value clear.

Take with you

Remember these three lines

  1. 1

    A list suits values that will be handled in order.

  2. 2

    A tuple often ties a status to a result.

  3. 3

    A map uses keys to explain its values.

Lesson completeRun the code once, then mark this lesson as done.