Hey,
I just came across a problem that I am sure can be solved elegantly in Julia but I am lacking a good starting angle. Assume that I have a collection of functors (callable types) of the form
struct Functor
f::function
end
(f::Functor)(x)::Real = f.f(x)
This makes sure that all objects of type Functor
can be called with a single argument x
(which would have to be checked in a proper constructor…)
What would be the best way to implement point-wise arithmetic on these functors? I.e., I’d like to be able to create a new functor via arbitrary Julia expressions treating functor objects as reals. Something like
a = Functor(sin)
b = Functor(cos)
c = @compose a + exp(b) # c should also be of type Functor
c(1) # should return sin(1) + exp(cos(1))
should work. Not quite sure but I suspect this is a macro problem since I essentially need to treat the argument to @compose
as Julia expression, replace all references to functors by their pointwise evaluation and then evaluate the expression.
Are macros the way to go here or are there better options?