JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

8.8.1 Processing Arrays with Functions

8.8.1 Processing Arrays with Functions


Suppose we have an array of numbers and we want to compute the mean and standard deviation of those values. We might do that in nonfunctional style like this:

var data = [1,1,3,5,5]; // This is our array of numbers

// The mean is the sum of the elements divided by the number of elements var total = 0; for(var i = 0; i < data.length; i++) total += data[i]; var mean = total/data.length; // The mean of our data is 3

// To compute the standard deviation, we first sum the squares of // the deviation of each element from the mean. total = 0; for(var i = 0; i < data.length; i++) {

var deviation = data[i] - mean;

total += deviation * deviation; } var stddev = Math.sqrt(total/(data.length-1)); // The standard deviation is 2

We can perform these same computations in concise functional style using the array methods map() and reduce() like this (see §7.9 to review these methods):

// First, define two simple functions var sum = function(x,y) { return x+y; }; var square = function(x) { return x*x; };

// Then use those functions with Array methods to compute mean and stddev var data = [1,1,3,5,5]; var mean = data.reduce(sum)/data.length; var deviations = data.map(function(x) {return x-mean;}); var stddev = Math.sqrt(deviations.map(square).reduce(sum)/(data.length-1));

What if we’re using ECMAScript 3 and don’t have access to these newer array methods? We can define our own map() and reduce() functions that use the built-in methods if they exist:

// Call the function f for each element of array a and return // an array of the results. Use Array.prototype.map if it is defined. var map = Array.prototype.map

? function(a, f) { return a.map(f); } // Use map method if it exists

: function(a,f) { // Otherwise, implement our own var results = []; for(var i = 0, len = a.length; i < len; i++) {

if (i in a) results[i] = f.call(null, a[i], i, a); } return results;

};

// Reduce the array a to a single value using the function f and // optional initial value. Use Array.prototype.reduce if it is defined. var reduce = Array.prototype.reduce

? function(a, f, initial) { // If the reduce() method exists. if (arguments.length > 2) return a.reduce(f, initial); // If an initial value was passed.

else return a.reduce(f); // Otherwise, no initial value. } : function(a, f, initial) { // This algorithm from the ES5 specification

var i = 0, len = a.length, accumulator;

// Start with the specified initial value, or the first value in a if (arguments.length > 2) accumulator = initial; else { // Find the first defined index in the array

if (len == 0) throw TypeError(); while(i < len) {

if (i in a) { accumulator = a[i++]; break;

}

else i++; } if (i == len) throw TypeError();

}

// Now call f for each remaining element in the array while(i < len) { if (i in a) accumulator = f.call(undefined, accumulator, a[i], i, a); i++; }

return accumulator; };

With these map() and reduce() functions defined, our code to compute the mean and standard deviation now looks like this:

var data = [1,1,3,5,5]; var sum = function(x,y) { return x+y; }; var square = function(x) { return x*x; }; var mean = reduce(data, sum)/data.length; var deviations = map(data, function(x) {return x-mean;}); var stddev = Math.sqrt(reduce(map(deviations, square), sum)/(data.length-1));

欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/31371.html
友情链接It题库(ittiku.com)| 版权归yishouce.com所有| 友链等可联系 admin#yishouce.com|粤ICP备16001685号-1