# How to apply an in-place function to a slice of a vector?

**URL:** <https://discourse.julialang.org/t/how-to-apply-an-in-place-function-to-a-slice-of-a-vector/108520>\
**Category:** New to Julia\
**Tags:** function, vector, functions, inplace, partial-evaluation\
**Created:** [January 8, 2024, 2:17pm UTC](https://discourse.julialang.org/t/how-to-apply-an-in-place-function-to-a-slice-of-a-vector/108520 "2024-01-08T14:17:44Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Post date:** [January 8, 2024, 2:17pm UTC](https://discourse.julialang.org/t/how-to-apply-an-in-place-function-to-a-slice-of-a-vector/108520/1 "2024-01-08T14:17:44Z")

</div>

I want to have an in-place function that takes a vector as an argument and applies another in-place function to a subarray (in general sense).  
How do I do this?

```julia
function f!(du, u)
    println("\nf called")
    @show u
    du .= reverse(u)
    return nothing
end

"""
Applies f!() to the first 2 elements.
"""
function g!(du, u)
    f!(du[1:2], u[1:2])
    du[3] = 17
    return nothing
end

du = zeros(2)
u = [3, 11]

dug = zeros(3)
ug = [u; 0]

@show f!(du, u)
@show du

@show g!(dug, ug)
@show dug

```

gives

```julia
f called
u = [3, 11]
f!(du, u) = nothing
du = [11.0, 3.0]

f called
u = [3, 11]
g!(dug, ug) = nothing
dug = [0.0, 0.0, 17.0] # why not [11, 3, 17]?

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 8, 2024, 2:22pm UTC](https://discourse.julialang.org/t/how-to-apply-an-in-place-function-to-a-slice-of-a-vector/108520/2 "2024-01-08T14:22:28Z")

</div>

In julia `du[1:2]` gives a copy of the slice, not a view (unfortunately). You can use the `@view` or `@views` macros, or the `view` function directly to construct views.

So your `g!` function should be

```julia
function g!(du, u)
    @views f(du[1:2], u[1:2])
    du[3] = 17
    return nothing
end

```

---

<div class="post-metadata">

**Author:** ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Post date:** [January 8, 2024, 2:26pm UTC](https://discourse.julialang.org/t/how-to-apply-an-in-place-function-to-a-slice-of-a-vector/108520/3 "2024-01-08T14:26:02Z")

</div>

> [@Mason](#):
>
> In julia `du[1:2]` gives a copy of the slice, not a view (unfortunately).

That is, this creates a new object in memory, even without binding it to any name?

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 8, 2024, 2:27pm UTC](https://discourse.julialang.org/t/how-to-apply-an-in-place-function-to-a-slice-of-a-vector/108520/4 "2024-01-08T14:27:22Z")

</div>

Bindings and allocations are totally different concepts, yes.

---

<div class="post-metadata">

**Author:** ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Post date:** [January 8, 2024, 2:34pm UTC](https://discourse.julialang.org/t/how-to-apply-an-in-place-function-to-a-slice-of-a-vector/108520/5 "2024-01-08T14:34:35Z")

</div>

Does this behaviour affect only function calls? Do I understand correctly that slices passed by value?

In REPL, I can update the array with

```julia
u = zeros(3)
u[1:2] = ones(2)

```

and get `[1, 1, 0]` as expected.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 8, 2024, 2:35pm UTC](https://discourse.julialang.org/t/how-to-apply-an-in-place-function-to-a-slice-of-a-vector/108520/6 "2024-01-08T14:35:40Z")

</div>

No, we just made the (IMO misleading) choice to have `u[1:2] = ones(2)` to be special syntax for `setindex!(u, ones(2), 1:2)`.

The `u[1:2]` there is never creating a slice, it’s just part of the `setindex!` syntax.

---

<div class="post-metadata">

**Author:** ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Post date:** [January 8, 2024, 2:49pm UTC](https://discourse.julialang.org/t/how-to-apply-an-in-place-function-to-a-slice-of-a-vector/108520/7 "2024-01-08T14:49:04Z")

</div>

> [@Mason](#):
>
> `u[1:2] = ones(2)` to be special syntax for `setindex!(u, ones(2), 1:2)`.

I’m afraid I don’t get it completely.  
I’m okay with the fact it’s some “special syntax”. But why does it work in REPL and not in a function (where it requires `@view`}? Where else will it not work?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [January 8, 2024, 2:55pm UTC](https://discourse.julialang.org/t/how-to-apply-an-in-place-function-to-a-slice-of-a-vector/108520/8 "2024-01-08T14:55:53Z")

</div>

the distinction isn’t repl vs function it’s before it after an equal sign.

---

<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:** [January 8, 2024, 3:17pm UTC](https://discourse.julialang.org/t/how-to-apply-an-in-place-function-to-a-slice-of-a-vector/108520/9 "2024-01-08T15:17:21Z")

</div>

> [@homocomputeris](#):
>
> I’m afraid I don’t get it completely.

When Julia sees `arr[indices...] = v`, it translates it to `setindex!(arr, v, indices...)` at an early stage. Docs: [`setindex!`](https://docs.julialang.org/en/v1/base/arrays/#Base.setindex!-Tuple%7BAbstractArray,%20Any,%20Vararg%7BAny%7D%7D).

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 8, 2024, 7:48pm UTC](https://discourse.julialang.org/t/how-to-apply-an-in-place-function-to-a-slice-of-a-vector/108520/10 "2024-01-08T19:48:46Z")

</div>

> [@homocomputeris](#):
>
> `du .= reverse(u)`

Incidentally, I believe `reverse(u)` will allocate a intermediare temporary vector. A lazy non-allocated version can be had by using `Iterators.reverse` or reverse the indices of a `view`, ie

```julia
du .= @view u[end:-1:begin]

```

---

<div class="post-metadata">

**Author:** ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Post date:** [January 9, 2024, 10:27am UTC](https://discourse.julialang.org/t/how-to-apply-an-in-place-function-to-a-slice-of-a-vector/108520/11 "2024-01-09T10:27:48Z")

</div>

Thanks. Reverse is there only to provide a MWE.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 9, 2024, 11:02am UTC](https://discourse.julialang.org/t/how-to-apply-an-in-place-function-to-a-slice-of-a-vector/108520/12 "2024-01-09T11:02:07Z")

</div>

Sure, but since the question concerns in-place operations and allocations, and since I expect almost everything in an MWE to be relevant, I assumed this was too.
