Elixir - Convert List into a Map
You can avoid the extra call to Enum.map/2
, and build the new map directly using Map.new/2
:
[1,2,3,4]
|> Enum.chunk_every(2)
|> Map.new(fn [k, v] -> {k, v} end)
Update: The previous version of this answer used chunk/2
but that has been deprecated in favor of chunk_every/2
.
You can use Enum.chunk_every/2
:
[1, 2, 3, 4]
|> Enum.chunk_every(2)
|> Enum.map(fn [a, b] -> {a, b} end)
|> Map.new