aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdlib/doc/src/maps.xml
blob: e2a62cb397fcbc2b3a3eb227ca92c6c35adc2e76 (plain) (blame)
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">

<erlref>
  <header>
    <copyright>
      <year>2013</year><year>2018</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 since="OTP 17.0">maps</module>
  <modulesummary>Maps processing functions.</modulesummary>
  <description>
    <p>This module contains functions for maps processing.</p>
  </description>

  <datatypes>
    <datatype>
      <name name="iterator"/>
      <desc>
        <p>An iterator representing the key value associations in a map.</p>
        <p>Created using <seealso marker="#iterator-1"><c>maps:iterator/1</c></seealso>.</p>
        <p>Consumed by <seealso marker="#next-1"><c>maps:next/1</c></seealso>,
          <seealso marker="#filter-2"><c>maps:filter/2</c></seealso>,
          <seealso marker="#fold-3"><c>maps:fold/3</c></seealso> and
          <seealso marker="#map-2"><c>maps:map/2</c></seealso>.</p>
      </desc>
    </datatype>
  </datatypes>

  <funcs>
    <func>
      <name name="filter" arity="2" since="OTP 18.0"/>
      <fsummary>Select pairs that satisfy a predicate.</fsummary>
      <desc>
        <p>Returns a map <c><anno>Map</anno></c> for which predicate
          <c><anno>Pred</anno></c> holds true in <c><anno>MapOrIter</anno></c>.</p>
        <p>The call fails with a <c>{badmap,Map}</c> exception if
          <c><anno>MapOrIter</anno></c> is not a map or valid iterator,
          or with <c>badarg</c> if <c><anno>Pred</anno></c> is not a
          function of arity 2.</p>
        <p><em>Example:</em></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" since="OTP 17.0"/>
      <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 fails with a <c>{badmap,Map}</c> exception if
          <c><anno>Map</anno></c> is not a map.</p>
        <p><em>Example:</em></p>
        <code type="none">
> Map = #{"hi" => 42},
  Key = "hi",
  maps:find(Key,Map).
{ok,42}</code>
      </desc>
    </func>

    <func>
      <name name="fold" arity="3" since="OTP 17.0"/>
      <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>MapOrIter</anno></c> in
          any order. Function <c>fun F/3</c> must return a new
          accumulator, which is passed to the next successive call.
          This function 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>The call fails with a <c>{badmap,Map}</c> exception if
          <c><anno>MapOrIter</anno></c> is not a map or valid iterator,
          or with <c>badarg</c> if <c><anno>Fun</anno></c> is not a
          function of arity 3.</p>
        <p><em>Example:</em></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" since="OTP 17.0"/>
      <fsummary></fsummary>
      <desc>
        <p>Takes a list of key-value tuples elements and builds a map. The
          associations can be in any order, and both keys and values in the
          association can be of any term. If the same key appears more than
          once, the latter (right-most) value is used and the previous values
          are ignored.</p>
        <p><em>Example:</em></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" since="OTP 17.0"/>
      <fsummary></fsummary>
      <desc>
        <p>Returns 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 fails 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><em>Example:</em></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" since="OTP 17.1"/>
      <fsummary></fsummary>
      <desc>
        <p>Returns 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>, <c><anno>Default</anno></c> is returned.</p>
        <p>The call fails with a <c>{badmap,Map}</c> exception if
          <c><anno>Map</anno></c> is not a map.</p>
        <p><em>Example:</em></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" since="OTP 17.0"/>
      <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 fails with a <c>{badmap,Map}</c> exception if
          <c><anno>Map</anno></c> is not a map.</p>
        <p><em>Example:</em></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="iterator" arity="1" since="OTP 21.0"/>
      <fsummary>Create a map iterator.</fsummary>
      <desc>
        <p>Returns a map iterator <c><anno>Iterator</anno></c> that can
          be used by <seealso marker="#next-1"><c>maps:next/1</c></seealso>
          to traverse the key-value associations in a map. When iterating
          over a map, the memory usage is guaranteed to be bounded no matter
          the size of the map.</p>
        <p>The call fails with a <c>{badmap,Map}</c> exception if
          <c><anno>Map</anno></c> is not a map.</p>
        <p><em>Example:</em></p>
        <code type="none">
> M = #{ a => 1, b => 2 }.
#{a => 1,b => 2}
> I = maps:iterator(M), ok.
ok
> {K1, V1, I2} = maps:next(I), {K1, V1}.
{a,1}
> {K2, V2, I3} = maps:next(I2),{K2, V2}.
{b,2}
> maps:next(I3).
none</code>
      </desc>
    </func>

    <func>
      <name name="keys" arity="1" since="OTP 17.0"/>
      <fsummary></fsummary>
      <desc>
        <p>Returns a complete list of keys, in any order, which resides
          within <c><anno>Map</anno></c>.</p>
        <p>The call fails with a <c>{badmap,Map}</c> exception if
          <c><anno>Map</anno></c> is not a map.</p>
        <p><em>Example:</em></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" since="OTP 17.0"/>
      <fsummary></fsummary>
      <desc>
        <p>Produces a new map <c><anno>Map</anno></c> by calling 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>MapOrIter</anno></c> in
          any order. Function <c>fun F/2</c> must return value
          <c><anno>V2</anno></c> to be associated with key <c><anno>K</anno></c>
          for the new map <c><anno>Map</anno></c>.</p>
        <p>The call fails with a <c>{badmap,Map}</c> exception if
          <c><anno>MapOrIter</anno></c> is not a map or valid iterator,
          or with <c>badarg</c> if <c><anno>Fun</anno></c> is not a
          function of arity 2.</p>
        <p><em>Example:</em></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" since="OTP 17.0"/>
      <fsummary></fsummary>
      <desc>
        <p>Merges two maps into a single map <c><anno>Map3</anno></c>. If two
          keys exist in both maps, the value in <c><anno>Map1</anno></c> is
          superseded by the value in <c><anno>Map2</anno></c>.</p>
        <p>The call fails 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><em>Example:</em></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" since="OTP 17.0"/>
      <fsummary></fsummary>
      <desc>
        <p>Returns a new empty map.</p>
        <p><em>Example:</em></p>
        <code type="none">
