将列表转换为地图,Elixir方式

时间:2016-12-14 09:15:00

标签: elixir

我有一个函数,它返回一个从List转换的Map(:erlang.memory())。它按照我的预期工作,但似乎并不美观。如何将片段细化为更多的灵丹妙药(意味着更美妙的逻辑)?

{{:total, total}, {:processes, processes}, {:processes_used, processes_used}, {:system, system}, {:atom, atom}, {:atom_used, atom_used}, {:binary, binary}, {:code, code}, {:ets, ets}} =
  :erlang.memory()
  |> List.to_tuple()

params = %{
  total: total,
  processes: processes,
}

1 个答案:

答案 0 :(得分:6)

iex(1)> :erlang.memory()
[total: 20258296, processes: 5377080, processes_used: 5370936, system: 14881216,
 atom: 264529, atom_used: 255982, binary: 72440, code: 6322711, ets: 335736]

iex(2)> :erlang.memory() |> Enum.into(%{})
%{atom: 264529, atom_used: 259196, binary: 149136, code: 6564510, ets: 347720,
  processes: 5518032, processes_used: 5516752, system: 15248920,
  total: 20766952}

Enum.into/2来救援。

NB 请参阅以下@Dogbert的宝贵意见。