1. Ranges
Well done. We're now going to look at ranges - another type of data structure.
2. Ranges - UnitRange
The concept behind the range object is simple. It is a collection of values, with a defined start and stop. To create a range, we specify start and stop values, separated by a colon.
If we wanted to print the values within this range, which in this example is the numbers from one to ten (including one and ten), we would need to iterate over this, just as we iterated over vectors previously using a loop. If we just try and print the variable containing the range, we get back our range definition. This is different to other languages such as R.
This type of range is called a UnitRange, and it is the simplest form that a Range can take in Julia.
3. Ranges - StepRange
A slightly more complex range is the StepRange. This is the same as a UnitRange, except we specify a step value, as shown in the syntax.
We can think of each step in a range as an iteration, similar to iterations in our loops exercises. With each step we take the previous value of the range, modify it using the step, and then we have our next value. Continue on until we reach our end value, and that's our StepRange.
4. Ranges - StepRange definition
In this example, we have defined a StepRange with a start value of one, a step value of ten, and an ending value of 50.
Note that we still cannot access the values within the range by simply printing the variable. This again gives us the range definition, just as with UnitRange.
5. Ranges - for iteration
To access the values within a range, we need to unpack the range by iterating over it. We can use a for loop.
Here we have a StepRange, starting at zero and ending at 50, with a step size of 10. The iterator is the variable value, and the iterable is our range, stored in my_range.
Looking at the output of our loop, we can see that we have printed the initial value of zero, and then added ten on to the result with each iteration until we reach the ending value of 50.
6. Ranges - access
Another way to access the values inside a range is to use the square bracket notation, which allows us to specify the index of each element. Here, we are printing the second element of our range, which, for a range starting at zero, will be ten.
7. Ranges - access
We also have access to the start, step, and stop keywords, which allow us to quickly get the corresponding values for a range.
8. Ranges - while iteration
We've seen how a for loop can iterate over a range, and a while loop can be used to do the same thing. We access each element of the range using the square bracket notation that we just covered.
We set our counter variable equal to one, as Julia starts indexing at 1 for data structures, and we then loop over the length of the range, printing the i'th element of the range until i is larger than the length of the range.
9. Ranges - splat unpacking
The final method to iterate over a range is to use the splat operator. Denoted by three periods, it allows us to unpack an iterable into its individual components.
In this example, we used the splat operator to unpack the range my_range to a vector.
In the output we can see that it has returned a vector with each element containing the next value in the range.
This is one simple and quick example of the splat operator. Other uses are outside the scope of this course.
10. Let's practice!
Now that we've seen how ranges work, let's practice.