In Elixir, how can a range be converted to a list?
I can declare a range as follows:
range = 1..10
Is there a way to convert the range to a list?
Enum.to_list/1
is what you're looking for:
iex(3)> Enum.to_list 1..10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The generic way to convert an enumerable into a specific collectable is Enum.into
:
Enum.into 1..10, []
#=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
You can also pass a transformation function as third argument:
Enum.into 1..10, %{}, &({&1, &1})
#=> %{1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 10}