I needed median and mean functions for something I'm working on.  
Should they go into MochiKit somewhere? Are they correct? Fast enough?

/* http://www.nist.gov/dads/HTML/mean.html */
var mean = function() {
     var data = flattenArguments(arguments);
     return sum(data) / data.length;
};

/* http://www.nist.gov/dads/HTML/median.html */
var median = function() {
     var data = flattenArguments(arguments);
     data.sort(compare);
     if (data.length % 2 == 0) {
         var upper = data.length / 2;
         return (data[upper] + data[upper - 1]) / 2;
     } else {
         return data[(data.length - 1) / 2];
     }
};

 >>> mean(1, [2, [3]]) === 2
true

 >>> mean(1, 2, 3) === 2
true

 >>> median(3, 1, 2) === 2
true

 >>> median(4,2,3,1) === 2.5
true

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"MochiKit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/mochikit
-~----------~----~----~----~------~----~------~--~---

Reply via email to