6

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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

For this specific case, there's also |> Map.new.
Enum.into/2 and Map.new/1 are elegance and Elixir-ish solution. Thank you!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.