What is closure (computer science)?

Source: Internet
Author: User
In the Swift programming langauge see closures, but do not understand what is, what kind of features, can do what objective-c do not? Programming beginners, seeking advice

Reply content:

To say Closure, you have to say Closed lambda expression, a Closed lambda expression is a lambda expression that has no free variables, such as λx. x , while λx. y x It's not Closed. One of the best properties of Closed LAMBDA expression is that its type must be isomorphic to some logical tautology, such as λx. λy. x y The type of "positive front" (αbeta) → αbeta
So how do you give an Open Lambda Expression to enclose live? The answer is to save all the free variables it references to something, and this Lambda Expression, which holds the free variable, is Closure. On its isomorphic logical side, the precondition is added to the left side of the sequential.

I'm not going to say anything grammatical.


A computer program can be roughly divided into code + data. It's easy for beginners to put the two on the line, thinking that code is code, data is data, and the two are completely different. But in fact, the two can be unified together. Unifying the code with the data is a threshold for learning computer programming.


can refer to my previous answer. What is a callback function?


It would be natural to save the data and use it later. But keep the code up and use it later, many people will feel very awkward, difficult to understand. It's all because we haven't had the sill yet.


Code command execution time, will be in a certain environment, simply save the code, or not enough, you need to save the environment of the code. Closures are, in fact, the code and the environment in which the code is in a holistic view. The surrounding environment, expressed as the data used by the code. In some languages, this concept is called code block (block), anonymous function (lambda), and so on.


The data and the code are no longer artificially separated and viewed together. Closures can be a natural concept. Data can be passed, passed from one place to another, and reused later. Closures from a certain point of view, but also data, of course, can be passed, from one function to another function, can also be kept, and later called. Because the environment is also maintained, the subsequent call, the restoration of the situation at the time, deferred execution, it is easy, very natural to achieve. What is the effect of deferred execution? It's 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 a non-local variable that the function executes.
The popular point is that closures allow a function to access variables that are declared in the context of the function's run, and can even access different runs above the variables.
Let's take a look at the scripting language:
function   Funa   ( callback  ) {  Alert   ( callback   ());     }  function  funb   () { var  str  =   "Hello World"      //function FUNB local variable, function Funa non-local variable  funa              ( function   ()  { return  str          }   }   
JavaScript closures--you know, I understand.
This is easier to understand. At least javascript closures refer to functions that have access to variables in another function scope, and other languages do not know (escaping can be compared with the concept of objects to understand, simply say:
objects are data with methods, and closures are methods with data
The second half of the data refers to the external data a piece of memory area, storage executable code and some variables, pointer learning is discrete? The result of certain operations on some sets is always in this set, which is called closure properties .... Although there is no =_= with this closure.
Closures are a powerful tool in functional languages, but they are a bit of a imaginative achievement here feeling.
In a nutshell, you can think of closures as a function that binds the values of certain variables, because functions in a functional language are one-level objects, so this is useful, for example, to define some data structures by returning some closures to the function.
Give me a chestnut:
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))
The class of the poor
  • 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.