Erlang basics · Lesson 11 (open contents)
01 · Open erl02 · Meet common terms03 · Tell two kinds of text apart04 · Bind a variable once05 · Split and filter a list06 · Parse a number safely07 · Choose one path with case08 · Let a function keep going09 · Put code in a module10 · Compare terms and Boolean results11 · Read and update nested maps12 · Build and inspect UTF-8 binaries13 · Let clauses choose the function path14 · Solve a list twice15 · Choose with patterns, guards, and errors16 · Describe a module and one record17 · Put one tested module in Rebar318 · Return success or failure as data19 · Read and write one file safely20 · Build a data pipeline with functions21 · Keep records behind a module boundary22 · Define a module contract23 · Describe the data contract24 · Test small rules and whole flows25 · Shape builds and make 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
ERLANG · FOUNDATION · LESSON 1125 minutes

Read and update nested maps

Use map patterns, `maps:get/2`, and immutable map updates.

01 · Start with the whole example

Run this first

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

Erlang
%% Rebuild each changed map layer
Student = #{profile => #{name => <<"Maya">>, age => 12}},
Profile = maps:get(profile, Student),
Older = Student#{profile := Profile#{age := 13}},
{maps:get(age, Profile), maps:get(age, maps:get(profile, Older))}.
Check the result first
  • The result is {12,13}; Student still contains age 12.
02 · Take the code apart

Read from the first line down

  1. Read maps:get(Key, Map): Read one key from a map.
  2. Read #{key := Value}: Update an existing map key.
  3. Read #{key => Value}: Add or replace a key.
03 · Meet the new symbols

Symbols are not secret signs

maps:get(Key, Map)

Read one key from a map.

#{key := Value}

Update an existing map key.

#{key => Value}

Add or replace a key.

04 · Ideas inside the code

Match each name to its meaning

01

map access

maps:get/2 reads a known key and raises when it is missing.

02

map update

Map#{key := value} updates an existing key and returns a new map.

03

nested update

Read the inner map, update it, then place that new map into the outer map.

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.

maps:get/2 reads a known key and raises when it is missing.

Map#{key := value} updates an existing key and returns a new map.

06 · Change it yourself

Close the answer and try

Change a nested page count from 80 to 100.

Practice starting point
Book = #{info => #{pages => 80}},
Info = maps:get(info, Book),
Longer = Book#{info := Info#{pages := ____}},
{maps:get(pages, Info), maps:get(pages, maps:get(info, Longer))}.

Target result: Return {80,100}.

Stuck? Read one hint

Use 100 as the new page value.

After you run it, see one answer
One answer
Book = #{info => #{pages => 80}},
Info = maps:get(info, Book),
Longer = Book#{info := Info#{pages := 100}},
{maps:get(pages, Info), maps:get(pages, maps:get(info, Longer))}.
Think about it: Does a map update change the old map?

No. It returns a new map and keeps the old value available.

Take with you

Remember these three lines

  1. 1

    Read known keys deliberately.

  2. 2

    Use := to require an existing key.

  3. 3

    Rebuild nested layers from the inside out.

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