September 29, 2011

partial application in Javascript part 3

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         <script language="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>

September 26, 2011

partial application in Javascript part 2

Here is part 2 of the videos on partial application in Javascript. We take the initial prototype and make it handle three parameters.




Here is the code:

 1 <html>
 2     <body>
 3         <script language="javascript">
 4 
 5             Function.prototype.partial3 = function(){
 6                 var original_function = this;
 7 
 8                 var args1 = [].slice.apply(arguments);
 9                 return function(){
10                     var args2 = [].slice.apply(arguments);
11                     return function(){
12                         var args3 = [].slice.apply(arguments);
13                         return original_function.apply(null, args1.concat(args2).concat(args3));
14                     }
15                 };
16             };
17 
18             function addThreeNumbers(a,b,c){return a+b+c;}
19 
20         </script>
21 
22         Partial application #2
23 
24     </body>
25 </html>
26 

September 23, 2011

partial application in Javascript part 1

I recently recorded a few videos on partial application in JavaScript. In part 1, I explain what it is and I provide a short code snippet as a first draft. I will build upon the code snippet in the subsequent videos.




Here is the code that I am using:
<html>
    <body>
        <script language="javascript">

            Function.prototype.partial = function(){
                var original_function = this;
                var args1 = [].slice.apply(arguments);

                return function(){
                    var args2 = [].slice.call(arguments);
                    return original_function.apply(null, args1.concat(args2));
                };
            };

            function addTwoNumbers(a,b){return a+b;}

        </script>

        Partial application #1

    </body>
</html>