# 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:** 19

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [March 26, 2023, 5:02am UTC](https://discourse.julialang.org/t/using-end-variable-in-ranges-passed-to-functions-other-than-getindex/7591/19 "2023-03-26T05:02:32Z")

</div>

And just today, [another thread](https://discourse.julialang.org/t/using-the-end-keyword-as-a-function-argument/96615) asking about this. It’s funny how interest in topics tends to bunch temporally 😅

> [@ParadaCarleton](#):
>
> But we might need something for indexing from the end.

As you saw last time, I had talked myself out of this when I realized it doesn’t save any boundschecks. But then I saw this thread:

> [@Base.Chop API rationale](https://discourse.julialang.org/t/base-chop-api-rationale/96473):
>
> [Base.chop](https://docs.julialang.org/en/v1/base/strings/#Base.chop) chop(s::AbstractString; head::Integer = 0, tail::Integer = 1) Remove the first head and the last tail characters from s. The call chop(s) removes the last character from s. If it is requested to remove more characters than length(s) then an empty string is returned. I am curious about the reasoning for two choices: uses keyword arguments instead of positional defaults to tail=1 Especially the default tail=1 I really am puzzled by: what is this function for?

Unicode makes the string API rather awkward, necessitating strange functions like `chop`; if we could access relative to first and last ordinal indices, methods like this wouldn’t be needed. Dictionaries and I imagine some other data structures could also make use of such indexing; I begin to wonder about sparse arrays.

Maybe it even makes sense to have _both_ `xs[begin:end]` indexing _and_ `xs[first:last]` indexing, where the former is understood to range over positional indices `eachindex(xs)`, whereas the latter ranges over ordinal indices `1:length(xs)`? (similar to the contrast between `xs[i]` vs `xs[n*th]`)

> [@ParadaCarleton](#):
>
> We could try using Python’s `-1st` for ordinal indices?

I entertained that thought momentarily, but I was turned off when I realized the inconsistency in having `1st:-1st` work but `(1:-1)st` not work. I’m not sure why unitrange `a:b` clamps to `a:a-1` for `b` much less than `a`; maybe that can be changed?

---

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