Functional programming practices in JavaScript (1)

Source: Internet
Author: User

Functional programming languages have been in the academic field for a long time, but historically they have no rich tools and libraries available for use. With the emergence of Haskell and F # functional programming languages on the. NET platform, Haskell has become more popular. Some traditional programming languages, such as C ++ and JavaScript, also introduce some structures and features provided by functional programming.

BKJIA recommended topics: JavaScript functional programming

In many cases, repeated JavaScript code leads to poor coding. If you use functional programming, you can avoid these problems. In addition, you can use the functional programming style to write more elegant callbacks. Because functional programming uses a completely different way of organizing programs, programmers who are used to using imperative examples may find functional programming a little hard to learn.

Functional Programming Concepts

Many developers know how to encode the language that specifies a solution by describing "how to do. For example, to compile a function to calculate factorial, I can write a loop to describe the program, or use recursion to find the product of all numbers. In both cases, the computing process is described in detail in the program. Listing 1 shows a possible C code used to calculate a factorial.

 
 
  1. List 1. Process-style factorial
  2. Int factorial (int n)
  3. {
  4. If (n <= 0)
  5. Return 1;
  6. Else
  7. Return n * factorial (n-1 );
  8. }

These languages are also called procedural programming languages because they define the process of solving the problem. Functional programming is significantly different from this principle. In functional programming, You need to describe the question "What is it ". Functional programming languages are also called declarative languages. The same factorial program can be written as the product of all numbers to n. The typical function program for calculating factorial looks like the example in Listing 2.

 
 
  1. List 2. factorial in a functional Style
  2. Factorial n, where n <= 0: = 1
  3. Factorial n: = foldr * 1 take n [1 ..]

The second statement indicates that you want to obtain the list of the first n numbers starting from 1 take n [1 ..]), and then find out their product, 1 is the base element. This definition is different from the previous example. There is no loop or recursion. It is like the arithmetic definition of a factorial function. Once you understand the meanings of the database functions take and foldr) and the mark list notation [], it is easy to write the code and the readability is also good. You can write routines with only three lines of Miranda code. Based on the parameters, You can traverse each node of the n-tree with breadth-first or depth-first, and the elements can be of any common type.
 
Historically, functional programming languages are not popular for many reasons. But recently, some functional programming languages are entering the computer industry. One example is Haskell on the. NET platform. In other cases, some existing languages borrow some concepts from functional programming languages.

Some C ++ implementation iterators and continuation, and some functional methods provided in JavaScript to construct functional construct) are examples of such borrow. However, the general language programming examples have not changed by using function-based construction. JavaScript has not become a functional programming language because of the addition of functional structures.

Here we mainly discuss the various advantages of functional construction in JavaScript, as well as the way they are used in daily coding and work. We will start with some basic functions and use them to view more interesting applications.


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.