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

Document the generic functions nextind() and prevind() #52658

Merged
merged 8 commits into from
Jan 28, 2024
68 changes: 68 additions & 0 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,74 @@ end

keys(@nospecialize t::Tuple) = OneTo(length(t))

"""
prevind(A, i)

Return the index before `i` in `A`. The returned index is often equivalent to `i
- 1` for an integer `i`. This function can be useful for generic code.

!!! warning
The returned index might be out of bounds. Consider using
[`checkbounds`](@ref).

See also: [`nextind`](@ref).

# Examples
```jldoctest
julia> x = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4

julia> prevind(x, 4) # valid result
3

julia> prevind(x, 1) # invalid result
0

julia> prevind(x, CartesianIndex(2, 2)) # valid result
CartesianIndex(1, 2)

julia> prevind(x, CartesianIndex(1, 1)) # invalid result
CartesianIndex(2, 0)
```
"""
function prevind end

"""
nextind(A, i)

Return the index after `i` in `A`. The returned index is often equivalent to `i
+ 1` for an integer `i`. This function can be useful for generic code.

!!! warning
The returned index might be out of bounds. Consider using
[`checkbounds`](@ref).

See also: [`prevind`](@ref).

# Examples
```jldoctest
julia> x = [1 2; 3 4]
2×2 Matrix{Int64}:
1 2
3 4

julia> nextind(x, 1) # valid result
2

julia> nextind(x, 4) # invalid result
5

julia> nextind(x, CartesianIndex(1, 1)) # valid result
CartesianIndex(2, 1)

julia> nextind(x, CartesianIndex(2, 2)) # invalid result
CartesianIndex(1, 3)
```
"""
function nextind end

prevind(@nospecialize(t::Tuple), i::Integer) = Int(i)-1
nextind(@nospecialize(t::Tuple), i::Integer) = Int(i)+1

Expand Down
2 changes: 2 additions & 0 deletions doc/src/base/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ Base.Broadcast.result_style
```@docs
Base.getindex(::AbstractArray, ::Any...)
Base.setindex!(::AbstractArray, ::Any, ::Any...)
Base.nextind
Base.prevind
Base.copyto!(::AbstractArray, ::CartesianIndices, ::AbstractArray, ::CartesianIndices)
Base.copy!
Base.isassigned
Expand Down
4 changes: 2 additions & 2 deletions doc/src/base/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ Base.chopprefix
Base.chopsuffix
Base.chomp
Base.thisind
Base.nextind
Base.prevind
Base.nextind(::AbstractString, ::Integer, ::Integer)
Base.prevind(::AbstractString, ::Integer, ::Integer)
Base.textwidth
Base.isascii
Base.iscntrl
Expand Down