Example analysis of JavaScript function programming

Source: Internet
Author: User
Tags array length pow square root

This article mainly introduces the JavaScript function programming, the example analyzes the JavaScript function type programming the related use skill, very has the practical value, the need friend may refer to the

The examples in this article describe JavaScript-functional programming. Share to everyone for your reference. The specific analysis is as follows:

JS, like other dynamic languages, can write higher-order functions, and so-called higher-order functions are functions that can manipulate functions. Because in JS the function is a thorough object, belonging to the first class of citizens, which provides a prerequisite for functional programming.

Here is an example code, from a JS tutorial, the function is to compute the average and standard deviation of the elements of the array, first of all, a list of non-functional programming:


1 2 3 4 5 6 7 8 9 10 11-12 var data = [1,1,3,5,5]; var total = 0; for (var i = 0;i < data.length;i++) Total + = Data[i]; var mean = Tatal/data.length; Average is 3//calculation standard difference total = 0; for (var i = 0;i < data.length;i++) {var deviation = data[i]-mean; Tatal + = deviation * deviation;} var StdDev = Math ,. sqrt (total/(data.length-1));/Standard deviation is 2

To use functional programming, we have predefined help functions (helper functions):


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The

Here we give the complete use of functional programming JS code:


1 2 3 4 5 6 7 8 9 10 var data = [1,1,3,5,5]; var sum = function (x,y) {return x+y;}; var product = function (x,y) {return x*y;}; var neg = partial (product,-1); var square = partial (math.pow,undefined,2); var sqrt = partial (math.pow,undefined,0.5); var reciprocal = partial (math.pow,undefined,-1); All right, come on, orgasm.:) var mean = product (reduce (data,sum), reciprocal (data.length)); var StdDev = sqrt (Product (Data,compose (square,partial (Sum,neg))), sum), mean (SUM (reciprocal ,-1)));

In addition to the reduce and map functions, the other functions are given in front of them. The reduce function resembles the inject function in Ruby:


1 2 ary = (1..10). To_a ary.inject (0) {|sum,i|sum + i}//Result 55

JS is written as follows:


1 2 3 4 var ary = [1,2,3,4,5,6,7,8,9,10] ary.reduce (function (sum,i) {return sum + i;},0);

0 is the initial value of sum, if omitted, sum is the value of the first element of the array, which can be omitted here.

The map function is also simple, similar to the operation of each element of the array, and then returns an operation of the array, in Ruby code, for example, JS code similar to this:


1 2 A = (1..3). To_a; #数组 [1,2,3] a.map {|x| x*2} #返回新数组 [2,4,6]

Let's analyze the long string of code below:

Sum and product define the functions for adding and multiplying elements;

Neg is also a function equivalent to: Product ( -1,X), that is, the X value is negative;

The square function is equivalent to: Math.pow (x,2), which computes the square value of x, noting that the second parameter of partial here is undefined, which means that the form participant here is filled by the first argument; it's clear: square (x) function equals Math.pow. (x,2).

The SQRT function is similar to square, and functions equivalent to: Math.pow (x,0.5), which is equivalent to calculating X's two-second square.

The last function reciprocal is also not difficult, equivalent to: Math.pow (x,-1), that is, calculate the negative side of X, which is equivalent to calculate the reciprocal of X.

Here's how to knead the various functions above the bird:

Look at the average of the calculation, very simple: first of all, calculate the elements of the array and then multiply the countdown to the length of the array, that is, the array and/array length.

Finally look at the seemingly difficult standard deviation, we'd better look inward:

First look at the layer that contains the neg:


1 2 Equivalent to function sum ( -1 * mean + x) partial (Sum,neg (mean)

Here's a look at the Compose function:


1 2 3 4 The following is an equivalent substitution on the source code, which can again be equivalent to://square (sum ( -1*mean + x)), expand again (I peel, I peel, I peel onions ...)://math.pow (SUM ( -1*mean + x), 2); Compose (square,sum ( -1*mean + x))

Next look at the map function:

It's clear!? That is, each element in data is an X, passed it into the following function, and then returns a computed new array, that is, the value of each element in the new array is the average of each element in data plus the minus of data, and then calculates the result of 2 of times.


1 Map (Data,math.pow (sum ( -1*mean + x), 2))

And then look at the reduce function outside of the map:


1 2 Adds each element value of the previous new array. Reduce (Map (...), sum)

Then take a look at the reciprocal function:


1 2 Reciprocal reciprocal equivalent to (data.length-1) (SUM (data.length,-1))

And look at the outer product function:


1 2 Equal to and divided by (data.length-1) product (reduce (...), reciprocal (...)) for the new array element.

The outermost sqrt represents the square root of the result of the above division; You can compare the code of the previous non function programming, the same drop:) Seemingly afraid of a large piece of code, the analysis after the difficulty immediately to 0. If you reader the final expression or did not understand, it is completely the cat's language ability problem, welcome to ask questions.

Explain finished, finish the work, finished.

I hope this article will help you with your JavaScript programming.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

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.