Map module
The Map
module contains functions to manipulate maps (dicts). The keys and values can be of any type.
new/0
Map.new() # empty map
Returns an empty map. There is no shorthand notation.
size/1
Map.size(key1: "value", key2: "value2") # 2
Parameters:
map
the map
Returns the number of keys in map
.
get/2
Map.get([key1: "value", key2: "value2"], "key1") # "value"
Map.get([key1: "value", key2: "value2"], "key3") # nil
Parameters:
map
the mapkey
the key
Returns the value at key
in map
or nil
if key
is not in the map
.
tip
There is a shorthand notation map.key
when key is not a variable.
get/3
Map.get([key1: "value", key2: "value2"], "key1", "not in map") # "value"
Map.get([key1: "value", key2: "value2"], "key3", "not in map") # "not in map"
Parameters:
map
the mapkey
the keydefault
the default value
Returns the value at key
in map
or default
if key
is not in the map
.
set/3
Map.set([key1: "value", key2: "value2"], "key1", "valueX") # [key1: "valueX", key2: "value2"]
Map.get([key1: "value", key2: "value2"], "key3", "value3") # [key1: "value", key2: "value2, key3: "value3"]
Parameters:
map
the mapkey
the keyvalue
the value to set
Returns a copy of map
where the value at key
is value
.
keys/1
Map.keys(key1: "value", key2: "value2") # ["key1", "key2"]
Parameters:
map
the map
Returns the list of keys in map
.
tip
Use it with a for loop
to iterate on a map!
for key in Map.keys(map) do
...
value = map[key]
...
end
values/1
Map.values(key1: "value", key2: "value2") # ["value", "value2"]
Parameters:
map
the map
Returns the list of values in map
.
delete/2
Map.delete([key1: "value", key2: "value2"], "key2") # [key1: "value"]
Parameters:
map
the mapkey
the key to remove
Returns a copy of the map
without the value at given key
.