# How can I define functions inside of functions in a performant way?

**URL:** <https://discourse.julialang.org/t/how-can-i-define-functions-inside-of-functions-in-a-performant-way/70213>\
**Category:** Performance\
**Created:** [October 22, 2021, 1:34pm UTC](https://discourse.julialang.org/t/how-can-i-define-functions-inside-of-functions-in-a-performant-way/70213 "2021-10-22T13:34:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![giancarloantonucci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giancarloantonucci/32/202551_2.png) [@giancarloantonucci](https://discourse.julialang.org/u/giancarloantonucci)\
**Post date:** [October 22, 2021, 1:34pm UTC](https://discourse.julialang.org/t/how-can-i-define-functions-inside-of-functions-in-a-performant-way/70213/1 "2021-10-22T13:34:03Z")

</div>

I want to define functions `f1` and `f2` (to be inputted inside a struct `S`) from another function `f`, defined at global scope, using another auxiliary function `fun`:

```julia
mutable struct S
    f1::Function
    f2::Function
end

function fun(f)
    f1 = f
    f2 = x -> 2 * f1(x)
    # or function f2(x); 2 * f1(x); end
    S(f1, f2)
end

f(x) = x
s = fun(f)

```

Now, this works:

```julia
typeof(s.f1)
> typeof(f)

```

However,

```julia
typeof(s.f2)
> var"#f2#1"{typeof(f)}

```

which I guess will lead to performance and time losses along the line. Is there a better way to do it?

---

<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:** [October 22, 2021, 1:36pm UTC](https://discourse.julialang.org/t/how-can-i-define-functions-inside-of-functions-in-a-performant-way/70213/2 "2021-10-22T13:36:27Z")

</div>

Note that getting `f1` or `f2` from `S` will be type unstable. You might want

```julia
mutable struct S{F1, F2}
    f1::F1
    f2::F2
end

```

---

<div class="post-metadata">

**Author:** ![giancarloantonucci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giancarloantonucci/32/202551_2.png) [@giancarloantonucci](https://discourse.julialang.org/u/giancarloantonucci)\
**Post date:** [October 22, 2021, 1:40pm UTC](https://discourse.julialang.org/t/how-can-i-define-functions-inside-of-functions-in-a-performant-way/70213/3 "2021-10-22T13:40:10Z")

</div>

Yes, of course, I always forget to mention that I already do that when I write MWEs. A better representation of my problem would then be:

```julia
mutable struct S{f1_T, f2_T}
    f1::f1_T
    f2::f2_T
end

function fun(f)
    f1 = f
    f2 = x -> 2 * f1(x)
    # or function f2(x); 2 * f1(x); end
    S(f1, f2)
end

f(x) = x
s = fun(f)

typeof(s.f1)
> typeof(f)

typeof(s.f2)
> var"#f2#1"{typeof(f)}

```

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [October 22, 2021, 1:43pm UTC](https://discourse.julialang.org/t/how-can-i-define-functions-inside-of-functions-in-a-performant-way/70213/4 "2021-10-22T13:43:43Z")

</div>

Maybe there is a misconception here. `var"#f2#1"{typeof(f)}` isn’t any worse than `typeof(f)`, it’s just that’s an anonymous function so there are no better ways of representing it in the REPL.

Also, did you know you can define one-line functions like

```julia
f2(x) = 2 * f1(x)

```

? 🙂

---

<div class="post-metadata">

**Author:** ![giancarloantonucci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giancarloantonucci/32/202551_2.png) [@giancarloantonucci](https://discourse.julialang.org/u/giancarloantonucci)\
**Post date:** [October 22, 2021, 2:53pm UTC](https://discourse.julialang.org/t/how-can-i-define-functions-inside-of-functions-in-a-performant-way/70213/5 "2021-10-22T14:53:01Z")

</div>

> [@giordano](#):
>
> Maybe there is a misconception here. `var"#f2#1"{typeof(f)}` isn’t any worse than `typeof(f)` , it’s just that’s an anonymous function so there are no better ways of representing it in the REPL.

I see.

> [@giordano](#):
>
> Also, did you know you can define one-line functions like
> 
> ```julia
> f2(x) = 2 * f1(x)
> 
> ```
> 
> ? 🙂

Actually, yes, but I had an issue (on julia v1.0) that prevented me to use that form, hence my example. However, I’m not able to reproduce that issue now (neither in v1.6 nor in v1.0) so I guess that’s sorted.

---

<div class="post-metadata">

**Author:** ![giancarloantonucci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giancarloantonucci/32/202551_2.png) [@giancarloantonucci](https://discourse.julialang.org/u/giancarloantonucci)\
**Post date:** [October 22, 2021, 3:02pm UTC](https://discourse.julialang.org/t/how-can-i-define-functions-inside-of-functions-in-a-performant-way/70213/6 "2021-10-22T15:02:32Z")

</div>

A follow-up question is: is this a good design pattern (in terms of performances) to do this kind of things? To be absolutely clear, this is the test case I have in mind:

```julia
module M
    mutable struct S{f1_T, f2_T}
        f1::f1_T
        f2::f2_T
    end

    function fun(f)
        f1 = f
        f2(x) = 2 * f1(x)
        S(f1, f2)
    end

    export S, fun
end

using .M

f(x) = x
s = fun(f)

typeof(s.f1)
typeof(s.f2)

```

I tried instead to define `f1` and `f2` as stubs outside `fun` and then overload them with new methods using `fun`, but that’s of course just worse.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 22, 2021, 3:29pm UTC](https://discourse.julialang.org/t/how-can-i-define-functions-inside-of-functions-in-a-performant-way/70213/7 "2021-10-22T15:29:12Z")

</div>

I think that making the struct mutable does not make sense, because each function has its own type. You won’t be able to replace the function by another one (I think, can’t check now).

Concerning the performance, it is hard to tell, it depends on what you will be doing with such structure.

---

<div class="post-metadata">

**Author:** ![giancarloantonucci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giancarloantonucci/32/202551_2.png) [@giancarloantonucci](https://discourse.julialang.org/u/giancarloantonucci)\
**Post date:** [October 22, 2021, 3:42pm UTC](https://discourse.julialang.org/t/how-can-i-define-functions-inside-of-functions-in-a-performant-way/70213/8 "2021-10-22T15:42:24Z")

</div>

> [@lmiq](#):
>
> I think that making the struct mutable does not make sense, because each function has its own type. You won’t be able to replace the function by another one (I think, can’t check now).

That’s very true; I didn’t think it through.

> [@lmiq](#):
>
> Concerning the performance, it is hard to tell, it depends on what you will be doing with such structure.

The idea is to take a function `f` and use `fun` to compute its derivatives (say, first and second ones, with `ForwardDiff` and/or `FiniteDifferences`), saving the results into `f1` and `f2`.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 22, 2021, 3:48pm UTC](https://discourse.julialang.org/t/how-can-i-define-functions-inside-of-functions-in-a-performant-way/70213/9 "2021-10-22T15:48:05Z")

</div>

For that the data structure is not important. The point is if you are planing to have functions that have this structure as a parameter, and if the structure changes frequently (thus begin type unstable) in your code.

If you using the structure just to store two functions (coming from wherever they come from) and then the expensive computation is done inside these functions (f1 and f2), how you stored them will not affect performance.

---

<div class="post-metadata">

**Author:** ![giancarloantonucci](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giancarloantonucci/32/202551_2.png) [@giancarloantonucci](https://discourse.julialang.org/u/giancarloantonucci)\
**Post date:** [October 22, 2021, 3:52pm UTC](https://discourse.julialang.org/t/how-can-i-define-functions-inside-of-functions-in-a-performant-way/70213/10 "2021-10-22T15:52:04Z")

</div>

> [@lmiq](#):
>
> If you using the structure just to store two functions (coming from wherever they come from) and then the expensive computation is done inside these functions (f1 and f2), how you stored them will not affect performance.

That’s it. Thanks!
