# Strip one element off the end of a \`OneTo\` (or \`UnitRange\`)

**URL:** <https://discourse.julialang.org/t/strip-one-element-off-the-end-of-a-oneto-or-unitrange/53404>\
**Category:** New to Julia\
**Created:** [January 15, 2021, 8:07pm UTC](https://discourse.julialang.org/t/strip-one-element-off-the-end-of-a-oneto-or-unitrange/53404 "2021-01-15T20:07:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![circonflexe](https://avatars.discourse-cdn.com/v4/letter/c/c57346/32.png) [@circonflexe](https://discourse.julialang.org/u/circonflexe)\
**Post date:** [January 15, 2021, 8:07pm UTC](https://discourse.julialang.org/t/strip-one-element-off-the-end-of-a-oneto-or-unitrange/53404/1 "2021-01-15T20:07:26Z")

</div>

Say that I have the following pseudo-code:

```julia
    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.)

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [January 15, 2021, 8:14pm UTC](https://discourse.julialang.org/t/strip-one-element-off-the-end-of-a-oneto-or-unitrange/53404/2 "2021-01-15T20:14:40Z")

</div>

Indexing is one way: `r[begin:end-1]`. But if you’re going to operate on `eachindex(A)`, you should probably be prepared to get `CartesianIndices` back… and while you can also index into them with `[begin:end-1]`, it’ll leave the performance wanting (to put it mildly).

You could do `Iterators.take(eachindex(A), length(A)-1)`.
