I was asked what’s the relationship between partial function application and closures.
I would say there isn’t any, unless I’m missing the point.
Let’s say I’m writing in python and I have a very simple function MySum defined as follows:
MySum = lambda x, y : x + y;
Now I’m fixing one parameter to obtain a function with smaller arity which returns the same value that MySum would return if I called it with the same parameters (partial application):
MyPartialSum = lambda x : MySum(x, 0);
I could do the the very same thing with C:
int MySum(int x, int y) { return x + y; }
int MyPartialSum(int x) { return MySum(x, 0); }
So, the dumb question is: what’s the difference? Why should I need closures for partial applications? These codes are equivalent, I don’t see what’s the bound with closures and partial application.
Partial application is a technique whereby you take an existing function and a subset of it’s arguments, and produce a new function that accepts the remaining arguments.
In other words, if you have function
F(a, b), a function that applies partial application ofawould look likeB(fn, a)whereF(a, b) = B(F, a)(b).In your example you’re simply creating new functions, rather than applying partial application to the existing one.
Here’s an example in python:
This creates a closure over the supplied function and argument. A new function is returned that calls the first function with new argument signature. The closure is important – it allows
fnaccess toarg. Now you can do this sort of thing:I’ve usually heard this referred to as currying.