Partial Application for Functions in Julia

1 minute read

Julia is purportedly a multi-paradigm language but I find their support for functional paradigms to be lacking. One feature that I looked for was Currying or Partial Application which corresponds to converting a function of multiple arguments into a sequence of single argument functions and taking a multiple argument function and fixing some of the arguments to produce a new function of lesser arity respectively. Currying does not play nicely with Julias multiple dispatch model, and the creators have seemingly no intention of supporting it in the future if the discussion forums are anything to go by. In Clojure one has the partial function for the latter. Here is my attempt to emulate this behaviour in Julia.

Partial Application

function partial(f,a...)
  (b...) -> f(a...,b...)
end

The function returns a lambda where the a… parameters are fixed. If you don’t know what the ellipsis do, check the documentation for “splat”. One can check the behavour is as expected

julia> function testf(x,y,z)
         2*x+y*y+x*y*z
       end
testf (generic function with 1 method)
julia> testf(2,3,4)
37
julia> partial(testf,2)(3,4)
37
julia> partial(testf,2,3)(4)
37

Of course you could just use a lambda.

Leave a Comment