> maps:new().
#{}</code>
      </desc>
    </func>

    <func>
      <name name="next" arity="1" since="OTP 21.0"/>
      <fsummary>Get the next key and value from an iterator.</fsummary>
      <desc>
        <p>Returns the next key-value association in
          <c><anno>Iterator</anno></c> and a new iterator for the
          remaining associations in the iterator.
        </p>
        <p>
          If there are no more associations in the iterator,
          <c>none</c> is returned.
        </p>
        <p><em>Example:</em></p>
        <code type="none">
> Map = #{a => 1, b => 2, c => 3}.
#{a => 1,b => 2,c => 3}
> I = maps:iterator(Map), ok.
ok
> {K1, V1, I1} = maps:next(I), {K1, V1}.
{a,1}
> {K2, V2, I2} = maps:next(I1), {K2, V2}.
{b,2}
> {K3, V3, I3} = maps:next(I2), {K3, V3}.
{c,3}
> maps:next(I3).
none</code>
      </desc>
    </func>

    <func>
      <name name="put" arity="3" since="OTP 17.0"/>
      <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 fails with a <c>{badmap,Map}</c> exception if
          <c><anno>Map1</anno></c> is not a map.</p>
        <p><em>Example:</em></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" since="OTP 17.0"/>
      <fsummary></fsummary>
      <desc>
        <p>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 fails with a <c>{badmap,Map}</c> exception if
          <c><anno>Map1</anno></c> is not a map.</p>
        <p><em>Example:</em></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" since="OTP 17.0"/>
      <fsummary></fsummary>
      <desc>
        <p>Returns the number of key-value associations in
          <c><anno>Map</anno></c>. This operation occurs in constant time.</p>
        <p><em>Example:</em></p>
        <code type="none">
> Map = #{42 => value_two,1337 => "value one","a" => 1},
  maps:size(Map).
3</code>
      </desc>
    </func>

    <func>
      <name name="take" arity="2" since="OTP 19.0"/>
      <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 tuple with the removed <c><anno>Value</anno></c>
	and the new map <c><anno>Map2</anno></c> without key
	<c><anno>Key</anno></c>.  If the key does not exist
	<c>error</c> is returned.
	</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" => "hello", "b" => "world"}.
#{"a" => "hello", "b" => "world"}
> maps:take("a",Map).
{"hello",#{"b" => "world"}}
> maps:take("does not exist",Map).
error</code>
      </desc>
    </func>

    <func>
      <name name="to_list" arity="1" since="OTP 17.0"/>
      <fsummary></fsummary>
      <desc>
        <p>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 fails with a <c>{badmap,Map}</c> exception if
          <c><anno>Map</anno></c> is not a map.</p>
        <p><em>Example:</em></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" since="OTP 17.0"/>
      <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 fails 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><em>Example:</em></p>
        <code type="none">
> Map = #{"a" => 1}.
#{"a" => 1}
> maps:update("a", 42, Map).
#{"a" => 42}</code>
      </desc>
    </func>

    <func>
      <name name="update_with" arity="3" since="OTP 19.0"/>
      <fsummary></fsummary>
      <desc>
        <p>Update a value in a <c><anno>Map1</anno></c> associated
        with <c><anno>Key</anno></c> by calling
        <c><anno>Fun</anno></c> on the old value to get a new
        value. An exception <c>{badkey,<anno>Key</anno>}</c> is
        generated if <c><anno>Key</anno></c> is not present in the
        map.</p>
        <p>Example:</p>
        <code type="none">
> Map = #{"counter" => 1},
  Fun = fun(V) -> V + 1 end,
  maps:update_with("counter",Fun,Map).
#{"counter" => 2}</code>
      </desc>
    </func>

    <func>
      <name name="update_with" arity="4" since="OTP 19.0"/>
      <fsummary></fsummary>
      <desc>
        <p>Update a value in a <c><anno>Map1</anno></c> associated
        with <c><anno>Key</anno></c> by calling
        <c><anno>Fun</anno></c> on the old value to get a new value.
        If <c><anno>Key</anno></c> is not present in
        <c><anno>Map1</anno></c> then <c><anno>Init</anno></c> will be
        associated with <c><anno>Key</anno></c>.
        </p>
        <p>Example:</p>
        <code type="none">
> Map = #{"counter" => 1},
  Fun = fun(V) -> V + 1 end,
  maps:update_with("new counter",Fun,42,Map).
#{"counter" => 1,"new counter" => 42}</code>
            </desc>
        </func>

    <func>
      <name name="values" arity="1" since="OTP 17.0"/>
      <fsummary></fsummary>
      <desc>
        <p>Returns a complete list of values, in arbitrary order, contained in
          map <c>Map</c>.</p>
        <p>The call fails with a <c>{badmap,Map}</c> exception if
          <c><anno>Map</anno></c> is not a map.</p>
        <p><em>Example:</em></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" since="OTP 17.3"/>
      <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> is ignored.</p>
        <p><em>Example:</em></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" since="OTP 17.0"/>
      <fsummary></fsummary>
      <desc>
        <p>Returns a new map <c><anno>Map2</anno></c> without 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> is ignored</p>
        <p><em>Example:</em></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>