We've seen ranges a bit earlier, when learning to index into strings
. They can be as simple as the following:
julia> r = 1:20
1:20
As with previous collections, we can index into ranges:
julia> abc = 'a':'z'
'a':1:'z'
julia> abc[10]
'j': ASCII/Unicode U+006a (category Ll: Letter, lowercase)
julia> abc[end]
'z': ASCII/Unicode U+007a (category Ll: Letter, lowercase)
A range can be expanded into its corresponding values by using the splat operator, "..."
. For example, we can splat it into a tuple:
julia> (1:20...,)
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
We can also splat it into a list:
julia> [1:20...]
20-element Array{Int64,1}
Note
The same is true for Tuples, which can also be splatted into lists, among other things: [(1,2,3)...]
.
We can see that the range steps in increments of one, by default. We can change that by passing it an optional step parameter. Here is an example of a range between 0
and 20
with a step of...