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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With