Maps in Erlang
=> writes, := updates and matches, maps:get reads with defaults.
%% Maps write with =>, update bound keys with :=, match with :=.
main(_) ->
Line = #{number => 7, state => idle, pair => "red-white"},
io:format("Pair: ~s~n", [maps:get(pair, Line)]),
io:format("Fallback: ~p~n", [maps:get(fuse, Line, none)]),
Ringing = Line#{state := ringing},
io:format("Now: ~p~n", [maps:get(state, Ringing)]),
#{number := N, state := S} = Ringing,
io:format("Line ~p is ~p~n", [N, S]),
io:format("Keys: ~p~n", [maps:keys(Ringing)]).
How it works
#{number => 7}builds;Line#{state := ringing}updates.:=in an update insists the key already exists.#{number := N} = Mappattern-matches values out.
Keywords and builtins used here
formatgetkeysmain
The run, in numbers
- Lines
- 13
- Characters to type
- 455
- Tokens
- 159
- Three-star pace
- 60 tpm
At the three-star pace of 60 tokens a minute, this run takes about 159 seconds.
Step 1 of 4 in Data shapes, step 15 of 31 in Language basics.