Elixir basics · Lesson 11 (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 · FOUNDATION · LESSON 1125 minutes

Read and update nested data

Use keyword lists, maps, `get_in`, `put_in`, and `update_in` without changing the source value.

01 · Start with the whole example

Run this first

Run it once, change one input, and compare the new result.

Elixir
# Update a nested map without changing student
student = %{profile: %{name: "Maya", age: 12}, options: [active: true]}
older = put_in(student.profile.age, 13)
{get_in(older, [:profile, :name]), student.profile.age, older.profile.age}
Check the result first
  • The result is {"Maya", 12, 13}; student remains unchanged.
02 · Take the code apart

Read from the first line down

  1. Read [active: true]: Write a keyword list.
  2. Read get_in(data, path): Read a value along a nested path.
  3. Read put_in(data.path, value): Return a copy with one nested value replaced.
03 · Meet the new symbols

Symbols are not secret signs

[active: true]

Write a keyword list.

get_in(data, path)

Read a value along a nested path.

put_in(data.path, value)

Return a copy with one nested value replaced.

04 · Ideas inside the code

Match each name to its meaning

01

keyword list

A keyword list is a list of two-item tuples with atom keys; options often use this form.

02

nested access

get_in/2 follows a path through nested maps or keyword data.

03

immutable update

put_in and update_in return new data instead of editing the original value.

05 · Make the idea clear

Why these forms are useful

Run the example first. Predict one result, then change one input and run it again.

A keyword list is a list of two-item tuples with atom keys; options often use this form.

get_in/2 follows a path through nested maps or keyword data.

06 · Change it yourself

Close the answer and try

Add 20 pages to a nested book map with update_in.

Practice starting point
book = %{info: %{title: "River", pages: 80}}
longer = update_in(book.info.pages, fn pages -> ____ end)
{book.info.pages, longer.info.pages}

Target result: Return {80, 100}.

Stuck? Read one hint

Return pages + 20 from the anonymous function.

After you run it, see one answer
One answer
book = %{info: %{title: "River", pages: 80}}
longer = update_in(book.info.pages, fn pages -> pages + 20 end)
{book.info.pages, longer.info.pages}
Think about it: Does `put_in` edit its input map?

No. It returns a new value with the requested path changed.

Take with you

Remember these three lines

  1. 1

    Keyword lists commonly carry options.

  2. 2

    Use paths to read nested data.

  3. 3

    Updates return new collections.

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