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

And just today, another thread asking about this. It’s funny how interest in topics tends to bunch temporally :sweat_smile:

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:

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])

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?

1 Like