Elixir basics · Lesson 04 (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 0420 minutes

Build a new list

Bind a list, transform it with `for`, and leave the original value unchanged.

01 · Start with the whole example

Build a new list and keep the old one

for returns a new list. It does not edit numbers.

Elixir
# Draw each value from numbers and collect a doubled value
numbers = [1, 2, 3]
doubled = for value <- numbers do
  value * 2
end

# Return both lists and check the original
{numbers, doubled, numbers == [1, 2, 3]}
Check the result first
  • doubled is [2, 4, 6].
  • numbers is still [1, 2, 3].
  • The final result is {[1, 2, 3], [2, 4, 6], true}.
02 · Take the code apart

Read from the first line down

  1. numbers = [1, 2, 3] binds the original list to a name.
  2. value <- numbers draws 1, then 2, then 3. The body calculates a result for each value.
  3. The comprehension collects those results in [2, 4, 6]. It never changes the original list.
03 · Meet the new symbols

Symbols are not secret signs

=

Bind the value on the right to the name on the left. The next lesson shows its matching job.

<-

Take each value from the collection on the right, one at a time.

for ... do ... end

Run the body for each drawn value and collect the results in a new list.

==

Compare two values and return true or false.

04 · Ideas inside the code

Match each name to its meaning

01

binding

Giving a value a name, as in numbers = [1, 2, 3].

02

immutable

An existing value is not changed in place. A transformation creates a new value.

03

list comprehension

A for expression that visits values and collects a new list of results.

05 · Make the idea clear

Why these forms are useful

A binding gives a value a name. In numbers = [1, 2, 3], the name numbers points to that list.

Elixir data is immutable: code does not change the existing list in place. A transformation builds a new list instead.

A for comprehension runs one expression for each value it draws from a collection. In value <- numbers, read <- as “take each value from numbers.”

06 · Change it yourself

Close the answer and try

Use a for comprehension to multiply every value in [1, 2, 3] by 3. Return both the old and new lists.

Practice starting point
numbers = [1, 2, 3]

tripled = for value ____ numbers do
  ____
end

{numbers, tripled}

Target result: The result should be {[1, 2, 3], [3, 6, 9]}.

Stuck? Read one hint

Draw values with <-, then calculate value * 3 in the body.

After you run it, see one answer
One answer
# Build a new list from the old one
numbers = [1, 2, 3]

tripled = for value <- numbers do
  value * 3
end

{numbers, tripled}
Think about it: After `doubled = for value <- numbers do value * 2 end`, did `numbers` change?

No. The comprehension built a new list and bound it to doubled. The original list stayed the same.

Take with you

Remember these three lines

  1. 1

    A binding gives a value a name.

  2. 2

    value <- list draws each value from a list.

  3. 3

    A for comprehension builds a new list instead of changing the old one.

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