# How to create a reversed view that allows for growing arrays?

**URL:** <https://discourse.julialang.org/t/how-to-create-a-reversed-view-that-allows-for-growing-arrays/96148>\
**Category:** General Usage\
**Created:** [March 15, 2023, 7:23pm UTC](https://discourse.julialang.org/t/how-to-create-a-reversed-view-that-allows-for-growing-arrays/96148 "2023-03-15T19:23:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![g-gundam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/g-gundam/32/47593_2.png) [@g-gundam](https://discourse.julialang.org/u/g-gundam)\
**Post date:** [March 15, 2023, 7:23pm UTC](https://discourse.julialang.org/t/how-to-create-a-reversed-view-that-allows-for-growing-arrays/96148/1 "2023-03-15T19:23:52Z")

</div>

Forgive me for resurrecting an old thread, but being able to get an inverted view of an array is amazing to me as someone who is new to Julia but had to write a lot of code to get this kind of functionality in another language. However, I have a question. Suppose we have an array `a` and an inverted view of it in `z`.

```julia-auto
a = [1:10;]
z = view(a, lastindex(a):-1:firstindex(a))
a[10] # 10
z[1] # 10

```

So far, so good, but what if `a` gets a new element pushed on to it.

```julia-auto
push!(a, 11)
a[11] # 11
z[1] # 10 (but I want it to be 11)

```

#### Is there a good way to get the view `z` to track changes in the length of `a`?

---

<div class="post-metadata">

**Author:** ![simsurace](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simsurace/32/30216_2.png) [@simsurace](https://discourse.julialang.org/u/simsurace)\
**Post date:** [March 15, 2023, 8:28pm UTC](https://discourse.julialang.org/t/how-to-create-a-reversed-view-that-allows-for-growing-arrays/96148/2 "2023-03-15T20:28:11Z")

</div>

I think this is the use case for a `Reversed` wrapper that constructs a range when `getindex` is called:

```julia
struct Reversed{T<:AbstractArray}
    A::T
end

Base.getindex(a::Reversed, i::Int) = a.A[end-i+1]

v = [1, 2, 3]
a = Reversed(v)
a[1] # 3
push!(v, 4)
a[1] # 4

```

---

<div class="post-metadata">

**Author:** ![g-gundam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/g-gundam/32/47593_2.png) [@g-gundam](https://discourse.julialang.org/u/g-gundam)\
**Post date:** [March 15, 2023, 10:19pm UTC](https://discourse.julialang.org/t/how-to-create-a-reversed-view-that-allows-for-growing-arrays/96148/3 "2023-03-15T22:19:58Z")

</div>

I continue to be amazed by what Julia can do. Thanks for the answer, @simsurace. Thanks for moving the thread, @mbauman .

(For the curious, the OP of this thread was originally a reply in [another thread](https://discourse.julialang.org/t/reverse-view-of-an-array/9657).)

---

<div class="post-metadata">

**Author:** ![torrance](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torrance/32/38990_2.png) [@torrance](https://discourse.julialang.org/u/torrance)\
**Post date:** [March 16, 2023, 5:31am UTC](https://discourse.julialang.org/t/how-to-create-a-reversed-view-that-allows-for-growing-arrays/96148/4 "2023-03-16T05:31:34Z")

</div>

Just a minor note: you only want to _iterate_ in reverse, rather than be able to index, the function `Iterators.reverse` will construct a reverse iterator for your vector, and will automatically accomodate changes to the underlying vector including adding new elements.

e.g.:

```julia
xs = rand(100)
sx = Iterators.reverse(xs)

xs[100] = 2
@assert first(sx) == 2 # true

push!(xs, 3)
@assert first(sx) == 3 # true

```
