zoukankan      html  css  js  c++  java
  • erlang tuple的一些操作

    ========================================================================
    Tuple 元组

    {Term1,...,TermN}
    每一项元组中的数据被称为元素(element)

    erlang:element(N, Tuple) -> term()
    Returns the Nth element (numbering from 1) of Tuple.


    1> P = {adam,24,{july,29}}.
    {adam,24,{july,29}}
    2> element(1,P).
    adam
    3> element(3,P).
    {july,29}


    erlang:tuple_size(Tuple) -> int()
    Returns an integer which is the number of elements in Tuple.


    5> tuple_size(P).
    3
    6> tuple_size({}).
    0


    erlang:setelement(Index, Tuple1, Value) -> Tuple2
    Returns a tuple which is a copy of the argument Tuple1 with the element given by the integer argument Index (the first element is the element with index 1) replaced by the argument Value.


    4> P2 = setelement(2,P,25).
    {adam,25,{july,29}}


    erlang:tuple_to_list(Tuple) -> [term()]
    Returns a list which corresponds to Tuple. Tuple may contain any Erlang terms.


    tuple_to_list({share, {'Ericsson_B', 163}}).
    [share,{'Ericsson_B',163}]


    erlang:append_element(Tuple1, Term) -> Tuple2
    Returns a new tuple which has one element more than Tuple1, and contains the elements in Tuple1 followed by Term as the last element.
    Semantically equivalent to list_to_tuple(tuple_to_list(Tuple) ++ [Term]), but much faster.


    erlang:append_element({one, two}, three).
    {one,two,three}


    以上摘自:
    http://blog.sina.com.cn/s/blog_453a02280100qpmr.html

    maps 也是很多关于tuple 的操作,这个可以结合着来 ========

    lists中关于tuple的操作:


    从元组列表 TupleList1 里删除元组的第 N 个值跟 Key 是一样的元素,只删除第一个匹配的元素,后面有相同的不做处理,
    最后返回处理过的新列表 TupleList2,如果没有找到,返回原来的列表!


    TupleList = [{a, 1}, {b, 2}, {c, 3}, {d, 4}],
    lists:keydelete(b, 1, TupleList).
    ------R:[{a,1},{c,3},{d,4}]


    keyfind(Key, N, TupleList) -> Tuple | false
    从元组列表 TupleList 里查找元组的第 N 个值跟 Key 是一样的元素。


    TupleList = [{a, 1}, {b, 2}, {c, 3}, {d, 4}],
    lists:keyfind(e, 1, TupleList).
    ------R:false


    元组列表 TupleList1 里每个元组的第 N 个值被函数 Fun 调用,调用产生的新值替换原来的,
    最后返回被函数 Fun 遍历调用过的新列表 TupleList2


    TupleList = [{a, 1}, {b, 2}, {c, 3}, {d, 4}],
    lists:keymap(fun(X) -> X * 2 end, 2, TupleList).
    ------R [{a,2},{b,4},{c,6},{d,8}]

    keymember(Key, N, TupleList) -> bool()
    TupleList = [{a, 1}, {b, 2}, {c, 3}, {d, 4}],
    lists:keymember(b, 1, TupleList).
    ------R true

    keymerge(N, TupleList1, TupleList2) -> TupleList3
    合并 2 个元组列表,合并好的新元组列表按元组的第 N 个值进行排序

    TupleList1 = [{a, 1}, {d, 4}],
    TupleList2 = [{b, 2}, {c, 3}],
    lists:keymerge(2, TupleList1, TupleList2).
    ------R [{a,1},{b,2},{c,3},{d,4}]

    从元组列表 TupleList1 里查找元组的第 N 个值跟 Key 是一样的元素,如果找到则用新元组替换,
    并返回一个新的元组列表 TupleList1,找不到则返回原来的元组列表 TupleList1
    TupleList = [{a, 1}, {b, 2}, {c, 3}, {d, 4}],
    lists:keyreplace(b, 1, TupleList, {b, 22}).
    ------R [{a,1},{b,22},{c,3},{d,4}]

    keysearch(Key, N, TupleList) -> {value, Tuple} | false
    跟 lists:keyfind/3 一样,都是从元组列表 TupleList 里查找元组的第 N 个值跟 Key 是一样的元素,
    只不过成功匹配找到时,返回值的格式不一样。
    TupleList = [{a, 1}, {b, 2}, {c, 3}, {d, 4}],
    lists:keysearch(b, 1, TupleList).
    ------R {value,{b,2}}

    keysort(N, TupleList1) -> TupleList2
    对元组列表 TupleList1 里按元组的第 N 个值进行排序,最后返回排序后的新元组列表 TupleList2
    TupleList = [{a, 3}, {b, 4}, {c, 1}, {d, 2}],
    lists:keysort(2, TupleList).
    ------R {[{c,1},{d,2},{a,3},{b,4}]

    keystore(Key, N, TupleList1, NewTuple) -> TupleList2
    从元组列表 TupleList1 里查找元组的第 N 个值跟 Key 是一样的元素,如果找到则用新元组替换,并返回一个新的元组列表 TupleList1,
    找不到则在原来的元组列表 TupleList1 后面加上新的元组
    TupleList = [{a, 1}, {b, 2}, {c, 3}, {d, 4}],
    lists:keystore(b, 1, TupleList, {b, 22}).
    ------R [{a,1},{b,22},{c,3},{d,4}]

    keytake(Key, N, TupleList1) -> {value, Tuple, TupleList2} | false
    从元组列表 TupleList 里查找元组的第 N 个值跟 Key 是一样的元素,如果找到这个元素,
    则把这个元素从列表里提取出来,最后返回被提取的元素和提取后的元组列表
    TupleList = [{a, 1}, {b, 2}, {c, 3}, {d, 4}],
    lists:keytake(b, 1, TupleList).
    ------R {value,{b,2},[{a,1},{c,3},{d,4}]}

    ukeymerge(N, TupleList1, TupleList2) -> TupleList3
    以元组的第 N 个值为键,合并 2 个元组列表,如果这 2 个列表里存在有元组的第 N 个值相同的情况,
    则保留 TupleList1 里的,丢弃 TupleList2 的。如果这 2 个元组列表里有重复的值,则删除。
    lists:ukeymerge(2, [{1, 10}, {2, 20}, {3, 30}], [{4, 20}, {3, 30}]).
    ------R [{1,10},{2,20},{3,30}]

    ukeysort(N, TupleList1) -> TupleList2
    以元组列表里的元组的第 N 个值为键,对元组列表 TuplleList1 进行排序,如果出现元组的第 N 个值有重复的,删除后面出现的
    lists:ukeysort(1, [{c, 1},{b, 2}, {b, 3}, {d, 4}, {a, 5}]).
    ------R [{a,5},{b,2},{c,1},{d,4}]

  • 相关阅读:
    ubuntu上搭建review board代码评审站点
    android5.1 for tq335x
    2015.04.11
    DECLARE_GLOBAL_DATA_PTR宏定义问题
    6410移植android4.4.2笔记(持续更新)
    ubuntu安装packet提示重复冲突问题
    android5.0 aosp编译记录(由于机器硬件原因,改为4.4.2编译通过)
    2015.01.16
    2015.01.14
    SDSM框架
  • 原文地址:https://www.cnblogs.com/ShankYan/p/5419243.html
Copyright © 2011-2022 走看看