Seeking short examples to "sell" Julia's type system

This is the reason I started learning Julia a few years ago : I needed complicated algorithms (particle swarm optimisation) to work on non-Float64 ,(in paticulare, MultiFloats and BigFloats). This was NOT possible in python or R without reimplementing a whole library. Few lines of Julia code…

9 Likes

While all of the above examples are really cool (and Julia is amazing of course), I would also mention some drawbacks of the type system. I think if you paint a fair picture people will trust your recommendations more.

2 Likes

Was not aware of this, thank you so much

1 Like

I think that discussion is very one-sided. Its fair to say that not everybody loves the subtyping in Julia but what appear to be downsides as presented in that article often turn out to be really useful in practise. Eg it can be very convenient to subtype an abstract type and then just implement the part of the specification you actually need. And what do you need? Just try it out and the errors will tell you what methods you are missing.

1 Like

I think that whatever view you take on subtyping design, the points raised in that blog article are at a level that I would not bring to an introductory presentation. I would at least go through The type system works well in the follow-up post before that.

1 Like

For an example of “whimsical” but incredibly useful self-defined units, here’s one from a paper I published last year:

It’s a big meta-analysis of many ecological interactions, and by defining our own units for all the predators and prey in addition to the usual physical units, we could easily be sure all the scaling equations were dimensionally consistent.

7 Likes

Yep, there are always pros and cons associated with any system. I suppose my broader point is to avoid saying “the type system is amazing, it can do no wrong”. Chances are someone will correctly point out that it has drawbacks, and this will reduce the impact of selling Julia. For example, the “try and see what happens” approach is annoying for large scale projects - it wastes a lot of time waiting for e.g. CI (especially since the precompile + first run step is so long in Julia).

@jd-foster Yep, I would strive for a balanced view. Be honest with the trade-offs and I think overall they will appreciate it.

1 Like

Here’s an example that doesn’t make types explicit.

Define a function

f(x,y) = -(3*(1-x)^2 * exp(-(x^2) - (y+1)^2)
    - 6*(x/5 - x^3 - y^5) * exp(-x^2 - y^2)
    - 1/3 * exp(-(x+1)^2 - y^2))

Explore it with a plot

using Plots
plotly() # this backend makes nice 3d plots
x = range(-4, step=0.1, stop=4)
y = x
surface(x, y, (x,y)->f(x,y),c=:viridis)

Use multiple dispatch to make a form for a vector (implicit), to use for optimization.

function f(θ)
   x,y = θ
   f(x,y) 
end

Optimize:

results = Optim.optimize(f, θstart, LBFGS(), 
2 Likes

Kind of related to rock-paper-scissors, I stole an idea from a blog post and implemented a Pokemon-based introduction to multiple dispatch: the notebook is here HW1a - Pokémon

1 Like