Current location - Training Enrollment Network - Mathematics courses - What is the meaning of defining closure in mathematics and what are its applications?
What is the meaning of defining closure in mathematics and what are its applications?
When an internal function is called, a closure is formed. Closure is a function, which can read the internal variables of other functions, define a function inside the function, and create a closure environment, so that the returned subroutine can capture I, so that a reference to this I can be retained in the subsequent execution process.

Application: In PHP, Scala, Scheme, Common Lisp, Smalltalk, Groovy, JavaScript, Ruby, Python, Go, Lua, objective c, swift and Java(Java8 and above), we can all find different levels of support for closures.

Closures contain free (not bound to a specific object) variables, which are not defined in this code block or any global context, but are defined in the environment where the code block is defined (local variables).

The word "closure" comes from the combination of the code block to be executed (because the code block contains free variables, these free variables and its referenced objects are not released) and the computing environment (scope) that provides binding for free variables.

Extended data

Closure makes the garbage collection mechanism of Javascript unable to recover the resources occupied by A, because the execution of the internal function B of A depends on the variables in A. ..

Because of its closeness, I in function A always exists after it returns, so that every time C () is executed, I is the value of I plus 1.

If A returns a function other than B, the situation is completely different. Because after the execution of A, B did not return to the outside world of A, but was only referenced by A, and at this time A will only be referenced by B. Therefore, functions A and B refer to each other without external interference (being referenced by the outside world), and functions A and B will be recycled.

The closure in target c is implemented by blocks. Apple has extended the syntax of Block in C, Objective-C and C++, which is supported in GCC4.2 and can be understood as function pointer, anonymous function, closure and lambda expression. Let's use a block object for the time being, because there are still some differences between the two.

If you use block objects inline, you don't need to declare them. The syntax for declaring a block object is similar to that for declaring a function pointer, except that the caret (*) should be used instead of the asterisk pointer (*) for the block object. The code declares a aBlock variable, which identifies a block that needs to pass in three parameters, and the return value is float.