erlang tuple的一些操做

========================================================================
Tuple 元組css

{Term1,...,TermN}
每一項元組中的數據被稱爲元素(element)html

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}
**************app

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}}
************ui

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

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

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.htmlthree

maps 也是不少關於tuple 的操做,這個能夠結合着來 ========element

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}]

相關文章
相關標籤/搜索