Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate a sequence of numbers in Elixir?

Tags:

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?

like image 773
Adam Waselnuk Avatar asked Jun 22 '15 14:06

Adam Waselnuk


1 Answers

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

like image 50
Paweł Obrok Avatar answered Nov 02 '22 19:11

Paweł Obrok