In a recent project, I’ve experimented with “partial()” to simplify object creation. Partially applying a function, similar to currying, is a technique in which a new function is synthesized by pre-filling the arguments to an existing function. Python’s implementation of partial is available in two separate modules: functools and functional. It’s best understood with an example, albeit contrived:
Instead of defining a factory class and a creation method on that class, I define creation function which accepts the dependencies as parameters, partially apply them and use the new function as my “creation function.” I prefer the second example, I enjoyed writing it, but both examples perform the same function with essentially the same number of lines and ultimately my preference is as basic as preferring a functional style of programming over object oriented.
So I’m wondering, how do other developers use currying or partial application in their projects?
Related posts:





Is this the same thing as “currying”?
Ben Scheirman - 7 Dec 10 at 11:16 am
Technically no, but my functional programming-fu is a weak. I’ll leave it up to the experts: http://srfi.schemers.org/srfi-26/mail-archive/msg00015.html
Sheheryar Sewani - 7 Dec 10 at 11:55 am
The other day I used partial functions in JavaScript to implement generators.
Here’s a simple example of a loop that never ends:
function neverEndingLoop() {
var i = 0;
return {
next: function() {
return i++;
}
};
}
var loop = neverEndingLoop();
// Dump 100 numbers from the "iterator"
for(var n=0; n < 100; n++) {
console.log(loop.next());
}
Luke Venediger - 9 Jan 11 at 5:06 am