I took this too far : https://github.com/JobJob/EndyIndexes.jl
pkg> add https://github.com/JobJob/EndyIndexes.jl.git
using EndyIndexes
const start_ = StartBasedIdx()
const end_ = EndBasedIdx()
arr = [1:10;]
for idx in (end_-2:end_, start_+1:end_-2)
@show arr[idx]
end
a = [1,2,3,4,5]
deleteat!(a, end_-1:end_)
Output
arr[idx] = [8, 9, 10]
arr[idx] = [2, 3, 4, 5, 6, 7, 8]
3-element Array{Int64,1}:
1
2
3
Thanks to the fantastic work that went into custom AbstractArray indexing this was surprisingly easy to implement.
Also just re the comment above, in Julia >=0.7, in multi-dimensional index expressions end
now lowers to lastindex(a, dim)
, in single index expressions just to lastindex(a)
jjulia> f1(a) = a[2, end-1:3, 1]
f1 (generic function with 1 method)
julia> f2(a) = a[end]
f2 (generic function with 1 method)
julia> @code_lowered f1(rand(3,3,3))
CodeInfo(
1 1 ─ %1 = (Base.lastindex)(a, 2) │
│ %2 = %1 - 1 │
│ %3 = %2:3 │
│ %4 = (Base.getindex)(a, 2, %3, 1) │
└── return %4 │
)
julia> @code_lowered f2(rand(3,3,3))
CodeInfo(
1 1 ─ %1 = (Base.lastindex)(a) │
│ %2 = (Base.getindex)(a, %1) │
└── return %2 │
)