Map group Map in Erlang details _erlang

Source: Internet
Author: User

The main problem with map matching is to recall the map group map in Erlang, which is called hash hash or Dict dictionary in other languages.

Erlang supports mapping groups starting with the R17 version

Create a mapping group

The mapping group in Erlang is represented by a struct #{} that creates a mapping group that can be

Copy Code code as follows:

% no matter how you sort, the end result is in the dictionary order of the Keys
#{name => "Wittyfox", age => 19}.
% => #{age => 20,name => "Wittyfox"}

% You can also create an empty mapping group
#{}.
% => #{}

Update a mapping group

The mapping group can be updated, and the so-called update is to create a new mapping group, because the variables in Erlang are immutable.

Copy Code code as follows:

% now I
Me = #{name => "Wittyfox", age => 19}.
% => #{age => 19,name => "Wittyfox"}

% Chinese New Year, another one year old, become a brand-new Me
Newme = me#{Age => 20}.
% => #{age => 20,name => "Wittyfox"}

% of course can also be directly modified
#{name => "Wittyfox", age =>}#{age => 20}.
% => #{age => 20,name => "Wittyfox"}

=> is used to create or update a map, update it if the key exists, or create a new mapping. If a key is accidentally spelled incorrectly, Oops.

Copy Code code as follows:

% would have wanted to update the age, the result accidentally misspelled, created a new mapping
me#{AEG => 20}.
% => #{aeg => 20,age => 19,name => "Wittyfox"}.

To avoid this, there is also a way to update the mapping using: =, which can only be used to update the mappings, not create new mappings, and throw a Badarg exception if the key does not exist.
Copy Code code as follows:

% does not exist AEG key, throw Badarg exception
me#{AEG: = 20}.
% * * * Exception Error:bad argument ... BlaBla

% only existing mappings can be updated
me#{Age: = 20}.
% => #{age => 20,name => "Wittyfox"}

The difference between two kinds of operators

1.=> can be used to update mappings or create new mappings
2.:= can only update mappings and throw exceptions when the key does not exist
So there's the following summary

When you create a mapping group

You can only use =>,:= to update mappings and not to create new mappings, and you need to create several mappings when creating a mapping group

Copy Code code as follows:

#{Name: = "Wittyfox", Age: = 19}.
% * 1:only Association operators ' => ' are allowed in map construction

Mapping group matches the

Only:=,=> on the left can create a new mapping when the key does not exist, and the mapping group match can be partially matched (only to the part of the left), so the match is meaningless

Copy Code code as follows:

% partial match: We just want to take out the age, so we're only interested in the argument that there is not a map
#{Age: = age} = Me.
% => #{age => 19,name => "Wittyfox"}

% age.
% => 19

% Non-legal match
#{Age => Age} = Me.
% * 1:illegal pattern


in order to better find the wrong

Use => only if you are creating a mapping group or when you explicitly need to create a new mapping, and are used on other occasions: =

Copy Code code as follows:

% here is to create a mapping group that can only use =>
New ()->
{OK, {? MODULE, #{name => "Wittyfox", Age => 19}}.

% here is match, only use: =
Show ({?) MODULE, #{name: = name, Age: = Age}})->
Io:format (' Name: ~p, Age: ~p~n ', [name, Age]).


Attention

The update mapping above, creating a new mapping and matching can be for multiple mappings at the same time, only as an example to select only a pair of mappings.

Mapping Group Operations

The maps module in Erlang is used to manipulate the mapping group

Mapping group creation and properties

Copy Code code as follows:

% Create Mapping Group
Maps:new ().
% => #{}

% return all keys
Maps:keys (Me).
% => [Age,name]

% to determine if there is a key
Maps:is_key (age, Me).
% => True
Maps:is_key (AEG, Me).
% => False

% key in order to return all values
Maps:values (Me).
% =>[19, "Wittyfox"]

% Map Quantity
Maps:size (Me).
% => 2

% can also use ERLANG:MAP_SIZE/1
% This function can be used within the Guard,maps module as well.
Map_size (Me).
% => 2

Mapping add, delete, get

Copy Code code as follows:

% MAPS:GET/2 throws an exception if the key does not exist
Maps:get (age, Me).
% => 19

% MAPS:GET/3 Returns the value of the third parameter when the key does not exist
Maps:get (AEG, Me, 20).
% => 20

% for updating or creating mappings, similar to =>
% of the so-called update, just return the updated new mapping group, the original mapping group does not change
Maps:put (gender, Male, Me).
% => #{age => 19,gender => male,name => "Wittyfox"}

% for update mappings, similar to: =, throw Badarg exception if key does not exist
Maps:update (age, Me).
% => #{age => 20,name => "Wittyfox"}

% Delete a mapping, the key does not exist when the equivalent of nothing to do, do not throw an exception
Maps:remove (age, Me).
% => #{name => "Wittyfox"}

% lookup key value, return error when key does not exist
Maps:find (age, Me).
% => {OK, 19}

Maps:find (AEG, Me).
% => Error

Merging of mapping groups

Copy Code code as follows:

% merge Two mapping groups, note that the second parameter is to create a new mapping group, so you can only use the =>
Maps:merge (Me, #{age => 10}).
% => #{age => 10,name => "Wittyfox"}

% equivalent
me#{age => 10}.

Transformation between a mapping group and a list

Copy Code code as follows:

% returns a list of mapped tuple pairs
Maps:to_list (Me).
% => [{age,19},{name, "Wittyfox"}]

% to build a mapping group from a list
Maps:from_list ([]).
% => #{}

Maps:from_list ([{name, ' Wittyfox '}, {age, 19}]).
% => #{age => 19,name => "Wittyfox"}

Traversal of a mapping group

Copy Code code as follows:

% perform actions on each pair of mappings for a mapping group
% X, Y is a pair of mapped keys and values
Maps:map (Fun (x, y)-> io:format ("~p => ~p~n", [X, y]) end, Me).
% age => 19% output
% name => "Wittyfox"% output
% => #{age => ok,name => OK}% return value

% X, Y is a pair of mapped keys and values, V is the result of the previous iteration, 0 is the initial value of the iteration
% here simple for each iteration value plus 1, the result is the mapping group of the number of mappings
Maps:fold (Fun (X, Y, v)-> v + 1 end, 0, Me).
% => 2

Selection of mappings in a mapping group

Returns a mapping group consisting of mappings of the keys specified in the first parameter

Copy Code code as follows:

Maps:with ([], Me).
% => #{}

Maps:with ([age], Me).
% => #{age => 19}

% key can not exist
Maps:with ([AEG], Me).
% => #{}


Returns a mapping group that consists of mappings in the list of the first parameter no longer in the key
Copy Code code as follows:

Maps:without ([], Me).
% => #{age => 19,name => "Wittyfox"}

Maps:without ([age], Me).
% => #{name => "Wittyfox"}

% key can also not exist
Maps:without ([age, Neme], Me).
% => #{name => "Wittyfox"}


Attention

It is worth mentioning that several functions in the maps module, such as map, fold, with, and without are all going to the list using MAPS:TO_LIST/1, and then using the tools of the lists module, and then using MAPS:FROM_LIST/1 to go back to the mapping group Of

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.