Changeset 7564 for trunk/src/lang

Show
Ignore:
Timestamp:
03/09/07 02:20:24 (22 months ago)
Author:
alex
Message:

refs #2414

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/lang/array.js

    r6862 r7564  
    8181        }, 
    8282 
    83         reduce: function(/*Array*/arr, initialValue, /*Object|Function*/obj, /*Function*/binary_func){ 
     83        reduce: function(/*Array*/arr, /*Function*/binary_func, /*mixed?*/initialValue, /*Object?*/thisObject){ 
    8484                // summary: 
    8585                //              similar to Python's builtin reduce() function. The result of 
     
    8888                //              this call is used along with the subsequent value from arr, and 
    8989                //              this continues until arr is exhausted. The return value is the 
    90                 //              last result. The "obj" and "initialValue" parameters may be 
    91                 //              safely omitted and the order of obj and binary_func may be 
    92                 //              reversed. The default order of the obj and binary_func argument 
    93                 //              will probably be reversed in a future release, and this call 
    94                 //              order is supported today. 
     90                //              last result. 
    9591                // usage: 
    9692                //              dojo.lang.reduce([1, 2, 3, 4], function(last, next){ return last+next}); 
    9793                //              returns 10 
     94 
     95                // FIXME: changes from 0.4.x calling syntax need to be documented in the 0.9 porting guide!!! 
    9896                var reducedValue = initialValue; 
    99                 if(arguments.length == 1){ 
    100                         dojo.debug("dojo.lang.reduce called with too few arguments!"); 
    101                         return false; 
    102                 }else if(arguments.length == 2){ 
    103                         binary_func = initialValue; 
    104                         reducedValue = arr.shift(); 
    105                 }else if(arguments.lenght == 3){ 
    106                         if(dojo.lang.isFunction(obj)){ 
    107                                 binary_func = obj; 
    108                                 obj = null; 
    109                         } 
    110                 }else{ 
    111                         // un-fsck the default order 
    112                         // FIXME: 
    113                         //              could be wrong for some strange function object cases. Not 
    114                         //              sure how to test for them. 
    115                         if(dojo.lang.isFunction(obj)){ 
    116                                 var tmp = binary_func; 
    117                                 binary_func = obj; 
    118                                 obj = tmp; 
    119                         } 
    120                 } 
    121  
    122                 var ob = obj ? obj : dj_global; 
     97                if(arguments.length == 2){ 
     98                        reducedValue = arr[0]; 
     99                        arr = arr.slice(1); 
     100                } 
     101 
     102                var ob = thisObject || dj_global; 
    123103                dojo.lang.map(arr,  
    124104                        function(val){