Typical high-order functions of javascript

Source: Internet
Author: User
Reason

Although javascript has been used before, it is designed to work with some scattered "code segments" written on the backend, not to mention javascript projects. It is a great honor that the company just arrived at the company last month and started to push down and rewrite the project. Our team made the architecture and implementation from the beginning, with a clear purpose, in order to improve and surpass the previous version. This is a real javascript "project". Of course, the server is not the responsibility of our team. This is why I really started programming with javascript full time. Since I was interested in the course of checking the formal method, and javascript is a functional language, I want to show more functional stuff with javascript.

Several Functions

These methods are newly added to the javascript 1.6 array. It is a typical functional function, and of course it is also very practical. The definition of functional is not from javascript.
Filter: returns a set of Xs (X indicates the type, and s indicates the set). A predicate is a ing (function) from X to bool ). Then, this set is taken into consideration and a set composed of elements with the predicate true is returned. The following is a simple implementation:

1 function filter(arr,callback){2     var i,out=[];3     for(i=0;i<arr.length;i++){4         if(callback(arr[i]))5             out.push(arr[i]);6     }7     return out;8 }

Add a simple test:

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

Result:

2,4,6,8,10

Map: accepts a set Xs, a function f, and uses f ing for each element in the Xs set in order, and returns the set f x1, f x2, f x3... f xn. The implementation is as follows:

1 function map(arr,callback){2     var i,l= arr && arr.length || 0,out = new Array(l);3     for(i=0;i<l;i++)4         out[i]=callback(arr[i]);5     return out;6 }

Test:

1 var arr = [1,2,3,4,5,6,7,8,9,10];2 var addTen = function(item){3     return item + 10;4 };5 var mapAdded = map(arr,addTen);6 console.log(mapAdded);

Result:

11,12,13,14,15,16,17,18,19,20

In addition, the forEach, every, and some functions appear in javascript 1.6. However, there is still a lack of a powerful function, which is the fold function ). The so-called map-reduce, with map without "reduce" is not very disappointing? Next let's take a look at this "reduce".

Reduce implementation

The reduce mentioned above is actually a fold function ). It accepts an Xs set and a binary operator f. Then insert f into the set between every two adjacent elements. For example, fold plus [1, 2, 4] means 1 + 2 + 3 + 4. To be more precise, a "Starting element" is usually required as the second parameter at the beginning of f. For example, fold plus [1, 2, 4] means (1 + (2 + (3 + (4 + 0 ))). The following is an implementation:

1 function fold(arr,callback,b){2     var i,x;3     if(b) x=b,i=0;4     else x=arr[0],i=1;5     for(;i<arr.length;i++)6         x=callback(arr[i],x);7     return x;8 }

Test:

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

Result:

55

In ECMAScript 5, this function is called reduce, and in function formula it is usually called fold, which is a very vivid name.

Summary

In fact, when implementing these functional functions, the writing style is not functional, because javascript has a circular statement. What if there are no loop 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.