Application of typical high-order function in JavaScript _javascript skills

Source: Internet
Author: User

reason
Although the JavaScript language has been used before, but in order to tie in with the back-end of some of the fragmented "code snippets", not to mention the JavaScript project. It is a great honor to have just arrived at the company last month to start to rewrite the project, our team from scratch to do the architecture and implementation, the purpose is clear, in order to improve and beyond the previous version. This is really a JavaScript "project", of course, the service side is not our team to take responsibility. This is where I really started to use JavaScript to program full-time. Since I was interested in formal methods at school and JavaScript was a functional language, I wanted to show more functional things in JavaScript.

several functions
These methods are all new to the JavaScript 1.6 array. is a very typical functional function, and of course very practical. The following is the definition of functional that does not come from JavaScript.

Filter: Accepts a set XS (x representation type, s representation set), a predicate, which is a mapping (function) from X to bool. Then you worry about the collection and return the set of elements that predicate is true. Here's a simple implementation:

Copy Code code as follows:

function Filter (arr,callback) {
var i,out=[];
for (i=0;i<arr.length;i++) {
if (callback (Arr[i]))
Out.push (Arr[i]);
}
return out;
}

Let's add a simple test:
Copy Code code as follows:

var arr = [1,2,3,4,5,6,7,8,9,10];
var even = function (item) {
if (typeof item!== "number") return false;
Return! (Item & 1);
};
var filtered = filter (Arr,even);
Console.log (filtered);

Results:
2,4,6,8,10
Map: Accepts a set XS, a function f, then uses the F mapping for each element in the XS set, and returns the set F x1, F x2, F x3 ... f xn. Implemented as follows:
Copy Code code as follows:

function Map (arr,callback) {
var i,l= arr && Arr.length | | 0,out = new Array (l);
for (i=0;i<l;i++)
Out[i]=callback (Arr[i]);
return out;
}

Test:
Copy Code code as follows:

var arr = [1,2,3,4,5,6,7,8,9,10];
var Addten = function (item) {
return item + 10;
};
var mapadded = map (Arr,addten);
Console.log (mapadded);

Results:
11,12,13,14,15,16,17,18,19,20
There are also foreach,every and some three functions that appear in JavaScript 1.6. But in the process of using the feeling is still missing a powerful function, it is the folding function (fold). Is the so-called map-reduce, with a map and no "reduce" is not a disappointment? Here's a look at this "reduce".

the implementation of reduce
The "reduce" mentioned above is actually a folding function (fold). It accepts an XS set, a two-dollar operator F. The f is then inserted between each of the two adjacent elements in the collection. For instance, fold plus [1,2,3,4] means 1+2+3+4. To be more precise, it is usually necessary to have a "start element" as the second parameter at the beginning of F. For example fold plus [1,2,3,4] means (1+ (3+ (4+0)). Here is the implementation:
Copy Code code as follows:

function fold (arr,callback,b) {
var i,x;
if (b) x=b,i=0;
else X=arr[0],i=1;
for (; i<arr.length;i++)
X=callback (ARR[I],X);
return x;
}

Test:
Copy Code code as follows:

var arr = [1,2,3,4,5,6,7,8,9,10];
var plus = function (a,b) {
return a+b;
};
var foldplus = fold (arr,plus,0);
Console.log (Foldplus);

Results:
55

This function is called reduce in ECMAScript 5, and the function is usually called fold, which is a very vivid name.
Summary
In fact, the above implementation of these functional functions when the writing style is not functional, because the JavaScript language has a circular statement. What if there are no looping statements? Leave it to the next exploration.

Related Article

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.

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.