Skip to content

Commit

Permalink
Document the generic functions nextind() and prevind() (#52658)
Browse files Browse the repository at this point in the history
Co-authored-by: inky <git@wo-class.cn>
Co-authored-by: Denis Barucic <barucic.d@gmail.com>
Co-authored-by: Lilith Orion Hafner <lilithhafner@gmail.com>
  • Loading branch information
4 people committed Jan 28, 2024
1 parent d298c44 commit 0ce62fe
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
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

0 comments on commit 0ce62fe

Please sign in to comment.