.TH proplists 3erl "stdlib 4.3.1.4" "Ericsson AB" "Erlang Module Definition" .SH NAME proplists \- Support functions for property lists. .SH DESCRIPTION .LP Property lists are ordinary lists containing entries in the form of either tuples, whose first elements are keys used for lookup and insertion, or atoms, which work as shorthand for tuples \fI{Atom, true}\fR\&\&. (Other terms are allowed in the lists, but are ignored by this module\&.) If there is more than one entry in a list for a certain key, the first occurrence normally overrides any later (irrespective of the arity of the tuples)\&. .LP Property lists are useful for representing inherited properties, such as options passed to a function where a user can specify options overriding the default settings, object properties, annotations, and so on\&. .LP Two keys are considered equal if they match (\fI=:=\fR\&)\&. That is, numbers are compared literally rather than by value, so that, for example, \fI1\fR\& and \fI1\&.0\fR\& are different keys\&. .SH DATA TYPES .nf \fBproperty()\fR\& = atom() | tuple() .br .fi .nf \fBproplist()\fR\& = [property()] .br .fi .SH EXPORTS .LP .nf .B append_values(Key, ListIn) -> ListOut .br .fi .br .RS .LP Types: .RS 3 Key = term() .br ListIn = ListOut = [term()] .br .RE .RE .RS .LP Similar to \fIget_all_values/2\fR\&, but each value is wrapped in a list unless it is already itself a list\&. The resulting list of lists is concatenated\&. This is often useful for "incremental" options\&. .LP \fIExample:\fR\& .LP .nf append_values(a, [{a, [1,2]}, {b, 0}, {a, 3}, {c, -1}, {a, [4]}]) .fi .LP returns: .LP .nf [1,2,3,4] .fi .RE .LP .nf .B compact(ListIn) -> ListOut .br .fi .br .RS .LP Types: .RS 3 ListIn = ListOut = [property()] .br .RE .RE .RS .LP Minimizes the representation of all entries in the list\&. This is equivalent to \fI[property(P) || P <- ListIn]\fR\&\&. .LP See also \fIproperty/1\fR\&, \fIunfold/1\fR\&\&. .RE .LP .nf .B delete(Key, List) -> List .br .fi .br .RS .LP Types: .RS 3 Key = term() .br List = [term()] .br .RE .RE .RS .LP Deletes all entries associated with \fIKey\fR\& from \fIList\fR\&\&. .RE .LP .nf .B expand(Expansions, ListIn) -> ListOut .br .fi .br .RS .LP Types: .RS 3 Expansions = [{Property :: property(), Expansion :: [term()]}] .br ListIn = ListOut = [term()] .br .RE .RE .RS .LP Expands particular properties to corresponding sets of properties (or other terms)\&. For each pair \fI{Property, Expansion}\fR\& in \fIExpansions\fR\&: if \fIE\fR\& is the first entry in \fIListIn\fR\& with the same key as \fIProperty\fR\&, and \fIE\fR\& and \fIProperty\fR\& have equivalent normal forms, then \fIE\fR\& is replaced with the terms in \fIExpansion\fR\&, and any following entries with the same key are deleted from \fIListIn\fR\&\&. .LP For example, the following expressions all return \fI[fie, bar, baz, fum]\fR\&: .LP .nf expand([{foo, [bar, baz]}], [fie, foo, fum]) expand([{{foo, true}, [bar, baz]}], [fie, foo, fum]) expand([{{foo, false}, [bar, baz]}], [fie, {foo, false}, fum]) .fi .LP However, no expansion is done in the following call because \fI{foo, false}\fR\& shadows \fIfoo\fR\&: .LP .nf expand([{{foo, true}, [bar, baz]}], [{foo, false}, fie, foo, fum]) .fi .LP Notice that if the original property term is to be preserved in the result when expanded, it must be included in the expansion list\&. The inserted terms are not expanded recursively\&. If \fIExpansions\fR\& contains more than one property with the same key, only the first occurrence is used\&. .LP See also \fInormalize/2\fR\&\&. .RE .LP .nf .B from_map(Map) -> List .br .fi .br .RS .LP Types: .RS 3 Map = #{Key => Value} .br List = [{Key, Value}] .br Key = Value = term() .br .RE .RE .RS .LP Converts the map \fIMap\fR\& to a property list\&. .RE .LP .nf .B get_all_values(Key, List) -> [term()] .br .fi .br .RS .LP Types: .RS 3 Key = term() .br List = [term()] .br .RE .RE .RS .LP Similar to \fIget_value/2\fR\&, but returns the list of values for \fIall\fR\& entries \fI{Key, Value}\fR\& in \fIList\fR\&\&. If no such entry exists, the result is the empty list\&. .RE .LP .nf .B get_bool(Key, List) -> boolean() .br .fi .br .RS .LP Types: .RS 3 Key = term() .br List = [term()] .br .RE .RE .RS .LP Returns the value of a boolean key/value option\&. If \fIlookup(Key, List)\fR\& would yield \fI{Key, true}\fR\&, this function returns \fItrue\fR\&, otherwise \fIfalse\fR\&\&. .LP See also \fIget_value/2\fR\&, \fIlookup/2\fR\&\&. .RE .LP .nf .B get_keys(List) -> [term()] .br .fi .br .RS .LP Types: .RS 3 List = [term()] .br .RE .RE .RS .LP Returns an unordered list of the keys used in \fIList\fR\&, not containing duplicates\&. .RE .LP .nf .B get_value(Key, List) -> term() .br .fi .br .RS .LP Types: .RS 3 Key = term() .br List = [term()] .br .RE .RE .RS .LP Equivalent to \fIget_value(Key, List, undefined)\fR\&\&. .RE .LP .nf .B get_value(Key, List, Default) -> term() .br .fi .br .RS .LP Types: .RS 3 Key = term() .br List = [term()] .br Default = term() .br .RE .RE .RS .LP Returns the value of a simple key/value property in \fIList\fR\&\&. If \fIlookup(Key, List)\fR\& would yield \fI{Key, Value}\fR\&, this function returns the corresponding \fIValue\fR\&, otherwise \fIDefault\fR\&\&. .LP See also \fIget_all_values/2\fR\&, \fIget_bool/2\fR\&, \fIget_value/2\fR\&, \fIlookup/2\fR\&\&. .RE .LP .nf .B is_defined(Key, List) -> boolean() .br .fi .br .RS .LP Types: .RS 3 Key = term() .br List = [term()] .br .RE .RE .RS .LP Returns \fItrue\fR\& if \fIList\fR\& contains at least one entry associated with \fIKey\fR\&, otherwise \fIfalse\fR\&\&. .RE .LP .nf .B lookup(Key, List) -> none | tuple() .br .fi .br .RS .LP Types: .RS 3 Key = term() .br List = [term()] .br .RE .RE .RS .LP Returns the first entry associated with \fIKey\fR\& in \fIList\fR\&, if one exists, otherwise returns \fInone\fR\&\&. For an atom \fIA\fR\& in the list, the tuple \fI{A, true}\fR\& is the entry associated with \fIA\fR\&\&. .LP See also \fIget_bool/2\fR\&, \fIget_value/2\fR\&, \fIlookup_all/2\fR\&\&. .RE .LP .nf .B lookup_all(Key, List) -> [tuple()] .br .fi .br .RS .LP Types: .RS 3 Key = term() .br List = [term()] .br .RE .RE .RS .LP Returns the list of all entries associated with \fIKey\fR\& in \fIList\fR\&\&. If no such entry exists, the result is the empty list\&. .LP See also \fIlookup/2\fR\&\&. .RE .LP .nf .B normalize(ListIn, Stages) -> ListOut .br .fi .br .RS .LP Types: .RS 3 ListIn = [term()] .br Stages = [Operation] .br Operation = .br {aliases, Aliases} | .br {negations, Negations} | .br {expand, Expansions} .br Aliases = Negations = [{Key, Key}] .br Expansions = [{Property :: property(), Expansion :: [term()]}] .br ListOut = [term()] .br .RE .RE .RS .LP Passes \fIListIn\fR\& through a sequence of substitution/expansion stages\&. For an \fIaliases\fR\& operation, function \fIsubstitute_aliases/2\fR\& is applied using the specified list of aliases: .RS 2 .TP 2 * For a \fInegations\fR\& operation, \fIsubstitute_negations/2\fR\& is applied using the specified negation list\&. .LP .TP 2 * For an \fIexpand\fR\& operation, function \fIexpand/2\fR\& is applied using the specified list of expansions\&. .LP .RE .LP The final result is automatically compacted (compare \fIcompact/1\fR\&)\&. .LP Typically you want to substitute negations first, then aliases, then perform one or more expansions (sometimes you want to pre-expand particular entries before doing the main expansion)\&. You might want to substitute negations and/or aliases repeatedly, to allow such forms in the right-hand side of aliases and expansion lists\&. .LP See also \fIsubstitute_negations/2\fR\&\&. .RE .LP .nf .B property(PropertyIn) -> PropertyOut .br .fi .br .RS .LP Types: .RS 3 PropertyIn = PropertyOut = property() .br .RE .RE .RS .LP Creates a normal form (minimal) representation of a property\&. If \fIPropertyIn\fR\& is \fI{Key, true}\fR\&, where \fIKey\fR\& is an atom, \fIKey\fR\& is returned, otherwise the whole term \fIPropertyIn\fR\& is returned\&. .LP See also \fIproperty/2\fR\&\&. .RE .LP .nf .B property(Key, Value) -> Property .br .fi .br .RS .LP Types: .RS 3 Key = Value = term() .br Property = atom() | {term(), term()} .br .RE .RE .RS .LP Creates a normal form (minimal) representation of a simple key/value property\&. Returns \fIKey\fR\& if \fIValue\fR\& is \fItrue\fR\& and \fIKey\fR\& is an atom, otherwise a tuple \fI{Key, Value}\fR\& is returned\&. .LP See also \fIproperty/1\fR\&\&. .RE .LP .nf .B split(List, Keys) -> {Lists, Rest} .br .fi .br .RS .LP Types: .RS 3 List = Keys = [term()] .br Lists = [[term()]] .br Rest = [term()] .br .RE .RE .RS .LP Partitions \fIList\fR\& into a list of sublists and a remainder\&. \fILists\fR\& contains one sublist for each key in \fIKeys\fR\&, in the corresponding order\&. The relative order of the elements in each sublist is preserved from the original \fIList\fR\&\&. \fIRest\fR\& contains the elements in \fIList\fR\& that are not associated with any of the specified keys, also with their original relative order preserved\&. .LP \fIExample:\fR\& .LP .nf split([{c, 2}, {e, 1}, a, {c, 3, 4}, d, {b, 5}, b], [a, b, c]) .fi .LP returns: .LP .nf {[[a], [{b, 5}, b],[{c, 2}, {c, 3, 4}]], [{e, 1}, d]} .fi .RE .LP .nf .B substitute_aliases(Aliases, ListIn) -> ListOut .br .fi .br .RS .LP Types: .RS 3 Aliases = [{Key, Key}] .br Key = term() .br ListIn = ListOut = [term()] .br .RE .RE .RS .LP Substitutes keys of properties\&. For each entry in \fIListIn\fR\&, if it is associated with some key \fIK1\fR\& such that \fI{K1, K2}\fR\& occurs in \fIAliases\fR\&, the key of the entry is changed to \fIK2\fR\&\&. If the same \fIK1\fR\& occurs more than once in \fIAliases\fR\&, only the first occurrence is used\&. .LP For example, \fIsubstitute_aliases([{color, colour}], L)\fR\& replaces all tuples \fI{color, \&.\&.\&.}\fR\& in \fIL\fR\& with \fI{colour, \&.\&.\&.}\fR\&, and all atoms \fIcolor\fR\& with \fIcolour\fR\&\&. .LP See also \fInormalize/2\fR\&, \fIsubstitute_negations/2\fR\&\&. .RE .LP .nf .B substitute_negations(Negations, ListIn) -> ListOut .br .fi .br .RS .LP Types: .RS 3 Negations = [{Key1, Key2}] .br Key1 = Key2 = term() .br ListIn = ListOut = [term()] .br .RE .RE .RS .LP Substitutes keys of boolean-valued properties and simultaneously negates their values\&. For each entry in \fIListIn\fR\&, if it is associated with some key \fIK1\fR\& such that \fI{K1, K2}\fR\& occurs in \fINegations\fR\&: if the entry was \fI{K1, true}\fR\&, it is replaced with \fI{K2, false}\fR\&, otherwise with \fIK2\fR\&, thus changing the name of the option and simultaneously negating the value specified by \fIget_bool(Key, ListIn)\fR\&\&. If the same \fIK1\fR\& occurs more than once in \fINegations\fR\&, only the first occurrence is used\&. .LP For example, \fIsubstitute_negations([{no_foo, foo}], L)\fR\& replaces any atom \fIno_foo\fR\& or tuple \fI{no_foo, true}\fR\& in \fIL\fR\& with \fI{foo, false}\fR\&, and any other tuple \fI{no_foo, \&.\&.\&.}\fR\& with \fIfoo\fR\&\&. .LP See also \fIget_bool/2\fR\&, \fInormalize/2\fR\&, \fIsubstitute_aliases/2\fR\&\&. .RE .LP .nf .B to_map(List) -> Map .br .fi .br .RS .LP Types: .RS 3 List = [Shorthand | {Key, Value} | term()] .br Map = #{Shorthand => true, Key => Value} .br Shorthand = atom() .br Key = Value = term() .br .RE .RE .RS .LP Converts the property list \fIList\fR\& to a map\&. .LP Shorthand atom values in \fIList\fR\& will be expanded to an association of the form \fIAtom => true\fR\&\&. Tuples of the form \fI{Key, Value}\fR\& in \fIList\fR\& will be converted to an association of the form \fIKey => Value\fR\&\&. Anything else will be silently ignored\&. .LP If the same key appears in \fIList\fR\& multiple times, the value of the one appearing nearest to the head of \fIList\fR\& will be in the result map, that is the value that would be returned by a call to \fIget_value(Key, List)\fR\&\&. .LP \fIExample:\fR\& .LP .nf to_map([a, {b, 1}, {c, 2}, {c, 3}]) .fi .LP returns: .LP .nf #{a => true, b => 1, c => 2} .fi .RE .LP .nf .B to_map(List, Stages) -> Map .br .fi .br .RS .LP Types: .RS 3 List = [term()] .br Stages = [Operation] .br Operation = .br {aliases, Aliases} | .br {negations, Negations} | .br {expand, Expansions} .br Aliases = Negations = [{Key, Key}] .br Expansions = [{Property :: property(), Expansion :: [term()]}] .br Map = #{term() => term()} .br .RE .RE .RS .LP Converts the property list \fIList\fR\& to a map after applying the normalizations given in \fIStages\fR\&\&. .LP See also \fInormalize/2\fR\&, \fIto_map/1\fR\&\&. .RE .LP .nf .B unfold(ListIn) -> ListOut .br .fi .br .RS .LP Types: .RS 3 ListIn = ListOut = [term()] .br .RE .RE .RS .LP Unfolds all occurrences of atoms in \fIListIn\fR\& to tuples \fI{Atom, true}\fR\&\&. .RE