What is closure (computer science?

Source: Internet
Author: User
TheSwiftprogramminglangauge can see closures, but does not quite understand what it is, what features it has, and what Objective-C can do? For beginners of programming, please advise me to see closures in The Swift programming langauge, but I don't quite understand what it is, what features it has, and what Objective-C cannot do? For beginners of programming, please advise: to say Closure, You Have To Say Closed Lambda Expression. A Closed Lambda Expression is a Lambda Expression without free variables, as shown in λX.X, And λX.YXIt is not Closed. One of the best properties of Closed Lambda expressions is that their types must be the same as a logical statement. For example λX. λY.XYThe type is "yes 」 (αβ) →αβ.
So how do I assign an Open Lambda Expression to Enclose? The answer is to save all the free variables it references to. The Lambda Expression that saves the free variables is Closure. On its homogeneous logic side, the premise is added to the left of the successive formula.

I won't talk about the syntax.


Computer programs can be roughly divided into code + data. It is easy for beginners to oppose the two and think that code is code, data is data, and the two are completely different. But in fact, the two can be unified. Unifying code and data is a threshold for learning computer programming.


For more information, see my previous answers. What is a callback function?


It is natural to save the data and use it later. But keep the code up and use it later. Many may find it awkward and hard to understand. Because there is no such thing.


When executing code commands, the code is in a certain environment. It is not enough to simply save the code. You need to save the environment where the code is located. Closure is actually a whole view of the environment in which code and code are stored. The surrounding environment shows the data used by the Code. In some languages, this concept is called block, anonymous function (lambda), and so on.


Data and code are no longer separated by humans and can be viewed in a unified manner. Closure is a natural concept. Data can be transmitted from one place to another and used later. A closure is also data from a certain point of view. It can also be passed from one function to another. It can also be maintained and called later. Because the environment is also maintained, it will be easy to implement in the future to restore the current situation and delay the execution. What is the role of delayed execution? This is another topic.

function makeCounter()      local count = 0      return function()           count = count + 1           return countend
A closure is a function, or a pointer to a function, plus non-local variables executed by the function.
To put it bluntly, the closure allows a function to access and declare the variables in the context of the function running, or even access the variables in different running procedures.
Let's look at the script language:

Function funA (callback) {alert (callback ();} function funB () {var str = "Hello World"; // local variable of function funB, function funA non-local variable funA (function () {return str ;});}
Javascript closure-you do not understand it. I understand it anyway.
This is easier to understand. At least JavaScript closures refer to the functions that have the right to access the variables in another function scope. Other languages do not know (escape can be compared with the concept of objects, Simply put:
Objects are data with methods, while closures are data-carrying methods.
The data in the last half refers to a memory area of external data, which stores executable code and some variables. Has the pointer learned to be discrete? In some sets, the results of some operations are always in this set. This is called the closure nature ............ Although it has nothing to do with this closure = _ =.
Closure is a very powerful tool in functional languages, but it is a bit of a sense.
Simply put, you can regard the closure as a function bound to the values of some variables. Because the function in functional languages is a level-1 object, such a property is very useful, for example, you can define some data structures by returning some closures in the function.
Example:
This is a stack I wrote when I learned common lisp.

(defun stackpush (stack x)        (cons #'(lambda () x)              #'(lambda () stack)))(defun stackpop (stack)        (apply (cdr stack)nil))(defun stacktop (stack)        (apply (car stack) nil))
Poor class

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.