yordisprieto
Partial Application vs Parameters - which do you prefer?
I was watching some functional programming video where the person codes the exact application in multiple ways.
One of the preferred ways was using partial applications for dependency injection and I kept thinking about some packages I was writing because I didn’t use the partial application because it felt odd in Elixir for some reason.
So I am curious about what would you prefer to read and write every day if you have to.
Using just extra parameters
def get_templates(repo, logger, pagination \\ []) do
# ...
end
Or partial application
def get_templates(repo, logger) do
fn pagination ->
end
end
Most Liked
NobbZ
Without library support I think writing curried functions (your second example is not partial application) is tedious. Also in elixir and erlang curried functions are expensive in terms of reduction count, as they are recursive calls that might not be necessary.
Especially when they are arpitrarily nested.
Also your curried example is not valid elixir, as anonymous functions can’t have optional parameters.
But yes, in general, I like partial application and currying, but in elixir/erlang I only felt to do so during the start, when I came from Haskell and was used to use that style. But now I rarely feel the need.
jola
Where can I read more about this? I don’t understand what you mean. Does this mean that I shouldn’t go crazy with FP in Elixir?
FP doesn’t mean currying. Elixir is a functional programming language, but it doesn’t have any syntactic sugar for currying so using it is, as @NobbZ put it, tedious. In languages like Haskell currying is “built in”. In the case of Haskell, that also hides the fact that everything is curried (except when you make use of it).
For an example of what currying is check out Ramda’s curry, which converts an ordinary function into a curried function in JS Ramda Documentation.
Qqwy
Because functions are not curried by default, working with curried functions is usually non-idiomatic in Elixir. I did write a library called Currying a while back that lets you curry arbitrary functions. However, partially applying functions in the same way as you’d do in Haskell has two problems in Elixir:
- It is slower because the compiler is not opitmized for these kinds of things; curried functions are not ‘built in’ and manually added currying is not removed by the compiler.
- In great contrast to both Haskell and Erlang, in Elixir, the ‘most important argument’ is usually the first argument, rather than the last: when partially applying a function, this is usually the thing we want to fill in last, which does not work well when working with currying-based partial application.
However, partial application based on the two super-helpful special forms that @NobbZ already mentioned as well (fn and &) is super helpful, very idiomatic and widely used in practice.
I do not think there exists a battle between parameters on one hand and partial application on the other: Whenever we use a function, this is where we decide whether we want to fill in all parameters at once, or still keep a couple of them blank (which we can do using fn, &, and/or wrapping the actual function we want to call in a new, descriptively named function). This is a decision for the consumer (the place the function is called) of the code, not the producer (the place the function is defined).







