Erlang – Maps

Erlang maps

In this guide, we will discuss Erlang Maps. A map is a compound data type with a variable number of key-value associations. Each key-value association in the map is called an association pair. The key and value parts of the pair are called elements. The number of association pairs is said to be the size of the map.

An example of how the Map data type can be used is shown in the following program.

Here we are defining a Map M1 which has 2 mappings. The map_size is an inbuilt function defined in Erlang which can be used to determine the size of the map.

Example

-module(helloworld). 
-export([start/0]). 

start() -> 
   M1 = #{name=>john,age=>25}, 
   io:fwrite("~w",[map_size(M1)]).

The output of the above program will be as follows.

Output

2

Some of the other methods available for maps are as follows.

Sr.No.Methods & Description
1from_list
This method is used to generate a map from a list.
2find
This method is used to find if a particular key exists in the map.
3get
This method is used to get the value of a particular key in the map.
4is_key
This method is used to determine if a particular key is defined as a key in the map.
5keys
This method is used to return all the keys from a map.
6merge
This method is used to merge 2 maps.
7put
This method is used to add a key value pair to the map.
8values
This method is used to return all the values from a map.
9remove
This method is used to remove a key value from the map.

Next Topic : Click Here

This Post Has One Comment

Leave a Reply