Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly call out reverse ranges in : and range docstring #53626

Merged
merged 12 commits into from
Apr 2, 2024
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.
jishnub marked this conversation as resolved.
Show resolved Hide resolved
* 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`.
stevengj marked this conversation as resolved.
Show resolved Hide resolved

See Extended Help for additional details on the returned type.
See also [`logrange`](@ref) for logarithmically spaced points.
Expand Down