summaryrefslogtreecommitdiff
path: root/lib/stdlib/doc/src/maps.xml
blob: 0f58f194217afe0bfcf9360e3318afd8177b7162 (plain)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">

<erlref>
	<header>
		<copyright>
			<year>2013</year><year>2016</year>
			<holder>Ericsson AB. All Rights Reserved.</holder>
		</copyright>
		<legalnotice>
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
		</legalnotice>
		<title>maps</title>
		<prepared>Björn-Egil Dahlberg</prepared>
		<docno>1</docno>
		<date>2014-02-28</date>
		<rev>A</rev>
	</header>
	<module>maps</module>
	<modulesummary>Maps Processing Functions</modulesummary>
	<description>
		<p>This module contains functions for maps processing.</p>
	</description>
	<funcs>

		<func>
			<name name="filter" arity="2"/>
			<fsummary>Choose pairs which satisfy a predicate</fsummary>
			<desc>
				<p>
                    Returns a map <c><anno>Map2</anno></c> for which predicate
                    <c><anno>Pred</anno></c> holds true in <c><anno>Map1</anno></c>.
				</p>
				<p>
                    The call will fail with a <c>{badmap,Map}</c> exception if
                    <c><anno>Map1</anno></c> is not a map or with <c>badarg</c> if
                    <c><anno>Pred</anno></c> is not a function of arity 2.
				</p>
				<p>Example:</p>
				<code type="none">
> M = #{a => 2, b => 3, c=> 4, "a" => 1, "b" => 2, "c" => 4},
  Pred = fun(K,V) -> is_atom(K) andalso (V rem 2) =:= 0 end,
  maps:filter(Pred,M).
#{a => 2,c => 4} </code>
			</desc>
		</func>

		<func>
			<name name="find" arity="2"/>
			<fsummary></fsummary>
			<desc>
				<p>
					Returns a tuple <c>{ok, Value}</c> where <c><anno>Value</anno></c> is the value associated with <c><anno>Key</anno></c>,
					or <c>error</c> if no value is associated with <c><anno>Key</anno></c> in <c><anno>Map</anno></c>. 
				</p>
				<p>
					The call will fail with a <c>{badmap,Map}</c> exception if <c><anno>Map</anno></c> is not a map.
				</p>
				<p>Example:</p>
				<code type="none">
> Map = #{"hi" => 42},
  Key = "hi",
  maps:find(Key,Map).
{ok,42} </code>
			</desc>
		</func>

		<func>
			<name name="fold" arity="3"/>
			<fsummary></fsummary>
			<desc>
				<p>
					Calls <c>F(K, V, AccIn)</c> for every <c><anno>K</anno></c> to value <c><anno>V</anno></c>
					association in <c><anno>Map</anno></c> in
					arbitrary order. The function <c>fun F/3</c> must return a new accumulator
					which is passed to the next successive call. <c>maps:fold/3</c> returns the final
					value of the accumulator. The initial accumulator value <c><anno>Init</anno></c> is returned if
					the map is empty.
				</p>
				<p>Example:</p>
				<code type="none">
> Fun = fun(K,V,AccIn) when is_list(K) -> AccIn + V end,
  Map = #{"k1" => 1, "k2" => 2, "k3" => 3},
  maps:fold(Fun,0,Map).
6</code>
			</desc>
		</func>

		<func>
			<name name="from_list" arity="1"/>
			<fsummary></fsummary>
			<desc>
				<p>
					The function takes a list of key-value tuples elements and builds a
					map. The associations may be in any order and both keys and values in the
					association may be of any term. If the same key appears more than once,
					the latter (rightmost) value is used and the previous values are ignored.
				</p>
				<p>Example:</p>
				<code type="none">
> List = [{"a",ignored},{1337,"value two"},{42,value_three},{"a",1}],
  maps:from_list(List).
#{42 => value_three,1337 => "value two","a" => 1}</code>
			</desc>
		</func>

		<func>
			<name name="get" arity="2"/>
			<fsummary></fsummary>
			<desc>
				<p>
					Returns the value <c><anno>Value</anno></c> associated with <c><anno>Key</anno></c> if
					<c><anno>Map</anno></c> contains <c><anno>Key</anno></c>.
				</p>
				<p>
					The call will fail with a <c>{badmap,Map}</c> exception if <c><anno>Map</anno></c> is not a map,
					or with a <c>{badkey,Key}</c> exception if no value is associated with <c><anno>Key</anno></c>.
				</p>
				<p>Example:</p>
				<code type="none">
> Key = 1337,
  Map = #{42 => value_two,1337 => "value one","a" => 1},
  maps:get(Key,Map).
