erlang更高效的合并元组列表

时间:2013-12-09 08:13:00

标签: erlang

我正在寻找提高效率的方法。

[{name, "Joe"}, {color, "Red"}, {file, "none"}] => {name,"Joe",color,"Red",file,"none"}.

list_to_tuple(lists:flatten([tuple_to_list(X1) || X1 <- L]))

2 个答案:

答案 0 :(得分:2)

list_to_tuple([X || T <- L, X <- tuple_to_list(T)])).

list_to_tuple([X || {K, V} <- L, X <- [K, V]]).

或更明确且可能最有效

merge_tuple_list(L) -> list_to_tuple(merge_tuple_list_(L)).

merge_tuple_list_([{K, V} | T]) -> [K, V | merge_tuple_list_(T) ];
merge_tuple_list_([])           -> [].

答案 1 :(得分:0)

也许

list_to_tuple(lists:flatten([K, V] || {K, V} <- L])).