Javascript Partial application
When working with Javascript there are a few core concepts that will make life easier and keep your code expressive. One of these core concepts is partial application, a very narrow definition would be: Defining a function that enables the application of another function with an incomplete set of arguments.
But what does that mean?
It means that I can define a function called tieUsingWindsor that takes a tie argument and calls a set of functions with default values. For example it would call tieTie which takes the tie argument, a knot argument and a pressure argument. by defining the tieUsingWindsor function we can apply the tieTie function and only need a single argument.
Code:
(function (){
"use strict";
//This method will require all
//three parameters to be passed.
var tieTie = function(tie, knot, tightness) {
var text = 'tied the %s using a
%s knot and tightness %d';
console.log(text, tie, knot, tightness);
};
//we can apply tieTie with only a single parameter.
var tieUsingWindsor = function(tie) {
var knot = 'windsor',
tightness = 20;
//apply tieTie.
tieTie(tie, knot, tightness);
};
//Partial application.
tieUsingWindsor('red tie');
//same result as above but had to pass all parameters.
tieTie('red tie', 'windsor', 20);
}());