Best practice for storing functions in a struct

In case it’s helpful, the general term for caching the arguments to a function (and only actually doing the computation for new inputs) is “memoization”. And, in fact, there’s a Julia package that can do it for you (in simple cases): https://github.com/simonster/Memoize.jl

As for functors, that’s just how we refer to a struct that stores some parameters and is itself callable like a function. They’re actually implemented exactly the same way as anonymous functions in Julia, except that we give them a name:

julia> struct Adder
         x::Int
       end

julia> function (a::Adder)(y)  # this is how we make an `Adder` callable like a function
         a.x + y
       end

julia> a1 = Adder(1)
Adder(1)

julia> a1(2)
3

Storing functors like this instead of anonymous functions may make your code easier to reason about.

2 Likes