# Non-mutating array functions

**URL:** https://discourse.julialang.org/t/non-mutating-array-functions/115948
**Category:** Internals & Design
**Created:** [June 20, 2024, 7:11pm UTC](https://discourse.julialang.org/t/non-mutating-array-functions/115948 "2024-06-20T19:11:08Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [June 20, 2024, 7:11pm UTC](https://discourse.julialang.org/t/non-mutating-array-functions/115948/1 "2024-06-20T19:11:08Z")

</div>

I sometimes want non-mutating versions of `push!` and `append!` which copy their mutable inputs. [Another thread](https://discourse.julialang.org/t/non-mutating-deleteat-splice-function/11139) asks about non-mutating versions of `splice!` and `deleteat!`.

Do we already have these somewhere? Can we add them to Base?

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [June 20, 2024, 8:30pm UTC](https://discourse.julialang.org/t/non-mutating-array-functions/115948/2 "2024-06-20T20:30:01Z")

</div>

concatenation?

---

<div class="post-metadata">

### Author: ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)
#### Post date: [June 20, 2024, 8:30pm UTC](https://discourse.julialang.org/t/non-mutating-array-functions/115948/3 "2024-06-20T20:30:58Z")

</div>

Yes, the non-mutating version of `append!` is simply `vcat`.

Here’s a non-mutating version of `deleteat!`:

```julia
julia> using Accessors: delete, @optic

julia> delete([3,4], @optic _[1]) == [4]
true

```

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [June 20, 2024, 8:36pm UTC](https://discourse.julialang.org/t/non-mutating-array-functions/115948/4 "2024-06-20T20:36:02Z")

</div>

> <https://github.com/JuliaLang/julia/issues/53040>
>
> I want a non-mutating version of \`append!\` that puts two sequences together.
> 
> …
> \`\`\`jl
> julia\> (append(x::T, y::T) where T\<: AbstractVector) = append!(copy(x), y);
> 
> julia\> (append(x::T, y::T) where T\<:AbstractString)= join(\[x, y\]);
> 
> julia\> append(\[:a, :b\], \[:c, :d\])
> 4-element Vector{Symbol}:
> :a
> :b
> :c
> :d
> 
> julia\> append("ab", "cd")
> "abcd"
> \`\`\`
> 
> 
> When the argument types differ we could have some promotion logic.
> 
> 
> Here are the properties implied by the "sequence" requirement. They should hold unless one of the functions on the left hand side throws an error.
> 
> 
> \- \`append(eachindex(a), eachindex(b)) == eachindex(append(a,b))\`.
> \- \`((x in a) || (x in b)) iff (x in append(a,b))\`
> \- \`i isa Integer implies a\[i\] == append(a,b)\[i\]\`
> \- \`i isa Integer implies b\[i\] == append(a,b)\[length(a)+i\]\`
> 
> I think that means for \`Matrix\` arguments it's \`hcat\` and for \`Set\` it's \`union\`, for \`Tuple\` it does the obvious thing. For \`NamedTuple\` it can be defined just like \`Tuple\` unless \`any(map(==, keys(a), keys(b)))\`, in which case it can error or last wins (I prefer the error, probably). I'm not sure what to say about \`Dict\` and \`Dictionary\`. This could be weakened to also support generators; idk if that's worth doing.

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [June 20, 2024, 8:36pm UTC](https://discourse.julialang.org/t/non-mutating-array-functions/115948/5 "2024-06-20T20:36:09Z")

</div>

haha I forgot that’s my issue

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [June 20, 2024, 9:08pm UTC](https://discourse.julialang.org/t/non-mutating-array-functions/115948/6 "2024-06-20T21:08:49Z")

</div>

> [@greatpet](#):
>
> ```julia
> julia> delete([3,4], @optic _[1]) == [4]
> 
> ```

It’s even nicer in practice:

```julia
x = [3, 4]
y = @delete x[1]

```

For `push!`, there’s of course `@insert` (:

```julia
y = @insert last(x) = 5

```
