我正在尋找更高效的方法。erlang更有效的合併元組列表
[{name, "Joe"}, {color, "Red"}, {file, "none"}] => {name,"Joe",color,"Red",file,"none"}.
list_to_tuple(lists:flatten([tuple_to_list(X1) || X1 <- L]))
我正在尋找更高效的方法。erlang更有效的合併元組列表
[{name, "Joe"}, {color, "Red"}, {file, "none"}] => {name,"Joe",color,"Red",file,"none"}.
list_to_tuple(lists:flatten([tuple_to_list(X1) || X1 <- L]))
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_([]) -> [].
最後一個肯定是最好的,在這種情況下試圖使用HO就變得複雜了。 – rvirding
也許
list_to_tuple(lists:flatten([K, V] || {K, V} <- L])).
請說明您的具體問題或添加其他詳細信息,以確切地突出顯示您的需求。因爲它目前已經寫好,所以很難準確地說出你在問什麼。 –