1proplists(3) Erlang Module Definition proplists(3)
2
3
4
6 proplists - Support functions for property lists.
7
9 Property lists are ordinary lists containing entries in the form of
10 either tuples, whose first elements are keys used for lookup and inser‐
11 tion, or atoms, which work as shorthand for tuples {Atom, true}. (Other
12 terms are allowed in the lists, but are ignored by this module.) If
13 there is more than one entry in a list for a certain key, the first
14 occurrence normally overrides any later (irrespective of the arity of
15 the tuples).
16
17 Property lists are useful for representing inherited properties, such
18 as options passed to a function where a user can specify options over‐
19 riding the default settings, object properties, annotations, and so on.
20
21 Two keys are considered equal if they match (=:=). That is, numbers are
22 compared literally rather than by value, so that, for example, 1 and
23 1.0 are different keys.
24
26 property() = atom() | tuple()
27
29 append_values(Key, ListIn) -> ListOut
30
31 Types:
32
33 Key = term()
34 ListIn = ListOut = [term()]
35
36 Similar to get_all_values/2, but each value is wrapped in a list
37 unless it is already itself a list. The resulting list of lists
38 is concatenated. This is often useful for "incremental" options.
39
40 Example:
41
42 append_values(a, [{a, [1,2]}, {b, 0}, {a, 3}, {c, -1}, {a, [4]}])
43
44 returns:
45
46 [1,2,3,4]
47
48 compact(ListIn) -> ListOut
49
50 Types:
51
52 ListIn = ListOut = [property()]
53
54 Minimizes the representation of all entries in the list. This is
55 equivalent to [property(P) || P <- ListIn].
56
57 See also property/1, unfold/1.
58
59 delete(Key, List) -> List
60
61 Types:
62
63 Key = term()
64 List = [term()]
65
66 Deletes all entries associated with Key from List.
67
68 expand(Expansions, ListIn) -> ListOut
69
70 Types:
71
72 Expansions = [{Property :: property(), Expansion ::
73 [term()]}]
74 ListIn = ListOut = [term()]
75
76 Expands particular properties to corresponding sets of proper‐
77 ties (or other terms). For each pair {Property, Expansion} in
78 Expansions: if E is the first entry in ListIn with the same key
79 as Property, and E and Property have equivalent normal forms,
80 then E is replaced with the terms in Expansion, and any follow‐
81 ing entries with the same key are deleted from ListIn.
82
83 For example, the following expressions all return [fie, bar,
84 baz, fum]:
85
86 expand([{foo, [bar, baz]}], [fie, foo, fum])
87 expand([{{foo, true}, [bar, baz]}], [fie, foo, fum])
88 expand([{{foo, false}, [bar, baz]}], [fie, {foo, false}, fum])
89
90 However, no expansion is done in the following call because
91 {foo, false} shadows foo:
92
93 expand([{{foo, true}, [bar, baz]}], [{foo, false}, fie, foo, fum])
94
95 Notice that if the original property term is to be preserved in
96 the result when expanded, it must be included in the expansion
97 list. The inserted terms are not expanded recursively. If Expan‐
98 sions contains more than one property with the same key, only
99 the first occurrence is used.
100
101 See also normalize/2.
102
103 get_all_values(Key, List) -> [term()]
104
105 Types:
106
107 Key = term()
108 List = [term()]
109
110 Similar to get_value/2, but returns the list of values for all
111 entries {Key, Value} in List. If no such entry exists, the
112 result is the empty list.
113
114 get_bool(Key, List) -> boolean()
115
116 Types:
117
118 Key = term()
119 List = [term()]
120
121 Returns the value of a boolean key/value option. If lookup(Key,
122 List) would yield {Key, true}, this function returns true, oth‐
123 erwise false.
124
125 See also get_value/2, lookup/2.
126
127 get_keys(List) -> [term()]
128
129 Types:
130
131 List = [term()]
132
133 Returns an unordered list of the keys used in List, not contain‐
134 ing duplicates.
135
136 get_value(Key, List) -> term()
137
138 Types:
139
140 Key = term()
141 List = [term()]
142
143 Equivalent to get_value(Key, List, undefined).
144
145 get_value(Key, List, Default) -> term()
146
147 Types:
148
149 Key = term()
150 List = [term()]
151 Default = term()
152
153 Returns the value of a simple key/value property in List. If
154 lookup(Key, List) would yield {Key, Value}, this function
155 returns the corresponding Value, otherwise Default.
156
157 See also get_all_values/2, get_bool/2, get_value/2, lookup/2.
158
159 is_defined(Key, List) -> boolean()
160
161 Types:
162
163 Key = term()
164 List = [term()]
165
166 Returns true if List contains at least one entry associated with
167 Key, otherwise false.
168
169 lookup(Key, List) -> none | tuple()
170
171 Types:
172
173 Key = term()
174 List = [term()]
175
176 Returns the first entry associated with Key in List, if one
177 exists, otherwise returns none. For an atom A in the list, the
178 tuple {A, true} is the entry associated with A.
179
180 See also get_bool/2, get_value/2, lookup_all/2.
181
182 lookup_all(Key, List) -> [tuple()]
183
184 Types:
185
186 Key = term()
187 List = [term()]
188
189 Returns the list of all entries associated with Key in List. If
190 no such entry exists, the result is the empty list.
191
192 See also lookup/2.
193
194 normalize(ListIn, Stages) -> ListOut
195
196 Types:
197
198 ListIn = [term()]
199 Stages = [Operation]
200 Operation =
201 {aliases, Aliases} |
202 {negations, Negations} |
203 {expand, Expansions}
204 Aliases = Negations = [{Key, Key}]
205 Expansions = [{Property :: property(), Expansion ::
206 [term()]}]
207 ListOut = [term()]
208
209 Passes ListIn through a sequence of substitution/expansion
210 stages. For an aliases operation, function substitute_aliases/2
211 is applied using the specified list of aliases:
212
213 * For a negations operation, substitute_negations/2 is applied
214 using the specified negation list.
215
216 * For an expand operation, function expand/2 is applied using
217 the specified list of expansions.
218
219 The final result is automatically compacted (compare compact/1).
220
221 Typically you want to substitute negations first, then aliases,
222 then perform one or more expansions (sometimes you want to pre-
223 expand particular entries before doing the main expansion). You
224 might want to substitute negations and/or aliases repeatedly, to
225 allow such forms in the right-hand side of aliases and expansion
226 lists.
227
228 See also substitute_negations/2.
229
230 property(PropertyIn) -> PropertyOut
231
232 Types:
233
234 PropertyIn = PropertyOut = property()
235
236 Creates a normal form (minimal) representation of a property. If
237 PropertyIn is {Key, true}, where Key is an atom, Key is
238 returned, otherwise the whole term PropertyIn is returned.
239
240 See also property/2.
241
242 property(Key, Value) -> Property
243
244 Types:
245
246 Key = Value = term()
247 Property = atom() | {term(), term()}
248
249 Creates a normal form (minimal) representation of a simple
250 key/value property. Returns Key if Value is true and Key is an
251 atom, otherwise a tuple {Key, Value} is returned.
252
253 See also property/1.
254
255 split(List, Keys) -> {Lists, Rest}
256
257 Types:
258
259 List = Keys = [term()]
260 Lists = [[term()]]
261 Rest = [term()]
262
263 Partitions List into a list of sublists and a remainder. Lists
264 contains one sublist for each key in Keys, in the corresponding
265 order. The relative order of the elements in each sublist is
266 preserved from the original List. Rest contains the elements in
267 List that are not associated with any of the specified keys,
268 also with their original relative order preserved.
269
270 Example:
271
272 split([{c, 2}, {e, 1}, a, {c, 3, 4}, d, {b, 5}, b], [a, b, c])
273
274 returns:
275
276 {[[a], [{b, 5}, b],[{c, 2}, {c, 3, 4}]], [{e, 1}, d]}
277
278 substitute_aliases(Aliases, ListIn) -> ListOut
279
280 Types:
281
282 Aliases = [{Key, Key}]
283 Key = term()
284 ListIn = ListOut = [term()]
285
286 Substitutes keys of properties. For each entry in ListIn, if it
287 is associated with some key K1 such that {K1, K2} occurs in
288 Aliases, the key of the entry is changed to K2. If the same K1
289 occurs more than once in Aliases, only the first occurrence is
290 used.
291
292 For example, substitute_aliases([{color, colour}], L) replaces
293 all tuples {color, ...} in L with {colour, ...}, and all atoms
294 color with colour.
295
296 See also normalize/2, substitute_negations/2.
297
298 substitute_negations(Negations, ListIn) -> ListOut
299
300 Types:
301
302 Negations = [{Key1, Key2}]
303 Key1 = Key2 = term()
304 ListIn = ListOut = [term()]
305
306 Substitutes keys of boolean-valued properties and simultaneously
307 negates their values. For each entry in ListIn, if it is associ‐
308 ated with some key K1 such that {K1, K2} occurs in Negations: if
309 the entry was {K1, true}, it is replaced with {K2, false}, oth‐
310 erwise with {K2, true}, thus changing the name of the option and
311 simultaneously negating the value specified by get_bool(Key,
312 ListIn). If the same K1 occurs more than once in Negations, only
313 the first occurrence is used.
314
315 For example, substitute_negations([{no_foo, foo}], L) replaces
316 any atom no_foo or tuple {no_foo, true} in L with {foo, false},
317 and any other tuple {no_foo, ...} with {foo, true}.
318
319 See also get_bool/2, normalize/2, substitute_aliases/2.
320
321 unfold(ListIn) -> ListOut
322
323 Types:
324
325 ListIn = ListOut = [term()]
326
327 Unfolds all occurrences of atoms in ListIn to tuples {Atom,
328 true}.
329
330
331
332Ericsson AB stdlib 3.4.5.1 proplists(3)