"value one"</code>
			</desc>
		</func>

		<func>
			<name name="get" arity="3"/>
			<fsummary></fsummary>
			<desc>
				<p>
					Returns the value <c><anno>Value</anno></c> associated with <c><anno>Key</anno></c> if
					<c><anno>Map</anno></c> contains <c><anno>Key</anno></c>.
					If no value is associated with <c><anno>Key</anno></c> then returns <c><anno>Default</anno></c>.
				</p>
				<p>
					The call will fail with a <c>{badmap,Map}</c> exception if <c><anno>Map</anno></c> is not a map.

				</p>
				<p>Example:</p>
				<code type="none">
> Map = #{ key1 => val1, key2 => val2 }.
#{key1 => val1,key2 => val2}
> maps:get(key1, Map, "Default value").
val1
> maps:get(key3, Map, "Default value").
"Default value"</code>
			</desc>
		</func>

		<func>
			<name name="is_key" arity="2"/>
			<fsummary></fsummary>
			<desc>
				<p>
					Returns <c>true</c> if map <c><anno>Map</anno></c> contains <c><anno>Key</anno></c> and returns
					<c>false</c> if it does not contain the <c><anno>Key</anno></c>.
				</p>
				<p>
					The call will fail with a <c>{badmap,Map}</c> exception if <c><anno>Map</anno></c> is not a map.
				</p>
				<p>Example:</p>
				<code type="none">
> Map = #{"42" => value}.
#{"42"> => value}
> maps:is_key("42",Map).
true
> maps:is_key(value,Map).
false</code>
			</desc>
		</func>

		<func>
			<name name="keys" arity="1"/>
			<fsummary></fsummary>
			<desc>
				<p>
					Returns a complete list of keys, in arbitrary order, which resides within <c><anno>Map</anno></c>.
				</p>
				<p>
					The call will fail with a <c>{badmap,Map}</c> exception if <c><anno>Map</anno></c> is not a map.
				</p>
				<p>Example:</p>
				<code type="none">
> Map = #{42 => value_three,1337 => "value two","a" => 1},
  maps:keys(Map).
[42,1337,"a"]</code>
			</desc>
		</func>

		<func>
			<name name="map" arity="2"/>
			<fsummary></fsummary>
			<desc>
				<p>
					The function produces a new map <c><anno>Map2</anno></c> by calling the function <c>fun F(K, V1)</c> for
					every <c><anno>K</anno></c> to value <c><anno>V1</anno></c> association in <c><anno>Map1</anno></c> in arbitrary order.
					The function <c>fun F/2</c> must return the value <c><anno>V2</anno></c> to be associated with key <c><anno>K</anno></c> for
					the new map <c><anno>Map2</anno></c>.
				</p>
				<p>Example:</p>
				<code type="none">
> Fun = fun(K,V1) when is_list(K) -> V1*2 end,
  Map = #{"k1" => 1, "k2" => 2, "k3" => 3},
  maps:map(Fun,Map).
#{"k1" => 2,"k2" => 4,"k3" => 6}</code>
			</desc>
		</func>

		<func>
			<name name="merge" arity="2"/>
			<fsummary></fsummary>
			<desc>
				<p>
					Merges two maps into a single map <c><anno>Map3</anno></c>. If two keys exists in both maps the
					value in <c><anno>Map1</anno></c> will be superseded by the value in <c><anno>Map2</anno></c>.
				</p>
				<p>
					The call will fail with a <c>{badmap,Map}</c> exception if <c><anno>Map1</anno></c> or
					<c><anno>Map2</anno></c> is not a map.
				</p>
				<p>Example:</p>
				<code type="none">
> Map1 = #{a => "value_one", b => "value_two"},
  Map2 = #{a => 1, c => 2},
  maps:merge(Map1,Map2).
#{a => 1,b => "value_two",c => 2}</code>
			</desc>
		</func>

		<func>
			<name name="new" arity="0"/>
			<fsummary></fsummary>
			<desc>
				<p>
					Returns a new empty map.
				</p>
				<p>Example:</p>
				<code type="none">
> maps:new().
#{}</code>
			</desc>
		</func>

		<func>
			<name name="put" arity="3"/>
			<fsummary></fsummary>
			<desc>
				<p>
					Associates <c><anno>Key</anno></c> with value <c><anno>Value</anno></c> and inserts the association into map <c>Map2</c>.
					If key <c><anno>Key</anno></c> already exists in map <c><anno>Map1</anno></c>, the old associated value is
					replaced by value <c><anno>Value</anno></c>. The function returns a new map <c><anno>Map2</anno></c> containing the new association and
					the old associations in <c><anno>Map1</anno></c>.
				</p>
				<p>
					The call will fail with a <c>{badmap,Map}</c> exception if <c><anno>Map1</anno></c> is not a map.
				</p>

				<p>Example:</p>
				<code type="none">
