For generic code I’d refrain from using [begin+4:end-4]
,
as it would fail on “strided” arrays (e.g. arrays stored in compact form, but with only even indices).
Edit: leaving the part below, which shows how indexing on eachindex
surprised me.
I read Paulo’s post as “iterate from the 5th element to the 4th before last”,
which seems a valid use case.
To do that, one can’t index on the eachindex(oa)
iterator itself
oa = OffsetArray(["a", "b", "c", "d", "e"], -2:2)
eachindex(oa)[3:5]
BoundsError: attempt to access 5-element OffsetArrays.IdOffsetRange{Int64, Base.OneTo{Int64}} with indices -2:2 at index [3:5]
I currently do not understand what it is trying to achieve.
But on the collected indexes, it works nicely:
oa = OffsetArray(["a", "b", "c", "d", "e"], -2:2)
# loop on the 3rd to 5th element
for idx in collect(eachindex(oa))[3:5]
println("$idx: $(oa[idx])")
end
0: c
1: d
2: e
I’d expect Base.iterators.take(iterator, 3:5)
to return a nice iterator for this purpose,
but it’s not the case yet (as of julia-1.6.5).
Julia is already awesome, and progressing quickly in the right direction. Keep on the good work !