maps
(stdlib)Maps processing functions.
This module contains functions for maps processing.
Types
iterator()
An iterator representing the key value associations in a map.
Created using maps:iterator/1
.
Consumed by maps:next/1
,
maps:filter/2
,
maps:fold/3
and
maps:map/2
.
Functions
filter(Pred, MapOrIter) -> Map
Pred = fun((Key, Value) -> boolean())
Key = Value = term()
MapOrIter = map() | iterator()
Map = map()
Returns a map
for which predicate
holds true in
.
The call fails with a {badmap,Map}
exception if
is not a map or valid iterator,
or with badarg
if
is not a
function of arity 2.
Example:
> 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}
find(Key, Map) -> {ok, Value} | error
Key = term()
Map = map()
Value = term()
Returns a tuple {ok, Value}
, where
is the value associated with
, or error
if no value is associated with
in
.
The call fails with a {badmap,Map}
exception if
is not a map.
Example:
> Map = #{"hi" => 42}, Key = "hi", maps:find(Key,Map). {ok,42}
fold(Fun, Init, MapOrIter) -> Acc
Fun = fun((K, V, AccIn) -> AccOut)
Init = Acc = AccIn = AccOut = term()
MapOrIter = map() | iterator()
K = V = term()
Calls F(K, V, AccIn)
for every
to value
association in
in
any order. Function fun F/3
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
is returned if the map is
empty.
The call fails with a {badmap,Map}
exception if
is not a map or valid iterator,
or with badarg
if
is not a
function of arity 3.
Example:
> 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
from_list(List) -> Map
List = [{Key, Value}]
Key = Value = term()
Map = map()
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.
Example:
> List = [{"a",ignored},{1337,"value two"},{42,value_three},{"a",1}], maps:from_list(List). #{42 => value_three,1337 => "value two","a" => 1}
get(Key, Map) -> Value
Key = term()
Map = map()
Value = term()
Returns value
associated with
if
contains
.
The call fails with a {badmap,Map}
exception if
is not a map, or with a {badkey,Key}
exception if no value is associated with
.
Example:
> Key = 1337, Map = #{42 => value_two,1337 => "value one","a" => 1}, maps:get(Key,Map). "value one"
get(Key, Map, Default) -> Value | Default
Key = term()
Map = map()
Value = Default = term()
Returns value
associated with
if
contains
. If no value is associated with
,
is returned.
The call fails with a {badmap,Map}
exception if
is not a map.
Example:
> Map = #{ key1 => val1, key2 => val2 }. #{key1 => val1,key2 => val2} > maps:get(key1, Map, "Default value"). val1 > maps:get(key3, Map, "Default value"). "Default value"
is_key(Key, Map) -> boolean()
Key = term()
Map = map()
Returns true
if map
contains
and returns false
if it does not
contain the
.
The call fails with a {badmap,Map}
exception if
is not a map.
Example:
> Map = #{"42" => value}. #{"42" => value} > maps:is_key("42",Map). true > maps:is_key(value,Map). false
iterator(Map) -> Iterator
Map = map()
Iterator = iterator()
Returns a map iterator
that can
be used by maps:next/1
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.
The call fails with a {badmap,Map}
exception if
is not a map.
Example:
> M = #{ a => 1, b => 2 }. #{a => 1,b => 2} > I = maps:iterator(M). [{a,1},{b,2}] > {K1, V1, I2} = maps:next(I). {a,1,[{b,2}]} > {K2, V2, I3} = maps:next(I2). {b,2,[]} > maps:next(I3). none
keys(Map) -> Keys
Map = map()
Keys = [Key]
Key = term()
Returns a complete list of keys, in any order, which resides
within
.
The call fails with a {badmap,Map}
exception if
is not a map.
Example:
> Map = #{42 => value_three,1337 => "value two","a" => 1}, maps:keys(Map). [42,1337,"a"]
map(Fun, MapOrIter) -> Map
Fun = fun((K, V1) -> V2)
MapOrIter = map() | iterator()
Map = map()
K = V1 = V2 = term()
Produces a new map
by calling function
fun F(K, V1)
for every
to value
association in
in
any order. Function fun F/2
must return value
to be associated with key
for the new map
.
The call fails with a {badmap,Map}
exception if
is not a map or valid iterator,
or with badarg
if
is not a
function of arity 2.
Example:
> 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}
merge(Map1, Map2) -> Map3
Map1 = Map2 = Map3 = map()
Merges two maps into a single map
. If two
keys exist in both maps, the value in
is
superseded by the value in
.
The call fails with a {badmap,Map}
exception if
or
is not a map.
Example:
> Map1 = #{a => "value_one", b => "value_two"}, Map2 = #{a => 1, c => 2}, maps:merge(Map1,Map2). #{a => 1,b => "value_two",c => 2}
new() -> Map
Map = map()
Returns a new empty map.
Example:
> maps:new(). #{}
next(Iterator) -> {Key, Value, NextIterator} | none
Iterator = iterator()
Key = Value = term()
NextIterator = iterator()
Returns the next key-value association in
and a new iterator for the
remaining associations in the iterator.
If there are no more associations in the iterator,
none
is returned.
Example:
> Map = #{a => 1, b => 2, c => 3}. #{a => 1,b => 2,c => 3} > Iter = maps:iterator(Map). [{a,1},{b,2},{c,3}] > {_, _, Iter1} = maps:next(Iter). {a,1,[{b,2},{c,3}]} > {_, _, Iter2} = maps:next(Iter1). {b,2,[{c,3}]} > {_, _, Iter3} = maps:next(Iter2). {c,3,[]} > maps:next(Iter3). none
put(Key, Value, Map1) -> Map2
Key = Value = term()
Map1 = Map2 = map()
Associates
with value
and inserts the association into map
Map2
. If key
already exists in map
, the old associated value is replaced by
value
. The function returns a new map
containing the new association and the old
associations in
.
The call fails with a {badmap,Map}
exception if
is not a map.
Example:
> Map = #{"a" => 1}. #{"a" => 1} > maps:put("a", 42, Map). #{"a" => 42} > maps:put("b", 1337, Map). #{"a" => 1,"b" => 1337}
remove(Key, Map1) -> Map2
Key = term()
Map1 = Map2 = map()
Removes the
, if it exists, and its
associated value from
and returns a new map
without key
.
The call fails with a {badmap,Map}
exception if
is not a map.
Example:
> Map = #{"a" => 1}. #{"a" => 1} > maps:remove("a",Map). #{} > maps:remove("b",Map). #{"a" => 1}
size(Map) -> integer() >= 0
Map = map()
Returns the number of key-value associations in
. This operation occurs in constant time.
Example:
> Map = #{42 => value_two,1337 => "value one","a" => 1}, maps:size(Map). 3
take(Key, Map1) -> {Value, Map2} | error
Key = term()
Map1 = map()
Value = term()
Map2 = map()
The function removes the
, if it
exists, and its associated value from
and returns a tuple with the removed
and the new map
without key
. If the key does not exist
error
is returned.
The call will fail with a {badmap,Map}
exception if
is not a map.
Example:
> Map = #{"a" => "hello", "b" => "world"}. #{"a" => "hello", "b" => "world"} > maps:take("a",Map). {"hello",#{"b" => "world"}} > maps:take("does not exist",Map). error
to_list(Map) -> [{Key, Value}]
Map = map()
Key = Value = term()
Returns a list of pairs representing the key-value associations of
, where the pairs
[{K1,V1}, ..., {Kn,Vn}]
are returned in arbitrary order.
The call fails with a {badmap,Map}
exception if
is not a map.
Example:
> Map = #{42 => value_three,1337 => "value two","a" => 1}, maps:to_list(Map). [{42,value_three},{1337,"value two"},{"a",1}]
update(Key, Value, Map1) -> Map2
Key = Value = term()
Map1 = Map2 = map()
If
exists in
, the
old associated value is replaced by value
.
The function returns a new map
containing
the new associated value.
The call fails with a {badmap,Map}
exception if
is not a map, or with a {badkey,Key}
exception if no value is associated with
.
Example:
> Map = #{"a" => 1}. #{"a" => 1} > maps:update("a", 42, Map). #{"a" => 42}
update_with(Key, Fun, Map1) -> Map2
Key = term()
Map1 = Map2 = map()
Fun = fun((Value1 :: term()) -> Value2 :: term())
Update a value in a
associated
with
by calling
on the old value to get a new
value. An exception {badkey,
is
generated if
is not present in the
map.
Example:
> Map = #{"counter" => 1}, Fun = fun(V) -> V + 1 end, maps:update_with("counter",Fun,Map). #{"counter" => 2}
update_with(Key, Fun, Init, Map1) -> Map2
Key = term()
Map1 = Map1
Map2 = Map2
Fun = fun((Value1 :: term()) -> Value2 :: term())
Init = term()
Update a value in a
associated
with
by calling
on the old value to get a new value.
If
is not present in
then
will be
associated with
.
Example:
> Map = #{"counter" => 1}, Fun = fun(V) -> V + 1 end, maps:update_with("new counter",Fun,42,Map). #{"counter" => 1,"new counter" => 42}
values(Map) -> Values
Map = map()
Values = [Value]
Value = term()
Returns a complete list of values, in arbitrary order, contained in
map Map
.
The call fails with a {badmap,Map}
exception if
is not a map.
Example:
> Map = #{42 => value_three,1337 => "value two","a" => 1}, maps:values(Map). [value_three,"value two",1]
with(Ks, Map1) -> Map2
Ks = [K]
Map1 = Map2 = map()
K = term()
Returns a new map
with the keys K1
through Kn
and their associated values from map
. Any key in
that does
not exist in
is ignored.
Example:
> Map = #{42 => value_three,1337 => "value two","a" => 1}, Ks = ["a",42,"other key"], maps:with(Ks,Map). #{42 => value_three,"a" => 1}
without(Ks, Map1) -> Map2
Ks = [K]
Map1 = Map2 = map()
K = term()
Returns a new map
without keys K1
through Kn
and their associated values from map
. Any key in
that does
not exist in
is ignored
Example:
> Map = #{42 => value_three,1337 => "value two","a" => 1}, Ks = ["a",42,"other key"], maps:without(Ks,Map). #{1337 => "value two"}