Here is the last part of the videos on partial application. We improve the code from previous videos and make it handle a variable number of parameters.
In the video, I mention that n is the number of times we will have to apply partial application before the underlying function is called. That is misleading. The situation is actually a little better than that. n is really the number of parameters the underlying function cares about. After n parameters are passed in, the original function will be called. Whether or not we pass in the parameters all at once (a, b, c) or one at a time.
Here is the final code:
1 <html> 2 <body> 3 <scriptlanguage="javascript"> 4 5 Function.prototype.partial = function(n){ 6 var original_function = this; 7 var args = []; 8 9 var recursive_partial = function(){ 10 args = args.concat([].slice.apply(arguments)); 11 if(args.length >= n){ 12 return original_function.apply(null, args); 13 } 14 return recursive_partial; 15 }; 16 return recursive_partial; 17 } 18 19 function addTwoNumbers(a,b){return a+b;} 20 function addThreeNumbers(a,b,c){return a+b+c;} 21 22 </script> 23 24 Partial application #3 25 26 </body> 27 </html>