In Elixir, how can a range be converted to a list?
Use Enum.map/2
range = 1..10
Enum.map(range, fn(x) -> x end)
or
Enum.map(range, &(&1))
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]
Use the pipeline operator
1..10 |> Enum.to_list
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
and then you can apply further transformations on it like
1..10 |> Enum.to_list |> Enum.sum
55
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}