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
proplists:get_valuereads with an optional default.- A bare atom like
duplexmeans{duplex, true}. lists:keyfindsearches any tagged-tuple list by position.
Keywords and builtins used here
caseendformatget_boolget_valuekeyfindmainof
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.
Step 3 of 4 in Data shapes, step 17 of 31 in Language basics.