typestar

Maps at scale in Erlang

maps:fold, filter, and merge work the whole table at once.

%% maps:fold, filter, and merge work the whole table at once.
main(_) ->
    Minutes = #{"5560199" => 12, "5560142" => 3, "5560170" => 21},

    Total = maps:fold(fun(_, M, Acc) -> Acc + M end, 0, Minutes),
    io:format("Billed minutes: ~p~n", [Total]),

    Long = maps:filter(fun(_, M) -> M > 10 end, Minutes),
    io:format("Long calls: ~p~n", [maps:size(Long)]),

    Evening = #{"5560142" => 6, "5560188" => 9},
    Merged = maps:merge(Minutes, Evening),
    io:format("All lines: ~p~n", [maps:size(Merged)]),
    io:format("5560142 now: ~p~n", [maps:get("5560142", Merged)]).

How it works

  1. maps:fold threads an accumulator over every pair.
  2. maps:filter keeps entries its fun approves.
  3. merge favors the second map on key collisions.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
546
Tokens
188
Three-star pace
60 tpm

At the three-star pace of 60 tokens a minute, this run takes about 188 seconds.

Type this snippet

Step 4 of 4 in Data shapes, step 18 of 31 in Language basics.

← Previous Next →