yordisprieto

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

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

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

Qqwy

TypeCheck Core Team

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:

  1. 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.
  2. 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).

Where Next?

Popular in Discussions Top

PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
cvkmohan
The upcoming Phoenix 1.6 release looks very interesting. Became a habit to watch the commits - and - what they are bringing in. phx.gen...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39247 209
New
crabonature
I’m still quite new to Elixir. As I understand we got in Elixir “multi guards” as convention to simplify one large guard with or’s?: de...
New
New
jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
New
jsonify
So, is Heroku the only free option for hosting Phoenix/Elixir at this point? I’m not ready to commit to paying monthly and was wondering ...
New
und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
griffinbyatt
Sobelow Sobelow is a security-focused static analysis tool for the Phoenix framework. For security researchers, it is a useful tool for g...
New

Other popular topics Top

greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31107 143
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement