StaticMap v0.1.1 StaticMap View Source
Link to this section Summary
Link to this section Functions
Defines a mapping function.
use StaticMap
defmap MyMap, [a: 1, b: 2]
This will create a module MyMap
in the current context, representing a statically-compiled version of the map.
The API of the generated module closely resembles that of Enum
, though the module does not implement Enumerable
.
Examples
Get the map itself:
iex> MyMap.to_map
%{a: 1, b: 2}
Get the map as a list:
iex> MyMap.to_list
[a: 1, b: 2]
Get values from the map, with a default of nil
(ala Map.get/1
):
iex> MyMap.get(:a)
1
iex> MyMap.get(:c)
nil
Fetch values from the map (ala Map.fetch/1
):
iex> MyMap.fetch(:a)
{:ok, 1}
iex> MyMap.fetch(:c)
:error
Get values from the map or raise (ala Map.fetch!/1
):
iex> MyMap.fetch!(:a)
1
iex> MyMap.fetch!(:c)
** (FunctionClauseError) no function clause matching in StaticMapTest.MyMap.fetch!/1
Test for membership (ala Map.has_key?/1
):
iex> MyMap.has_key?(:a)
true
iex> MyMap.has_key?(:c)
false
Get keys (ala Map.keys/1
), but as a compile-time-generated MapSet
:
iex> MyMap.keys_set
#MapSet<[:a, :b]>
Get values (ala Map.values/1
), but as a compile-time-generated MapSet
:
iex> MyMap.values_set
#MapSet<[1, 2]>