Ranges
Mix.install([
{:jason, "~> 1.4"},
{:kino, "~> 0.9", override: true},
{:youtube, github: "brooklinjazz/youtube"},
{:hidden_cell, github: "brooklinjazz/hidden_cell"}
])
Navigation
Home Report An Issue Pokemon BattleEnumReview Questions
Upon completing this lesson, a student should be able to answer the following questions.
- How do we represent a range of numbers in Elixir?
- What is the step of a range, and how do we change it?
- How can we pattern match on a range?
Ranges
Ranges are useful for representing an ascending or descending sequence of numbers, without needing to manually type each number in a list.
For example, you could have a range from 1 to 10.
1..10
flowchart LR
1 --> 2 --> 3 --> 4 --> 5 --> 6 --> 7 --> 8 --> 9 --> 10
or 5 to -5.
5..-5
flowchart LR
5 --> 4 --> 3 --> 2 --> 1 --> 0 --> -1 --> -2 --> -3 --> -4 --> -5
You can visualize a range as representing an ascending or descending list of integers.
On its own, it might not seem like much. But ranges will be incredibly useful with the Enum module. You’ll learn more about the Enum module in a future lesson. For now, it’s enough to understand the relationship between ranges and lists.
For example the range 1..5
equates to [1, 2, 3, 4, 5]
as a list.
To establish that relationship, you can use Enum.to_list/1 to convert a range from the list.
Enum.to_list(1..5)
Ranges can have a step. By default ranges have a step of 1
. That means that by default they
include each whole number in the range.
You can override the default step of the range using //
:
0..10//2
This creates a range from 0
to 10
with a step of 2
.
0 --> 2 --> 4 --> 6 --> 8 --> 10
That means the range 0..10//2
correlates to the list [0, 2, 4, 6, 8, 10]
Here’s the same range converted into a list to help you visualize it. Once again, you will
learn more about what Enum.to_list
is in a future lesson.
Enum.to_list(0..10//2)
Your Turn
In the Elixir cell below, use Enum.to_list/1 to convert a range from 3
to 9
with a step of 3
into a list.
Example solution
Enum.to_list(3..9//3)
Pattern Matching With Ranges
We can pattern match on ranges just like any other data type.
For the most strict match, the left and right side must be exactly equal.
1..10 = 1..10
We can also bind the start and end of the range to a variable in our pattern match.
start..finish = 1..10
start
finish
We can also bind the step.
start..finish//step = 0..9//3
start
finish
step
Commit Your Progress
DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.
Run git status
to ensure there are no undesirable changes.
Then run the following in your command line from the curriculum
folder to commit your progress.
$ git add .
$ git commit -m "finish Ranges reading"
$ git push
We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.
We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.