> Map = #{"a" => 1}.
#{"a" => 1}
> maps:put("a", 42, Map).
#{"a" => 42}
> maps:put("b", 1337, Map).
#{"a" => 1,"b" => 1337}</code>
			</desc>
		</func>

		<func>
			<name name="remove" arity="2"/>
			<fsummary></fsummary>
			<desc>
				<p>
					The function removes the <c><anno>Key</anno></c>, if it exists, and its associated value from
					<c><anno>Map1</anno></c> and returns a new map <c><anno>Map2</anno></c> without key <c><anno>Key</anno></c>.
				</p>
				<p>
					The call will fail with a <c>{badmap,Map}</c> exception if <c><anno>Map1</anno></c> is not a map.
				</p>
				<p>Example:</p>
				<code type="none">
> Map = #{"a" => 1}.
#{"a" => 1}
> maps:remove("a",Map).
#{}
> maps:remove("b",Map).
#{"a" => 1}</code>
			</desc>
		</func>

		<func>
			<name name="size" arity="1"/>
			<fsummary></fsummary>
			<desc>
				<p>
					The function returns the number of key-value associations in the <c><anno>Map</anno></c>.
					This operation happens in constant time.
				</p>
				<p>Example:</p>
				<code type="none">
> Map = #{42 => value_two,1337 => "value one","a" => 1},
  maps:size(Map).
3</code>
			</desc>
		</func>

		<func>
			<name name="to_list" arity="1"/>
			<fsummary></fsummary>
			<desc>
				<p>
					The fuction returns a list of pairs representing the key-value associations of <c><anno>Map</anno></c>,
					where the pairs, <c>[{K1,V1}, ..., {Kn,Vn}]</c>, are returned in arbitrary order.
				</p>
				<p>
					The call will fail with a <c>{badmap,Map}</c> exception if <c><anno>Map</anno></c> is not a map.
				</p>
				<p>Example:</p>
				<code type="none">
> Map = #{42 => value_three,1337 => "value two","a" => 1},
  maps:to_list(Map).
[{42,value_three},{1337,"value two"},{"a",1}]</code>
			</desc>
		</func>

		<func>
			<name name="update" arity="3"/>
			<fsummary></fsummary>
			<desc>
				<p>
					If <c><anno>Key</anno></c> exists in <c><anno>Map1</anno></c> the old associated value is
					replaced by value <c><anno>Value</anno></c>. The function returns a new map <c><anno>Map2</anno></c> containing
					the new associated value.
				</p>
				<p>
					The call will fail with a <c>{badmap,Map}</c> exception if <c><anno>Map1</anno></c> is not a map,
					or with a <c>{badkey,Key}</c> exception if no value is associated with <c><anno>Key</anno></c>.
				</p>
				<p>Example:</p>
				<code type="none">
> Map = #{"a" => 1}.
#{"a" => 1}
> maps:update("a", 42, Map).
#{"a" => 42}</code>
			</desc>
		</func>

		<func>
			<name name="values" arity="1"/>
			<fsummary></fsummary>
			<desc>
				<p>
					Returns a complete list of values, in arbitrary order, contained in map <c>Map</c>.
				</p>
				<p>
					The call will fail with a <c>{badmap,Map}</c> exception if <c><anno>Map</anno></c> is not a map.
				</p>
				<p>Example:</p>
				<code type="none">
> Map = #{42 => value_three,1337 => "value two","a" => 1},
  maps:values(Map).
[value_three,"value two",1]</code>
			</desc>
		</func>

		<func>
			<name name="with" arity="2"/>
			<fsummary></fsummary>
			<desc>
				<p>
					Returns a new map <c><anno>Map2</anno></c> with the keys <c>K1</c> through <c>Kn</c> and their associated values from map <c><anno>Map1</anno></c>.
					Any key in <c><anno>Ks</anno></c> that does not exist in <c><anno>Map1</anno></c> are ignored.
				</p>
				<p>Example:</p>
				<code type="none">
> Map = #{42 => value_three,1337 => "value two","a" => 1},
  Ks = ["a",42,"other key"],
  maps:with(Ks,Map).
#{42 => value_three,"a" => 1}</code>
			</desc>
		</func>

		<func>
			<name name="without" arity="2"/>
			<fsummary></fsummary>
			<desc>
				<p>
					Returns a new map <c><anno>Map2</anno></c> without the keys <c>K1</c> through <c>Kn</c> and their associated values from map <c><anno>Map1</anno></c>.
					Any key in <c><anno>Ks</anno></c> that does not exist in <c><anno>Map1</anno></c> are ignored.
				</p>
				<p>Example:</p>
				<code type="none">
> Map = #{42 => value_three,1337 => "value two","a" => 1},
  Ks = ["a",42,"other key"],
  maps:without(Ks,Map).
#{1337 => "value two"}</code>
			</desc>
		</func>
	</funcs>
</erlref>