# Using end variable in ranges passed to functions other than getindex?

**URL:** <https://discourse.julialang.org/t/using-end-variable-in-ranges-passed-to-functions-other-than-getindex/7591>\
**Category:** General Usage\
**Tags:** indexing\
**Created:** [December 7, 2017, 2:04am UTC](https://discourse.julialang.org/t/using-end-variable-in-ranges-passed-to-functions-other-than-getindex/7591 "2017-12-07T02:04:44Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![jobjob](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jobjob/32/160_2.png) [@jobjob](https://discourse.julialang.org/u/jobjob)\
**Post date:** [October 10, 2018, 8:27am UTC](https://discourse.julialang.org/t/using-end-variable-in-ranges-passed-to-functions-other-than-getindex/7591/7 "2018-10-10T08:27:45Z")

</div>

I took this too far 😄: [https://github.com/JobJob/EndyIndexes.jl](https://github.com/JobJob/EndyIndexes.jl)

```julia
pkg> add https://github.com/JobJob/EndyIndexes.jl.git

```

```julia
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

```julia
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)`

```julia
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 │
)

```

---

_[View the full topic](https://discourse.julialang.org/t/using-end-variable-in-ranges-passed-to-functions-other-than-getindex/7591)._
