Skip to content

Commit

Permalink
Explicitly call out reverse ranges in : and range docstring (#53626)
Browse files Browse the repository at this point in the history
  • Loading branch information
BioTurboNick authored Apr 2, 2024
1 parent d8d3842 commit e99627f
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ _colon(::Any, ::Any, start::T, step, stop::T) where {T} =
(:)(start, [step], stop)
Range operator. `a:b` constructs a range from `a` to `b` with a step size
equal to 1, which produces:
equal to +1, which produces:
* a [`UnitRange`](@ref) when `a` and `b` are integers, or
* a [`StepRange`](@ref) when `a` and `b` are characters, or
Expand All @@ -41,6 +41,9 @@ equal to 1, which produces:
`a:s:b` is similar but uses a step size of `s` (a [`StepRange`](@ref) or
[`StepRangeLen`](@ref)). See also [`range`](@ref) for more control.
To create a descending range, use `reverse(a:b)` or a negative step size, e.g. `b:-1:a`.
Otherwise, when `b < a`, an empty range will be constructed and normalized to `a:a-1`.
The operator `:` is also used in indexing to select whole dimensions, e.g. in `A[:, 1]`.
`:` is also used to [`quote`](@ref) code, e.g. `:(x + y) isa Expr` and `:x isa Symbol`.
Expand All @@ -66,8 +69,12 @@ Mathematically a range is uniquely determined by any three of `start`, `step`, `
Valid invocations of range are:
* Call `range` with any three of `start`, `step`, `stop`, `length`.
* Call `range` with two of `start`, `stop`, `length`. In this case `step` will be assumed
to be one. If both arguments are Integers, a [`UnitRange`](@ref) will be returned.
* Call `range` with one of `stop` or `length`. `start` and `step` will be assumed to be one.
to be positive one. If both arguments are Integers, a [`UnitRange`](@ref) will be returned.
* Call `range` with one of `stop` or `length`. `start` and `step` will be assumed to be positive one.
To construct a descending range, specify a negative step size, e.g. `range(5, 1; step = -1)` => [5,4,3,2,1]. Otherwise,
a `stop` value less than the `start` value, with the default `step` of `+1`, constructs an empty range. Empty ranges
are normalized such that the `stop` is one less than the `start`, e.g. `range(5, 1) == 5:4`.
See Extended Help for additional details on the returned type.
See also [`logrange`](@ref) for logarithmically spaced points.
Expand Down

0 comments on commit e99627f

Please sign in to comment.