Java 8 introduces lambda expressions, as well as functional programming styles. In understanding the functional programming process, made some notes, excerpts from this article.
Nested functions (Nested function)
1. A nested function refers to a function defined within another function. The outer function, which is referred to as the outer function.
2. The nesting of functions can be multiple layers of nesting. A nested function can see the non-local variables of all its outer functions. In the actual program, the number of nested layers is generally very small. Below is an example of a three-layer nesting, innerofinner can also access the variable x defined in the outer function weight.
function outer () { var x = 1; function inner () { + = 1; Console.log (x); function Innerofinner () { + = ten; Console.log (x); } return innerofinner; } return Inner;}
3. Nested functions have read and write permissions on non-local variables.
4. If a nested function can escape the outer function, for example, if the function is a first citizen, and the embedded function is passed to another function as a parameter or as a return value, then a closure (closure) is generated, and the function's original environment variable can be accessed by invoking the escaped function. At this point, the frame of the outer function will continue to exist until all closures pointing to the frame disappear. The non-local variables that the closure points to are moved from the stack memory to the heap memory.
5. Javascript supports nested functions. Java indirectly supports built-in functions through LAMBDA expressions, inner classes, and anonymous classes.
example, add is an outer function, plus is a nested function. Counter is a local variable for add and a non-local variable (non-local variable) for Plus. Inside the function body of an outer function add, a nested function plus is defined, then the nested function is called and the result is returned.
function Add () { var counter = 0; function Plus () {counter + = 1;} Plus (); return counter;}
Non-local variables (non-local variable)
1. A non-local variable is actually a relative term, which is a variable that is neither local nor global. This term is commonly used in inline functions, or in the context of anonymous functions.
2. In programming languages, it is difficult to implement non-local variables, which also makes it difficult to implement functions such as nesting functions, anonymous functions, higher order functions, and functions as first citizens.
3. As mentioned in the 4th above, when the nested function is escaped from the outer function, a closure is generated and used to locate the nonlocal variable. When the outer function returns an inline function, the outer function is executed, ejected from the stack, and the non-local variable disappears from the stack. But non-local variables are still referenced by memory functions, so they need to be reassigned to the heap. Thus, the life cycle of a non-local variable is longer than the outer function that defines it.
A simple example of a non-local variable, referring to the code of Add/plus in a nested function and an explanation.
Closures (Closure)
1. In operation, a closure refers to a record that stores a function and its environment variables. This function can be either a known function or an anonymous function.
In the following example, the first closure function is originally a known function, and the second closure function is an anonymous function.
functionf (x) {functiong (Y) {returnX +y; } returnG;}functionh (x) {return function(y) {returnX +y}}varFF = f (3); FF (5);//return 8;varHH = h (3); HH (5);//return 8;
Closures can be used directly without assigning to other variables, which are called anonymous closures, as follows.
F (3) (5); // return 8
2. In the original nested function example, the embedded function is executed in the outer layer function, and if the outer function returns a nested function, a closure is generated. See the example below.
function Add () { var counter = 0; function Plus () { + =1 ; return counter; } return Plus;}
Resources
Nested function, Wikipedia
Non-local variable, Wikipedia
Closure (computer programming), Wikipedia
[Java] Functional programming related concepts-NOTE 1