Home Web Front-end JS Tutorial Introduction to typical high-order function applications in javascript 2_javascript skills

Introduction to typical high-order function applications in javascript 2_javascript skills

May 16, 2016 pm 05:44 PM
higher order function

Foreword
In the previous article, Typical High-Order Functions in JavaScript, several typical functional functions were mainly implemented. The article also raised the question at the end, why is that implementation "different" from functional languages ​​such as F#? Let’s try a more “functional” implementation today.

Another implementation
Similarly, try to make some changes to the previously implemented function 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 consisting of a number and a set. From the definition, we can see that each set can be regarded as a number. and a set of pairs. For example: {1,2,4,5} can be considered as a pair consisting of the number 1 and the set {2,4,5}, written as (1, {2,4,5}). Recursively, {2,4,5} can be viewed as (2, {4,5}). Finally, it is (5, Ø). Based on this understanding, we can use recursive methods to eliminate loops, because we have visited every data item during decomposition, and the terminal condition is the empty set. Let’s take a look at another implementation of the filter function. The original function name is prefixed with f to distinguish it from the previous function:
Copy the code 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:
Copy code 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] After eliminating the loop, it is closer to the inductive definition of mathematics and appears more natural. Similarly, look at the ffold function again:
Copy the code 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
Use the same method for other functions. This feels more functional, but can it be closer to the mathematical definition? Try again next time.
==========2013.1.8 Update==================
As mentioned above, whether the writing methods can be closer to the mathematical definitions , let’s try using a linked list. First give a definition:
Copy code The code is as follows:

var node = function() {
this.data = 0;
this.tail = null;
};

Initialize another linked list :
Copy code 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:
Copy code 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 pair consisting of a "head" and a "tail" (set). Each time the function is called, it is decomposed into head and tail until the set is empty (after writing the above lfold function, I really feel that it is so perfect, it is simply a definition. If the program looks like this, there will be no need for comments. It is really a pleasure) . This is the expression closest to the mathematical definition. Because JavaScript does not support many functional language matches, it cannot be "automatically" decomposed, and it cannot directly express inductive definitions.

In addition to the above things, JavaScript can also implement partial in functional expressions. Hitch in the Dojo framework achieves this function. This is another obvious example of functional expressions being close to mathematics. I will discuss this in my next blog.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to do functional programming with PHP How to do functional programming with PHP Jun 06, 2023 am 08:21 AM

PHP is a widely used server-side language. One of the reasons why many web developers like to use PHP is its rich function library and simple and easy-to-use function syntax. Functional programming is a programming paradigm that well encapsulates data and behavior, making the code more modular and easy to maintain and test. In this article, we will introduce how to use PHP for functional programming. Functional Programming Basics The core idea of ​​functional programming is to treat functions as first-class citizens. Functions themselves can be passed, returned, and composed like variables.

How to create higher-order functions in Python? How to create higher-order functions in Python? Sep 05, 2023 pm 07:29 PM

In Python, a function that takes another function as an argument or returns a function as output is called a higher-order function. Let's see its features - the function can be stored in a variable. This function can be passed as a parameter to another function. Higher-order functions can be stored in the form of lists, hash tables, etc. Functions can be returned from functions. Let's look at some examples − Functions as objects The Chinese translation of Example is: Example In this example, these functions are treated as objects. Here, function demo() is assigned to a variable - #Creatingafunctiondefdemo(mystr):returnmystr.swapcase()#swappingthecase

What are the higher-order functions in Python? What are the higher-order functions in Python? Nov 10, 2023 pm 04:42 PM

High-order functions include map(), filter(), reduce(), lambda function, partial(), etc. Detailed introduction: 1. map(): This built-in function accepts a function and one or more iterable objects as input, and then returns an iterator that applies the input function to each element of the iterable object; 2. filter() : This built-in function takes a function and an iterable object as input, and returns an iterator that yields those elements that cause the input function to return True, etc.

PHP Arrow Functions: How to handle nested calls to higher-order functions PHP Arrow Functions: How to handle nested calls to higher-order functions Sep 13, 2023 am 08:27 AM

PHP arrow functions: How to handle nested calls of higher-order functions, specific code examples are needed Introduction: In PHP7.4 version, the concept of arrow functions (arrowfunctions) was introduced. Arrow functions are a concise way of writing and can be processed elegantly. Nested calls to higher-order functions. This article will introduce the basic use of arrow functions and demonstrate how to handle nested calls of higher-order functions through specific code examples. 1. What is an arrow function? Arrow function is a new feature introduced in PHP7.4 version. It is a

Higher-order functions in Python Higher-order functions in Python Sep 13, 2023 pm 06:53 PM

Introduction to Python's World of Higher-Order Functions If you want to improve your Python programming skills and generate more expressive and efficient code, you've come to the right place. Functions in Python are more than just specialized blocks of code. They are also powerful things that can be moved, transferred, and even dynamically generated. Higher-order functions enhance this versatility by processing other functions. This article will extensively discuss the principles of higher-order functions. We'll explore the basics of processes as first-class objects, dive into real-world examples of higher-order functions, and encourage the power of lambda functions for clear and beautiful code. The functional programming model and its advantages when used in Python will also be discussed. After reading this article, you will have a firm grasp of higher-order functions and know

Analysis of application scenarios of high-order functions in Golang Analysis of application scenarios of high-order functions in Golang May 17, 2023 pm 05:40 PM

With the popularity and development of the Golang language, more and more developers are beginning to try to use functional programming ideas. Higher-order functions in Golang bring great convenience to functional programming and are widely used in actual development. So, what are the application scenarios of high-order functions in Golang? Next, we will analyze this. Processing of function parameters and return values ​​In Golang, functions can be used as parameters of other functions or return functions. This means that we can pass a function as a parameter to another

How to do functional programming with PHP How to do functional programming with PHP Jun 06, 2023 am 08:21 AM

PHP is a widely used server-side language. One of the reasons why many web developers like to use PHP is its rich function library and simple and easy-to-use function syntax. Functional programming is a programming paradigm that well encapsulates data and behavior, making the code more modular and easy to maintain and test. In this article, we will introduce how to use PHP for functional programming. Functional Programming Basics The core idea of ​​functional programming is to treat functions as first-class citizens. Functions themselves can be passed, returned, and composed like variables.

How to understand higher-order functions of function types in Golang? How to understand higher-order functions of function types in Golang? Apr 20, 2024 am 11:54 AM

Golang higher-order functions accept and return functions. They fall into two categories: receiving functions as parameters: processing other functions or executing dynamic programs. Return functions as return values: Create and return functions that can be stored and later executed.

See all articles