From 896ff6a8d50ac07e82c5c5e2a3ef29051099cd12 Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Mon, 13 Jul 2020 16:02:37 -0400 Subject: [PATCH] restrict last(v::AbstractArray, n::Integer) to AbstractVector MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As explained in my comment https://github.com/JuliaLang/julia/pull/34868/files#r453380729 … in general, this optimization only seems to make sense for arrays with linear indexing, but it's easiest to just use AbstractVector here and rely on the `Iterators.reverse` fallback for other cases. --- base/abstractarray.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index 68dce7e7d0b20..589d460e9a3f7 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -410,7 +410,7 @@ Float64[] """ last(itr, n::Integer) = reverse!(collect(Iterators.take(Iterators.reverse(itr), n))) # Faster method for arrays -function last(v::AbstractArray, n::Integer) +function last(v::AbstractVector, n::Integer) n < 0 && throw(ArgumentError("Number of elements must be nonnegative")) @inbounds v[max(begin, end - n + 1):end] end