Erlang Records and mapping groups

Source: Internet
Author: User

A record is actually another form of a tuple. By using records, you can associate a name for each element in a tuple.

A mapping group is a collection of associations of key-value pairs. The key can be any of the Erlang data types.

When to use a mapping group or record

Records are another form of tuples, so they are stored in the same way as performance characteristics and tuples. The mapping group takes up more storage space than the tuple, and it looks more slowly. On the other hand, mapping groups are much more flexible than tuples.

Use the recorded scene:

    • When you can use some predetermined and fixed number of atoms to represent the data;
    • When the number of elements in the record and the name of the element do not change over time;
    • There is a large push tuple, and each tuple has the same result.

Use a map group scenario:

    • Used to represent a key-value data structure when the key cannot be known beforehand;
    • Used to represent data when there are a large number of different keys;
    • Use as a universal data structure when convenience is important and efficiency is irrelevant;
    • Used as a "self-explanatory" data structure, that is, the use of easy to guess the meaning of the value from the key name;
    • Used to denote a key-value resolution tree, such as an XML or configuration file;
    • Use JSON to communicate with other programming languages.
By recording items in a named tuple

For small tuples, it is almost impossible to remember what each element represents, but it is more convenient to name individual elements when the tuple contains a large number of elements. Once these elements are named, you can point to them by their names, without having to remember their location in the tuple.

To name the elements in a tuple with a record declaration, the syntax is:

-record (Name, {                 two keys with a default value of               key1 = Default1,               = Default2,               ...                %% the next line is equivalent to key 3 = undefined                Key3,               ...}).

Name is the name of the record, Key1, Key2 These are the names of the fields contained in the record, they must be atoms, each field in the record can have a default value, and if the value of a field is not specified when the record is created, the default value is used.

Suppose you want to manipulate a list of things to do. We first define a TODO record and then save it in a file (the definition of the record can be stored in the Erlang source code file, or it can be saved in an extension file named. HRL, and then included in the Erlang source code file). File containment is the only way to ensure that multiple Erlang modules share the same record definition.

Erlang Records and mapping groups

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.