typestar

Proplists in Erlang

Tagged tuples in a list: the classic options style.

%% Proplists: tagged tuples in a list, the classic options style.
main(_) ->
    Options = [{speed, 300}, {parity, even}, duplex],

    io:format("Speed: ~p~n", [proplists:get_value(speed, Options)]),
    io:format("Stop bits: ~p~n",
              [proplists:get_value(stop_bits, Options, 1)]),
    io:format("Duplex? ~p~n", [proplists:get_bool(duplex, Options)]),

    %% lists:keyfind works on any tagged-tuple list.
    case lists:keyfind(parity, 1, Options) of
        {parity, P} -> io:format("Parity ~p~n", [P]);
        false -> io:format("No parity set~n")
    end.

How it works

  1. proplists:get_value reads with an optional default.
  2. A bare atom like duplex means {duplex, true}.
  3. lists:keyfind searches any tagged-tuple list by position.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
515
Tokens
142
Three-star pace
60 tpm

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

Type this snippet

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

← Previous Next →