I was testing some code and noticed something that seemed odd to me when trying to use the range operator (“:”).
julia> 1:5
1:5
but
julia> 5:1
5:4
I don’t see anything in the help for : that suggests different behavior if a<b or a>b for a:b, although in the help for range I do see this statement: If length is not specified and stop - start is not an integer multiple of step, a range that ends before stop will be produced.
So I guess that the issue is that a range that goes from a larger number to a smaller is giving a negative number, and is not considered an integer multiple of step? Is this expected behavior? I’d sort of expect to be able to create a range from a larger number to a smaller.
help?> :
search: : :: ?: >: <:
(:)(start, [step], stop)
Range operator. a:b constructs a range from a to b with a step
size equal to 1, which produces:
• a UnitRange when a and b are integers, or
• a StepRange when a and b are characters, or
• a StepRangeLen when a and/or b are floating-point.
a:s:b is similar but uses a step size of s (a StepRange or
StepRangeLen). See also range for more control.
The operator : is also used in indexing to select whole
dimensions, e.g. in A[:, 1].
: is also used to quote code, e.g. :(x + y) isa Expr and :x isa
Symbol. Since :2 isa Int, it does not create a range in indexing:
v[:2] == v[2] != v[begin:2].
This is the expected and documented behavior. If you want to go backwards, you need a step of -1.
Ah, I see. So 5:-1:1 works just fine. I guess in my head, I only ever think of step sizes as magnitudes, not directions. Guess I’m still used to how R does it.