J teaches you how to use Erlang-tuple

Source: Internet
Author: User

Tuples are the composite data types used to store a group of data elements. The data elements must be Erlang data types, the tuples are defined using closed curly braces {}, and the elements in them are separated by commas.

Example: 1> {1, 2, 3 }.

2> {1, {2, 3}, 4 }.

3> {1, [2, 3], 4 }.

4> {1, "23", 4 }.

 

The BIFS function of tuple:

Erlang: tuple_size (tuple)-> length. Length of tuple

Erlang: element (I, tuple)-> aton (). Select the element at position I.

Erlang: setelement (I, tuple, Aton)-> tuple2, modify the value of position I in tuple to Aton.

 

Refer to lists to obtain some functions similar to lists:

-Module (com_record ).

% ===================================================== ====================================
% API functions
% ===================================================== ====================================
-Export ([MAP/2,
Get_name/1,
Foreach/2,
Foldl/3,
Foldl2/4,
Foldl_index/4,
Merge/3,
Merge/4,
Any/2,
Any/3,
Tuple_merge/3,
Put_pd_fields/2,
Get_pd_fields/2
]).

-Spec any (Pred, t)-> Boolean () when
PRED: Fun (ELEM: term ()-> Boolean ()),
T: tuple ().

% @ Doc like lists: Any/1
Any (Pred, tuple)->
Any (Pred, tuple, 2 ).

Any (_, {}, _)-> false;
Any (Pred, tuple, begin)->
S = tuple_size (tuple ),
If S <begin->
False;
True->
Any _ (Pred, tuple, begin, S)
End.

Any _ (Pred, tuple, M, m)->
PRED (element (M, tuple ));
Any _ (Pred, tuple, n, m)->
Case PRED (element (M, tuple)
True->
True;
_->
Any _ (Pred, tuple, n + 1, m)
End.

 


% @ Doc record Map
-Spec map (func, record)-> record2 when
Record: tuple,
Record2: tuple,
FUNC: Fun (term ()-> term ()),
Tuple: tuple ().
Map (func, record)->
[Name | list] = Erlang: tuple_to_list (record ),
List2 = lists: Map (func, list ),
Erlang: list_to_tuple ([name | list2]).

-Compile ({inline, [get_name/1]}).
% @ DOC get Record Name.
-Spec get_name (tuple ()-> atom ().
Get_name (record)->
Erlang: element (1, record ).


% @ Doc foreach element not include name.
-Spec foreach (fun, record)-> OK when
Fun: Fun (term ()-> _),
Record: tuple ().
Foreach (fun, record) When is_tuple (record)->
Com_util: For (2, Erlang: tuple_size (record), fun ).


% @ Doc foldl element not include name.
-Spec foldl (fun, acc0, record)-> acc1 when
Fun: Fun (term (), accin)-> accout ),
Acc0: term (),
Acc1: term (),
Accin: term (),
Accout: term (),
Record: tuple ().

% @ Doc not aceess Record Name.
Foldl (func, ACC, record) When is_function (func, 2)->
Case Erlang: tuple_size (record)
1-> ACC;
M->
Foldl _ (func, ACC, record, 2, m)
End.

Foldl2 (func, ACC, record, begin)
When is_function (func, 2)->
Case Erlang: tuple_size (record)
S when S <begin-> ACC;
S->
Foldl _ (func, ACC, record, begin, S)
End.


Foldl _ (func, ACC, record, n, n)->
Func (Erlang: element (n, record), ACC );
Foldl _ (func, ACC, record, n, m)->
Foldl _ (func,
Func (Erlang: element (n, record), ACC ),
Record, n + 1, m ).

% @ Doc same foldl2 but fun Take 3 ARG
Foldl_index (func, ACC, record, begin)->
Case Erlang: tuple_size (record)
S when S <begin-> ACC;
S->
Foldl_index _ (func, ACC, record, begin, S)
End.

% @ Doc merge two same type record with func
% Two record must same type.
% Fun argument is record Elements
% @ Usage
% Merage (fun (a, B)-> A + B end, {AA, 1}, {AA, 3, 3 })
%-> {AA, 4,4, 4}
-Spec Merge (fun, record1, record2)-> recordout when
Fun: Fun (a, B)-> T ),
Record1: recordout,
Record2: recordout,
Recordout: tuple (),
A: term (),
B: term (),
T: term ().

Merge (fun, record1, record2) When is_function (fun, 2)->
Merge _ (fun, record1, record2, 2, {get_name (record1 )}).

Merge (fun, record1, record2, begin) When is_function (fun, 2)->
Merge _ (fun, record1, record2, begin, {get_name (record1 )}).

Tuple_merge (fun, T1, T2)->
Merge _ (fun, T1, T2, 1 ,{}).

Merge _ (fun, T1, T2, begin, ACC)->
Com_util: Fold (begin,
Erlang: tuple_size (T1 ),
Fun (index, recordout)->
Erlang: append_element (recordout,
Fun (Erlang: element (index, T1 ),
Erlang: element (index, T2 )))
End,
ACC
).

 

Foldl_index _ (func, ACC, record, n, n)->
Func (n, Erlang: element (n, record), ACC );
Foldl_index _ (func, ACC, record, n, m)->
Foldl_index _ (func,
Func (n, Erlang: element (n, record), ACC ),
Record, n + 1, m ).

 

% Hack best meta Conversion
% @ Doc every filed in the process dictionary will be prefixed with Pd _
% Each process dictionary can only be assigned a single value
Put_pd_fields (record, fieldsname)
When Erlang: is_list (fieldsname ),
Erlang: is_tuple (record)->
Lists: foldl (fun (fname, index)->
Case Erlang: Put (Erlang: list_to_atom ("Pd _" ++ Erlang: atom_to_list (fname )),
Erlang: element (index, record ))
Of
Undefined-> OK;
OV-> IO: Format ("Err recrod_put ~ P not first ~ P \ n ", [fname, ov])
End,
Index + 1
End,
2,
Fieldsname ).

% @ Doc record
-Spec get_pd_fields (recordname, [atom ()])-> recordout when
Recordname: Atom (),
Recordout: tuple ().

Get_pd_fields (recordname, fieldsname)
When Erlang: is_list (fieldsname)->
Lists: foldl (fun (fname, R)->
Erlang: append_element (r,
Erlang: Get (Erlang: list_to_atom ("Pd _" ++ Erlang: atom_to_list (fname ))))
End,
{Recordname },
Fieldsname ).


% Test Unit

%-Define (test, 1 ).
-Ifdef (test ).
-Include_lib ("eunit/include/eunit. HRL ").

-Record (TT, {ID, name, age }).

Pd_fields_test ()->
R = # TT {id = 32, name = <"feof">, age = [1, 2]},
Put_pd_fields (R, record_info (fields, TT )),
? Assertequal (32, get (pd_id )),
? Assertequal (<"feof">, get (pd_name )),
? Assertequal ([1, 2, 4], get (pd_age )),

R2 = get_pd_fields (TT, record_info (fields, TT )),
? Assertequal (R2, R ),

OK.

-Endif.

J teaches you how to use Erlang-tuple

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.