Introduction to typical javascript high-order function applications _ javascript tips-js tutorial

Source: Internet
Author: User
In the previous article, typical high-order functions of javascript mainly implement several typical functional functions. At last, the article also raised questions, why is that implementation different from functional languages such as F? Let's try the implementation of a more "function" today. Preface
In the previous article, typical high-order functions of javascript mainly implement several typical functional functions. At the end of the article, I also raised some questions. Why is that implementation different from functional languages such as F? Let's try a more "function" implementation today.

Another implementation
Similarly, try to make some changes to the previously implemented functions and remove the for loop. How to remove it? Here we first introduce the inductive definition of a set:

A set is either an empty set or a number pair composed of a number and a set. You can see from the definition that each set can be regarded as a number and a pair of a set. For example, {1, 2, 4, 5} can be considered as a pair of number 1 and set {2, 4, 5}, which is written as (1, {2, 4, 5 }). Recursively, {2, 4, 5} can be regarded as (2, {4, 5 }). The last is (5, Ø ). According to this understanding, we can use recursive methods to eliminate loops, because we have accessed each data item during decomposition and the ending condition is an empty set. Next let's take a look at another Implementation of the filter function. The original function name is prefixed with f to distinguish the previous function:

The Code is as follows:


Function ffilter (arr, callback ){
Var I = arguments [2] | 0,
Out = arguments [3] | [];
If (! Arr [I]) return arguments [3];
If (callback (arr [I])
Out. push (arr [I]);
Return arguments. callee (arr, callback, ++ I, out );
}


Test:

The Code is 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 );
};
Console. log (ffilter (arr, even ));


Result:
[2, 4, 6, 8, 10] in this way, after the cycle is eliminated, it is more natural to be close to the inductive definition of mathematics. Similarly, let's look at the ffold function again:

The Code is as follows:


Var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
Var plus = function (a, B ){
Return a + B;
};
Console. log (ffold (arr, plus, 3 ));


Result:
58
You can use the same method for other functions. In this way, I feel more functional, but can I get closer to the mathematical definition? Try again next time.
=========== 2013.1.8 update ====================================
If the writing method is closer to the mathematical definition, let's try using the linked list. First, a definition is provided:

The Code is as follows:


Var node = function (){
This. data = 0;
This. tail = null;
};


Initialize a linked list:

The Code is as follows:


Var n1 = new node (), n2 = new node (), n3 = new node (), n4 = new node (), n5 = new node ();
N1.data = 1, n1.tail = n2;
N2.data = 2, n2.tail = n3;
N3.data = 3, n3.tail = n4;
N4.data = 4, n4.tail = n5;
N5.data = 5, n5.tail = null;


Fold Linked List version:

The Code is as follows:


Function lfold (head, callback, B ){
If (! Head) return B;
Else return callback (head. data, arguments. callee (head. tail, callback, B ));
}


Output result:
18
According to the previous definition, a set is either an empty set or a number pair composed of a "Header" and a "tail" (SET. Every time a function is called, it is divided into head and tail until the set is empty (the lfold function above is really perfect, it is simply defined, if the program is long like this, comments are not needed. It is a pleasure ). This is the closest expression to the mathematical definition. Because javascript does not support match in many functional languages, it cannot be "automatically" decomposed, so it cannot directly represent inductive definitions.

In addition to the above, javascript can also implement the partial in the functional formula. The hitch in the dojo framework implements this function, this is another obvious example of function closeness to mathematics. I will discuss it in the next blog.
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.