# Storage of functions with pre-defined argument values

**URL:** <https://discourse.julialang.org/t/storage-of-functions-with-pre-defined-argument-values/103502>\
**Category:** New to Julia\
**Tags:** function\
**Created:** [September 4, 2023, 9:58am UTC](https://discourse.julialang.org/t/storage-of-functions-with-pre-defined-argument-values/103502 "2023-09-04T09:58:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charly\_Theo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charly_theo/32/19717_2.png) [@Charly\_Theo](https://discourse.julialang.org/u/Charly_Theo)\
**Post date:** [September 4, 2023, 9:58am UTC](https://discourse.julialang.org/t/storage-of-functions-with-pre-defined-argument-values/103502/1 "2023-09-04T09:58:32Z")

</div>

Hi all,

I am trying to store a vector of functions, which can later be retrieved and applied to one or more vectors. An example:

```julia
using ShiftedArrays

fs = Function[ShiftedArrays.lag, diff]
x = rand(10)

for f in fs
    x = fs(x)
end

```

The above works. My issue is that I don’t know how to do it (optimally) if I want to specify some of the argument values before storing them. Let’s say that I want the third lag, e.g. lag(x, n = 3). While I could store this as, say, lag3(x) = ShiftedArrays.lag(x, 3), the argument ‘n’ is randomly selected when the code runs, and hence I would have to pre-define maybe 100 such functions lag1(x)…lag100(x) which seems not ideal.

I have thought about storing this in structs, but don’t know if this is the “correct”/optimal way to go either, e.g.:

```julia
using ShiftedArrays

mutable struct Lag
    n::Int
end

my_lag(x::AbstractVector, t::Lag) = ShiftedArrays.lag(x, t.n)

x = rand(10)
my_lag(x, Lag(3)

```

Going down this road, I would store the struct Lag(3) for later usage.

Is there a more elegant way to solve this given that the lag parameter ‘n’ isn’t determined until the program runs and with the understanding that there may be many more functions than just lag and diff?

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [September 4, 2023, 10:08am UTC](https://discourse.julialang.org/t/storage-of-functions-with-pre-defined-argument-values/103502/2 "2023-09-04T10:08:29Z")

</div>

Does this help?

```julia
julia> f(x, y) = x + y
f (generic function with 1 method)

julia> fix_f(y) = x -> f(x, y)
fix_f (generic function with 1 method)

julia> f_3 = fix_f(3)
#3 (generic function with 1 method)

julia> f_3(10)
13

```

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [September 4, 2023, 10:09am UTC](https://discourse.julialang.org/t/storage-of-functions-with-pre-defined-argument-values/103502/3 "2023-09-04T10:09:36Z")

</div>

You might be looking for [anonymous functions](https://docs.julialang.org/en/v1/manual/functions/#man-anonymous-functions). Then you can do for example:

```julia
using ShiftedArrays
randlag = rand(0:10)
fs = [x -> ShiftedArrays.lag(x,3), x -> ShiftedArrays.lag(x,5), x -> ShiftedArrays.lag(x, randlag)]

```

You can use anonymous functions like every other value, e.g. pass the around or store them in variables for later use.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [September 4, 2023, 11:47am UTC](https://discourse.julialang.org/t/storage-of-functions-with-pre-defined-argument-values/103502/4 "2023-09-04T11:47:27Z")

</div>

You can make the struct approach more elegant by making it callable.

```julia
(t::Lag)(x::AbstractVector) = ShiftedArrays.lag(x, t.n)

f = Lag(3)
x = rand(10)
f(x)

```

---

<div class="post-metadata">

**Author:** ![Charly\_Theo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/charly_theo/32/19717_2.png) [@Charly\_Theo](https://discourse.julialang.org/u/Charly_Theo)\
**Post date:** [September 5, 2023, 6:57am UTC](https://discourse.julialang.org/t/storage-of-functions-with-pre-defined-argument-values/103502/5 "2023-09-05T06:57:30Z")

</div>

Thank you @barucden, @abraemer, and @GunnarFarneback for your quick feedback! All of your suggestions could well end up being the final solution. For now I will try out the anonymous function approach.
