This article has been published exclusively by Backend Technology Picks.

Developers use Java8 to write complex set-processing algorithms that require simple code to run efficiently on heavy cpus, which is where Lambda expressions came from.

Tip: Functional programming is language agnostic, it’s an idea, and it can be done in any language, just how easy it is to do it.

Lambda itself is the use of functional programming in Java, so what is functional programming?

1. What is functional programming

It’s hard to understand in technical terms, so you can get a sense of what functional programming is by looking at some of its features and benefits. The emphasis is on marking.

Reference nguyen other article: www.ruanyifeng.com/blog/2012/0…

1.1 the characteristics of

Functions are “first class citizens”, which means that functions are equal to other data types and can be assigned to other variables, passed in as arguments to another function, or returned as a value from another function. For example, the print variable in the following code is a function that can be used as an argument to another function.

var print = function(i){ console.log(i); }; [1, 2, 3]. ForEach (print);
Copy the code

“Expression” is a pure operation that always returns a value; A statement is an operation that does not return a value. Functional programming requires expressions, not statements. In other words, each step is pure operation and has a return value.

And the reason is because functional programming was motivated to try to compute, not to think about I/O.” The statement “reads and writes to the system and is excluded.

Of course, in practice, it is impossible not to do I/O. Therefore, in the programming process, functional programming only requires to limit I/O to a minimum, do not have unnecessary read and write behavior, and maintain the simplicity of the calculation process.

3) No side effects

A “side effect” is an interaction between the inside and outside of a function (most typically, modifying the value of a global variable) that produces a result other than the operation.

Functional programming emphasizes the absence of “side effects,” meaning that functions remain independent, all they do is return a new value, and nothing else, especially not changing the value of an external variable.

4. Do not change the status

As mentioned above, functional programming simply returns new values and does not modify system variables. Therefore, it does not modify variables, is also an important feature of it.

In other types of languages, variables are often used to hold “states.” Leaving variables unchanged means that state cannot be stored in variables. The best example of functional programming that uses parameters to preserve state is recursion.

5. Transparent references

Referential transparency refers to the fact that functions operate not depending on external variables or “state”, but only on input parameters, and the return value of a reference function is always the same as long as the parameters are the same.

With the third and fourth points above, this is obvious. In other languages, the return value of a function is dependent on the system state, and the return value is different depending on the state. This is called “reference opacity” and makes it difficult to observe and understand the behavior of your program.

1.2 the advantages

1. Simple code, fast development 2. Close to natural language, easy to understand

1 plus 2 times 3 minus 4 in functional language

Add (1, 2). Multiply (3) subtract (4)Copy the code

3. Easier code management

Does not depend on or change the state of the outside world. Given an input parameter, the result returned must be the same. Therefore, each function can be treated as an independent unit, which is good for unit testing and debugging, as well as modular combinations.

4. Easy “concurrent programming”

Functional programming does not need to consider “deadlocks” because it does not modify variables, so there is no problem of “locking” threads. We don’t have to worry about one thread’s data being modified by another, so we can feel comfortable spreading the work across multiple threads.

2. Examples of functional programming

Functional programming is concerned with mapping data, imperative programming is concerned with problem-solving steps.

The functional style has no assignment and therefore no for loop, which can only be implemented by recursive calls.

public class First { public static void main(String[] args) { int a = 10,b = 20; int c = a+b; System.out.println(c); }}Copy the code

This code uses commands to represent programs, and the order in which commands are executed to represent combinations of programs, so it is not functional programming. It’s kind of process-oriented thinking.

public class First {
    public static void main(String[] args) {
        int a = 10,b = 20;
        add(a,b);
    }
    static int add(int a,int b){
        int c = a+b;
        returnc; }}Copy the code

This code uses functions to represent the program, but internally it uses a combination of commands, not really functional programming. Object-oriented thinking.

public class First {
    public static void main(String[] args) {
        int a = 10,b = 20;
        add(a,b);
    }
    static int add(int a,int b){
        returna+b; }}Copy the code

This code uses functions to express programs, and uses combinations of functions to express combinations of programs, which is completely functional programming.

The difference between object-oriented, procedural and functional programming

The difference between “procedural” and “object” is “encapsulation.” The difference between “functional” and “object-oriented” is that “no external state is used.” This is also shown in the three pieces of code above.

If you have any objections, please comment below. Thank you.