Say that I have the following pseudo-code:
for i in 1:length(array)-1
array[i+1] = some_function(array[i])
end
Now I want to rewrite the loop using eachindex
instead of 1:length
(because it is simpler, and because that array might need to be 0-indexed at some point). Is there a function operating on OneTo
, or on UnitRange
, which changes the interval [a:b]
to [a+δa:b+δb]
? (I rapidly looked at "range.jl"
and saw no such function.)
(I would be tempted to define Base.:-(r::OneTo, d::Int) = OneTo(r.stop-d)
, so that 1:length(array)
could be replaced by eachindex(array)
even when followed by an offset. But that addresses only one end of the problem.)