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

last(itr) should use Iterators.reverse #43497

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,12 @@ function first(v::AbstractVector, n::Integer)
end

"""
last(coll)
last(itr)

Get the last element of an ordered collection, if it can be computed in O(1) time. This is
accomplished by calling [`lastindex`](@ref) to get the last index. Return the end
point of an [`AbstractRange`](@ref) even if it is empty.
Get the last element of an ordered collection `itr`, if it can be computed
in O(1) time. This is accomplished for general iterable collections by
calling [`first`](@ref) on [`Iterators.reverse(itr)`](@ref Iterators.reverse).
(Returns the end point of an [`AbstractRange`](@ref) even if it is empty.)

See also [`first`](@ref), [`endswith`](@ref).

Expand All @@ -476,7 +477,8 @@ julia> last([1; 2; 3; 4])
4
```
"""
last(a) = a[end]
last(a) = first(Iterators.reverse(itr))
last(a::Union{AbstractArray,Tuple,Cmd}) = a[end]

"""
last(itr, n::Integer)
Expand Down
3 changes: 0 additions & 3 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1167,9 +1167,6 @@ end
isdone(r::Iterators.Reverse{<:EachLine}, state) = isempty(state.chunks)
isdone(r::Iterators.Reverse{<:EachLine}) = isdone(r.itr)

# use reverse iteration to get end of EachLines (if possible)
last(itr::EachLine) = first(Iterators.reverse(itr))

struct ReadEachIterator{T, IOT <: IO}
stream::IOT
end
Expand Down