In Python I can store an index range into a variable and reuse it. In particular, I can store a range with implicit begin-at-0 and end-at-end-of-list extremas.
I know I can define my own slice type, and I think I understand what methods I need to define in order for it to work on most Array types. But I am hopeful there is some obvious class in Base that already implements this. A cursory look through the docs and some googling/ducking did not really help.
Just my_list[4:end]? Ok, I get you want to pull out the 4:end to re-use it, but note that there is zero cost to constructing that thing. Unlike Python, our : range syntax works outside of indexing. You can just do my_boring_slice = 3:3:8 or my_interesting_slice = 4:length(my_list) or even my_whole_dimension = (:).
Julia’s slices are just arrays of integers. That’s it. There is a very simple package that you can use — EndpointRanges.jl — that can give you an end that works outside of indexing and dynamically matches the arrays it indexes into.
Thanks, EndpointRanges is exactly what I needed! I do have a vague sense of discomfort that this is not in Base, but that is probably just because I was burnt by Javascript.
Is EndpointRanges.jl still necessary? You can also index from the beginning of the array with begin, for use with OffsetArrays.jl and such.
using OffsetArrays
a=OffsetArray([1:11;],-6)
#11-element OffsetArray(::Vector{Int64}, -5:5) with eltype Int64 with indices -5:5:
a[3] #9
a[begin+2:3:end] # [3,6,9]