# JS Authoritative Guide Learning Summary--8.8 functional programming and 8.8.1 using functions to manipulate arrays

Source: Internet
Author: User

Content Highlights:

Unlike Lisp, Haskell, JS is not a functional programming language, but in JS you can manipulate functions like objects,

In other words, the function programming technique can be applied in JS. Array methods in ES5, such as map () and reduce (), are well suited for functional programming styles.

I. Working with arrays using functions

Suppose there is an array, the array elements are numbers, and we want to calculate the mean and standard deviation of those elements.

In the case of non-functional programming styles, the code is:

var data = [1,1,3,5,5]; Here is the array to be processed

Average is the sum and value of all 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 average is 3.

Calculate the standard deviation, first calculate the square of the deviation after each data minus the average and then sum

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 value of the standard deviation is 2

You can use the array method map () and reduce () to achieve the same calculations,

First, define two simple functions

var sum = function (x, y) {return x+y;};

var square = function (x) {return x*x;};

These functions and array methods are then used to calculate the average and standard deviation

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));

Console.log (mean); 3
Console.log (deviations); [ -2,-2,0,2,2]
Console.log (Deviations.map (square));//[4, 4, 0, 4, 4]
Console.log (Deviations.map (square). reduce (sum)); 16
Console.log (StdDev); 2

ES3 does not contain these array methods, and we can customize the map () and reduce () functions if there are no built-in methods :

Call the function f () for each array element and return an array of results. If ARRAY.PROTOTYPE.MAP is defined, use this method.

var map = Array.prototype.map

? function (a,f) {return a.map (f);}

: function (a,f) {

var result = [];

for (var i =0,len=a.length;i<len;i++) {

if (i in a) results[i] = F.call (null,a[i],i,a);

}

return result;

};

Use the function f () and optional initial values to reduce the array A to a value, and if Array.prototype.reduce exists, use this method

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 is passed in

else return a.reduce (f); Otherwise there is no initial value

}

: function (a,f,initial) {//This algorithm comes from the ES5 specification

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

Start with a specific initial value, or the first value is taken from a

if (ARGUMENTS.LENGTH>2) accumulator = initial;

else{

if (Len ==0) throw TypeError ();

while (I<len) {

if (i in a) {

accumulator = a[i++];

Break

}

else i++;

}

if (i==len) throw TypeError ();

}

For the remaining elements in the array, call F () in turn

while (I<len) {

if (i in a) accumulator = F.call (undefined,accumulator,a[i],i,a);

i++;

}

return accumulator;

};

use the defined map () and reduce () functions to calculate the code for the mean and standard deviation:

var data = [1,1,3,5,5];

var sum = function (x, y) {return x+y;};

var square = function (x, y) {return x*y;};

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));

JS Authoritative Guide Learning Summary--8.8 functional programming and 8.8.1 using functions to manipulate arrays

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.