How can I generate a sequence of numbers in Elixir?

While doing some Elixir exercises, I discovered the need to quickly generate a sequence of 1 to n integers. In Ruby, I would do this:

numbers = (1..100) 

Is there something similar in Elixir?

+6
source share
1 answer

Elixir has a very similar feature:

 iex(2)> numbers = 1..10 1..10 iex(3)> Enum.to_list(numbers) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] iex(4)> Enum.map(numbers, fn x -> x * x end) [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] 

For documentation see Range

+9
source

All Articles