# Idiom to preinitialize function parameters

**URL:** <https://discourse.julialang.org/t/idiom-to-preinitialize-function-parameters/126676>\
**Category:** General Usage\
**Created:** [March 7, 2025, 12:31pm UTC](https://discourse.julialang.org/t/idiom-to-preinitialize-function-parameters/126676 "2025-03-07T12:31:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nilsbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilsbecker/32/12157_2.png) [@nilsbecker](https://discourse.julialang.org/u/nilsbecker)\
**Post date:** [March 7, 2025, 12:31pm UTC](https://discourse.julialang.org/t/idiom-to-preinitialize-function-parameters/126676/1 "2025-03-07T12:31:22Z")

</div>

Coming from a language with curried function arguments, when I try to precompute some parameters for a function that is to be used in a hot loop, I end up with code like this:

```julia
precompute = length
function setup(input)
  param = precompute(input)
  compute(x) = (x+param; nothing)
  return compute
end
mykernel = setup("complicated input that takes time to initialize")
for i in 1:1000
  mykernel(i)
end

```

So parameters are often stored in closures. The code reads a bit cumbersome and error messages are riddled with a lot of cryptic function types. Is there a better way to do this in terms of readability and/or perfomance? I’ve read the advice to pass parameters around, possibly aided by macros in Parameters.jl ? How would you do it?

---

<div class="post-metadata">

**Author:** ![nilsbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilsbecker/32/12157_2.png) [@nilsbecker](https://discourse.julialang.org/u/nilsbecker)\
**Post date:** [March 7, 2025, 12:50pm UTC](https://discourse.julialang.org/t/idiom-to-preinitialize-function-parameters/126676/2 "2025-03-07T12:50:12Z")

</div>

So I guess with explicit parameter passing it would look more like

```julia
function compute(param, x)
  x+param
  nothing
end
function setup_param(input)
  return length(input)
end
mykernel2(x) = compute(param, x)

```

with flat, rather than nested, functions definitions. When parameters become too many, I could pack them into named tuples, and use `@unpack` from UnPack.jl to take out just the ones I need inside the hot loop. Is that better?

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [March 7, 2025, 12:51pm UTC](https://discourse.julialang.org/t/idiom-to-preinitialize-function-parameters/126676/3 "2025-03-07T12:51:42Z")

</div>

I’m not sure this will solve your problem but I have two answers in mind.

1. One can define function arguments using previous function arguments.

```julia
function f(x, y=exp(x), z=x*y)
    return x + y + z
end

```

1. It is a frequent pattern in Julia to define two versions of a function: one in-place and one out-of-place. Typically this may look like:

```julia
function mul!(result, A, b)
    # multiply A by b and store in result
end

function *(A, b)
    result = # preallocate
    mul!(result, A, b)
    return result
end

```

Does this help?

---

<div class="post-metadata">

**Author:** ![nilsbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilsbecker/32/12157_2.png) [@nilsbecker](https://discourse.julialang.org/u/nilsbecker)\
**Post date:** [March 7, 2025, 1:19pm UTC](https://discourse.julialang.org/t/idiom-to-preinitialize-function-parameters/126676/4 "2025-03-07T13:19:52Z")

</div>

1. seems like a clever way to do initialization but I don’t quite see how to apply it here. If i do

```julia
input = 4
for i in 1:1000
  f(input; z=i)
end

```

then `y` will get computed automatically, but in every iteration, no?

1. is something that I have become aware of; typically the ‘kernel’ is the inplace function, for speed. But the non-varying input of the kernel still needs setting up first, which is what I was concerned about.

---

<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:** [March 7, 2025, 1:22pm UTC](https://discourse.julialang.org/t/idiom-to-preinitialize-function-parameters/126676/5 "2025-03-07T13:22:15Z")

</div>

> [@nilsbecker](#):
>
> So parameters are often stored in closures. The code reads a bit cumbersome and error messages are riddled with a lot of cryptic function types. Is there a better way to do this in terms of readability and/or perfomance? I’ve read the advice to pass parameters around, possibly aided by macros in [Parameters.jl](https://juliaregistries.github.io/General/packages/redirect_to_repo/Parameters) ? How would you do it?

I don’t think Parameters.jl is really needed here. You can just use NamedTuples and destructuring syntax.

For example:

```julia
function setup(input)
    (; p1=exp(1-input), p2=rand())
end
function f(x, params)
    (;p1, p2) = params
    x * p1 - p2
end

let params = setup(10.0)
    for i in 1:1000
        f(x, params)
    end
end

```

or of course you can just use keyword arguments like so:

```julia
function f(x; p1, p2)
    x * p1 - p2
end

let params = setup(10.0)
    for i in 1:1000
        f(x; params...)
    end
end

```

---

<div class="post-metadata">

**Author:** ![Alexander\_Knudson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander_knudson/32/215656_2.png) [@Alexander\_Knudson](https://discourse.julialang.org/u/Alexander_Knudson)\
**Post date:** [March 7, 2025, 4:41pm UTC](https://discourse.julialang.org/t/idiom-to-preinitialize-function-parameters/126676/6 "2025-03-07T16:41:24Z")

</div>

Why not create a struct that contains the precomputed parameters and use it as a [functor](https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects)?

```julia
# struct to hold precomputed parameters
struct MyKernel{T}
    param::T
end

# setup method to create the kernel
function setup(input, precompute)
    # precompute stuff...
    param = precompute(input)
    return MyKernel{typeof(param)}(param)
end

# kernel computation
function (kernel::MyKernel{T})(x::S) where {T, S}
    return (x + kernel.param, nothing)
end

```

Then your code would look like this:

```julia
mykernel = MyKernel("complicated input...")

for i in 1:1000
    mykernel(i)
end

```

I’m not sure if this is more/less efficient than a closure, but it is type stable and (for me) easier to reason about where the parameters are coming from.

---

<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:** [March 7, 2025, 6:27pm UTC](https://discourse.julialang.org/t/idiom-to-preinitialize-function-parameters/126676/7 "2025-03-07T18:27:04Z")

</div>

Yep, there’s nothing wrong with this approach. It’s basically the closure one but with better stacktraces and a bit more boiler-plate.
