Multiple dispatch on function types

aberges
Sat, 22 Jul 2023 15:29:07 +0000
New to Julia

I’m designing a small module to compute fractals. I have implemented 2 algorithms: escape time and distance estimation. These algorithms have the exact same arguments except that distance estimation needs 1 more.

On the main code, I have the generic compute function and I’m leveraging multiple dispatch on the arguments to use different variations for Fatou and Mandelbrot sets, and now wanted to implement the same for the 2 classes of algorithms.

function compute(s::FatouSet, canvas::Canvas, algorithm::typeof(escape_time))::Array{Float64}
    c, f, _, max_iter, radius = s
    construct_mesh(canvas) .|> x -> algorithm(z -> f(z, c), x, max_iter, radius)
end

function compute(s::FatouSet, canvas::Canvas, algorithm::typeof(distance_estimation))::Array{Float64}
    c, f, df, max_iter, radius = s
    construct_mesh(canvas) .|> x -> algorithm(z -> f(z, c), df, x, max_iter, radius)
end

I read on the docs that functions have a singleton type, and was hoping to use that to differentiate the 2 implementations.

The different implementations are only needed because of that extra argument that makes them different, so maybe a better way exists.

Edit: added implementation details.

17 posts - 5 participants