Adam Waselnuk
Adam Waselnuk

Reputation: 970

How can I generate a sequence of numbers in Elixir?

Running through some Elixir exercises, I found 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?

Upvotes: 7

Views: 12655

Answers (1)

Paweł Obrok
Paweł Obrok

Reputation: 23174

There is a very similar feature in Elixir:

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

Upvotes: 11

Related Questions