hykw
hykw

Reputation: 690

Converting List to Map, Elixir way

I have a function which returns a Map, converted from List(:erlang.memory()). It works as I expected, but it doesn't seem to be beautiful. How can I refine the snippet to more elixir-way(meaning more beautiful logic)?

{{: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,
}

Upvotes: 6

Views: 2429

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

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 comes to the rescue.

NB Please refer also to valuable comment by @Dogbert below.

Upvotes: 9

Related Questions