Need suggestions on how to convert from OOP library to Julia

Something which has a constructor and a single method sounds a lot like a functor to me, and that could be a nice way to set up your code. For example:

julia> struct Adder
         x::Int
       end

julia> function (adder::Adder)(in)
         adder.x + in
       end

julia> struct Multiplier
         x::Int
       end

julia> function (mult::Multiplier)(in)
         mult.x * in
       end

julia> a = Adder(1)
Adder(1)

julia> m = Multiplier(2)
Multiplier(2)

julia> a(5)
6

julia> m(5)
10

Now that we’ve set things up in this way, chaining modules is exactly the same as chaining functions:

julia> m(a(5))
12

You can use the |> operator to write things in order from input to output, which may be more natural for pipelines:

julia> 5 |> a |> m
12

And you can use the \circ operator to compose functions together:

julia> ma = m ∘ a
Multiplier(2) ∘ Adder(1)

julia> ma(5)
12
1 Like