1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
<?xml version="1.0" encoding="latin1" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">
<erlref>
<header>
<copyright>
<year>2002</year><year>2011</year>
<holder>Ericsson AB. All Rights Reserved.</holder>
</copyright>
<legalnotice>
The contents of this file are subject to the Erlang Public License,
Version 1.1, (the "License"); you may not use this file except in
compliance with the License. You should have received a copy of the
Erlang Public License along with this software. If not, it can be
retrieved online at http://www.erlang.org/.
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
the License for the specific language governing rights and limitations
under the License.
</legalnotice>
<title>proplists</title>
<prepared>Patrik Nyblom</prepared>
<responsible>Kenneth Lundin</responsible>
<docno>1</docno>
<approved>Kenneth Lundin</approved>
<checked></checked>
<date>2002-09-28</date>
<rev>A</rev>
<file>proplists.sgml</file>
</header>
<module>proplists</module>
<modulesummary>Support functions for property lists</modulesummary>
<description>
<p>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 <c>{Atom, true}</c>. (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).</p>
<p>Property lists are useful for representing inherited properties,
such as options passed to a function where a user may specify options
overriding the default settings, object properties, annotations,
etc.</p>
<p>Two keys are considered equal if they match (<c>=:=</c>). In other
words, numbers are compared literally rather than by value, so that,
for instance, <c>1</c> and <c>1.0</c> are different keys.</p>
</description>
<datatypes>
<datatype>
<name name="property"/>
</datatype>
</datatypes>
<funcs>
<func>
<name name="append_values" arity="2"/>
<fsummary></fsummary>
<desc>
<p>Similar to <c>get_all_values/2</c>, but each value is
wrapped in a list unless it is already itself a list, and the
resulting list of lists is concatenated. This is often useful for
"incremental" options; e.g., <c>append_values(a, [{a, [1,2]}, {b, 0}, {a, 3}, {c, -1}, {a, [4]}])</c> will return the list
<c>[1,2,3,4]</c>.</p>
</desc>
</func>
<func>
<name name="compact" arity="1"/>
<fsummary></fsummary>
<desc>
<p>Minimizes the representation of all entries in the list. This is
equivalent to <c><![CDATA[[property(P) || P <- ListIn]]]></c>.</p>
<p>See also: <c>property/1</c>, <c>unfold/1</c>.</p>
</desc>
</func>
<func>
<name name="delete" arity="2"/>
<fsummary></fsummary>
<desc>
<p>Deletes all entries associated with <c><anno>Key</anno></c> from
<c><anno>List</anno></c>.</p>
</desc>
</func>
<func>
<name name="expand" arity="2"/>
<fsummary></fsummary>
<desc>
<p>Expands particular properties to corresponding sets of
properties (or other terms). For each pair <c>{<anno>Property</anno>, <anno>Expansion</anno>}</c> in <c><anno>Expansions</anno></c>, if <c>E</c> is
the first entry in <c><anno>ListIn</anno></c> with the same key as
<c><anno>Property</anno></c>, and <c>E</c> and <c><anno>Property</anno></c>
have equivalent normal forms, then <c>E</c> is replaced with
the terms in <c><anno>Expansion</anno></c>, and any following entries with
the same key are deleted from <c><anno>ListIn</anno></c>.</p>
<p>For example, the following expressions all return <c>[fie, bar, baz, fum]</c>:</p>
<code type="none">
expand([{foo, [bar, baz]}],
[fie, foo, fum])
expand([{{foo, true}, [bar, baz]}],
[fie, foo, fum])
expand([{{foo, false}, [bar, baz]}],
[fie, {foo, false}, fum])</code>
<p>However, no expansion is done in the following call:</p>
<code type="none">
expand([{{foo, true}, [bar, baz]}],
[{foo, false}, fie, foo, fum])</code>
<p>because <c>{foo, false}</c> shadows <c>foo</c>.</p>
<p>Note 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
<c><anno>Expansions</anno></c> contains more than one property with the same
key, only the first occurrence is used.</p>
<p>See also: <c>normalize/2</c>.</p>
</desc>
</func>
<func>
<name name="get_all_values" arity="2"/>
<fsummary></fsummary>
<desc>
<p>Similar to <c>get_value/2</c>, but returns the list of
values for <em>all</em> entries <c>{Key, Value}</c> in
<c><anno>List</anno></c>. If no such entry exists, the result is the empty
list.</p>
<p>See also: <c>get_value/2</c>.</p>
</desc>
</func>
<func>
<name name="get_bool" arity="2"/>
<fsummary></fsummary>
<desc>
<p>Returns the value of a boolean key/value option. If
<c>lookup(<anno>Key</anno>, <anno>List</anno>)</c> would yield <c>{<anno>Key</anno>, true}</c>,
this function returns <c>true</c>; otherwise <c>false</c>
is returned.</p>
<p>See also: <c>get_value/2</c>, <c>lookup/2</c>.</p>
</desc>
</func>
<func>
<name name="get_keys" arity="1"/>
<fsummary></fsummary>
<desc>
<p>Returns an unordered list of the keys used in <c><anno>List</anno></c>,
not containing duplicates.</p>
</desc>
</func>
<func>
<name name="get_value" arity="2"/>
<fsummary></fsummary>
<desc>
<p>Equivalent to <c>get_value(<anno>Key</anno>, <anno>List</anno>, undefined)</c>.</p>
</desc>
</func>
<func>
<name name="get_value" arity="3"/>
<fsummary></fsummary>
<desc>
<p>Returns the value of a simple key/value property in
<c><anno>List</anno></c>. If <c>lookup(<anno>Key</anno>, <anno>List</anno>)</c> would yield
<c>{<anno>Key</anno>, Value}</c>, this function returns the corresponding
<c>Value</c>, otherwise <c><anno>Default</anno></c> is returned.</p>
<p>See also: <c>get_all_values/2</c>, <c>get_bool/2</c>,
<c>get_value/2</c>, <c>lookup/2</c>.</p>
</desc>
</func>
<func>
<name name="is_defined" arity="2"/>
<fsummary></fsummary>
<desc>
<p>Returns <c>true</c> if <c><anno>List</anno></c> contains at least
one entry associated with <c><anno>Key</anno></c>, otherwise
<c>false</c> is returned.</p>
</desc>
</func>
<func>
<name name="lookup" arity="2"/>
<fsummary></fsummary>
<desc>
<p>Returns the first entry associated with <c><anno>Key</anno></c> in
<c><anno>List</anno></c>, if one exists, otherwise returns
<c>none</c>. For an atom <c>A</c> in the list, the tuple
<c>{A, true}</c> is the entry associated with <c>A</c>.</p>
<p>See also: <c>get_bool/2</c>, <c>get_value/2</c>,
<c>lookup_all/2</c>.</p>
</desc>
</func>
<func>
<name name="lookup_all" arity="2"/>
<fsummary></fsummary>
<desc>
<p>Returns the list of all entries associated with <c><anno>Key</anno></c>
in <c><anno>List</anno></c>. If no such entry exists, the result is the
empty list.</p>
<p>See also: <c>lookup/2</c>.</p>
</desc>
</func>
<func>
<name name="normalize" arity="2"/>
<fsummary></fsummary>
<desc>
<p>Passes <c><anno>ListIn</anno></c> through a sequence of
substitution/expansion stages. For an <c>aliases</c> operation,
the function <c>substitute_aliases/2</c> is applied using the
given list of aliases; for a <c>negations</c> operation,
<c>substitute_negations/2</c> is applied using the given
negation list; for an <c>expand</c> operation, the function
<c>expand/2</c> is applied using the given list of expansions.
The final result is automatically compacted (cf.
<c>compact/1</c>).</p>
<p>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.</p>
<p>See also: <c>compact/1</c>, <c>expand/2</c>,
<c>substitute_aliases/2</c>, <c>substitute_negations/2</c>.</p>
</desc>
</func>
<func>
<name name="property" arity="1"/>
<fsummary></fsummary>
<desc>
<p>Creates a normal form (minimal) representation of a property. If
<c><anno>PropertyIn</anno></c> is <c>{Key, true}</c> where
<c>Key</c> is an atom, this returns <c>Key</c>, otherwise
the whole term <c><anno>PropertyIn</anno></c> is returned.</p>
<p>See also: <c>property/2</c>.</p>
</desc>
</func>
<func>
<name name="property" arity="2"/>
<fsummary></fsummary>
<desc>
<p>Creates a normal form (minimal) representation of a simple
key/value property. Returns <c><anno>Key</anno></c> if <c><anno>Value</anno></c> is
<c>true</c> and <c><anno>Key</anno></c> is an atom, otherwise a tuple
<c>{<anno>Key</anno>, <anno>Value</anno>}</c> is returned.</p>
<p>See also: <c>property/1</c>.</p>
</desc>
</func>
<func>
<name name="split" arity="2"/>
<fsummary></fsummary>
<desc>
<p>Partitions <c><anno>List</anno></c> into a list of sublists and a
remainder. <c><anno>Lists</anno></c> contains one sublist for each key in
<c><anno>Keys</anno></c>, in the corresponding order. The relative order of
the elements in each sublist is preserved from the original
<c><anno>List</anno></c>. <c><anno>Rest</anno></c> contains the elements in
<c><anno>List</anno></c> that are not associated with any of the given keys,
also with their original relative order preserved.</p>
<p>Example:
split([{c, 2}, {e, 1}, a, {c, 3, 4}, d, {b, 5}, b], [a, b, c])</p>
<p>returns</p>
<p>{[[a], [{b, 5}, b],[{c, 2}, {c, 3, 4}]], [{e, 1}, d]}</p>
</desc>
</func>
<func>
<name name="substitute_aliases" arity="2"/>
<fsummary></fsummary>
<desc>
<p>Substitutes keys of properties. For each entry in
<c><anno>ListIn</anno></c>, if it is associated with some key <c>K1</c>
such that <c>{K1, K2}</c> occurs in <c><anno>Aliases</anno></c>, the
key of the entry is changed to <c>K2</c>. If the same
<c>K1</c> occurs more than once in <c><anno>Aliases</anno></c>, only
the first occurrence is used.</p>
<p>Example: <c>substitute_aliases([{color, colour}], L)</c>
will replace all tuples <c>{color, ...}</c> in <c>L</c>
with <c>{colour, ...}</c>, and all atoms <c>color</c>
with <c>colour</c>.</p>
<p>See also: <c>normalize/2</c>, <c>substitute_negations/2</c>.</p>
</desc>
</func>
<func>
<name name="substitute_negations" arity="2"/>
<fsummary></fsummary>
<desc>
<p>Substitutes keys of boolean-valued properties and
simultaneously negates their values. For each entry in
<c><anno>ListIn</anno></c>, if it is associated with some key <c>K1</c>
such that <c>{K1, K2}</c> occurs in <c><anno>Negations</anno></c>, then
if the entry was <c>{K1, true}</c> it will be replaced with
<c>{K2, false}</c>, otherwise it will be replaced with
<c>{K2, true}</c>, thus changing the name of the option and
simultaneously negating the value given by
<c>get_bool(ListIn)</c>. If the same <c>K1</c> occurs more
than once in <c><anno>Negations</anno></c>, only the first occurrence is
used.</p>
<p>Example: <c>substitute_negations([{no_foo, foo}], L)</c>
will replace any atom <c>no_foo</c> or tuple
<c>{no_foo, true}</c> in <c>L</c> with <c>{foo, false}</c>,
and any other tuple <c>{no_foo, ...}</c> with
<c>{foo, true}</c>.</p>
<p>See also: <c>get_bool/2</c>, <c>normalize/2</c>,
<c>substitute_aliases/2</c>.</p>
</desc>
</func>
<func>
<name name="unfold" arity="1"/>
<fsummary></fsummary>
<desc>
<p>Unfolds all occurrences of atoms in <c><anno>ListIn</anno></c> to tuples
<c>{Atom, true}</c>.</p>
</desc>
</func>
</funcs>
</erlref>
|