Elixir Map To List

Keywords List Enum Elixir

To get a list of values without keywords from a map 

iex> Map.values(%{a: 1, b: 2})
[1, 2]

Documentation Reference

It can also be done with Keyword

iex> Keyword.values([a: 1, b: 2])
[1, 2]
iex> Keyword.values([a: 1, b: 2, a: 3])
[1, 2, 3]

Documentation Reference

To transform a map to a list with keywords:

iex> Map.to_list(%{a: 1})
[a: 1]
iex> Map.to_list(%{1 => 2})
[{1, 2}]

Documentation Reference

Turns out you iterate over a Map exactly like you do over a Keyword List (i.e. you use a tuple):

Enum.each  %{a: 1, b: 2, c: 3},  fn {k, v} ->
  IO.puts "#{k} --> #{v}"

Web Reference

Joining to Lists of Lists

Whenever you want to iterate over two enumerables simultaneously, you should think about using Enum.zip/2. This is how I'd do it with Enum.zip/2 and for:

for {point, result} <- Enum.zip(points, results) do
  point ++ [result]
|> IO.inspect

Web Reference

List to Map:

results_map = results
  |> Enum.map(fn(x) -> %{cluster: x} end)


North New Jersey Office

Kinnelon, NJ

Philadelphia Office

Media, PA

office Hours

Mon - Fri : 8am to 6pm

Social Media