Can anyone determine what closure is in the language of the real world?

Possible duplicate:
What is & Closing & rsquo ;?

I read more and more about closing, but in fact I do not understand what it is from a practical point of view. I read Wikipedia on this page, but this is not entirely clear to me, because I have more practical programming experience (self-training), and not in the field of computer science. If this is a asked question, I apologize since my initial search did not return anything that really answered me.

edit: Thanks for pointing me in the right direction! I see that this has already been clearly answered, so I will close the question.

+4
source share
5 answers

Eric Lippert's blog pretty well explains this in a practical sense.

So is Kyle in SO

+5
source

An operation is called closed over a set when the result of this operation also belongs to this set.

For example, consider a set of positive integers. Addition is a closed operation on this set, because adding two positive integers will always give a positive integer.

However, the subtraction does not overlap with this set, because 2 - 3 will give -1, which does not belong to the set of positive integers.

amuses

+1
source

Two sentences of one sentence:

• closure are local variables for a function that are kept alive after the function returns, or

• closing is a stack frame that is not freed when the function returns. (as if the "stack-frame" was malloc'ed instead of being on the stack!)

http://blog.morrisjohns.com/javascript_closures_for_dummies

+1
source

probably best demonstrated by example

program exit (artificially) with a prefix *

JavaScript:

 js> function newCounter() { var i = 0; var counterFunction = function () { i += 1; return i; } return counterFunction; } js> aCounter = newCounter() * function () { * i += 1; * return i; * } js> aCounter() * 1 js> aCounter() * 2 js> aCounter() * 3 js> bCounter = newCounter() * function () { * i += 1; * return i; * } js> bCounter() * 1 js> aCounter() * 4 
+1
source

“Real world language” is a difficult measure for an objective measurement, but I will try, because in the end I also lack the CS-background (EE major here) and a little self-taught; -)

In many languages, a function “sees” more than one “region” (group) of variables - not only its local variables, but also its module or namespace, but also (if it is included in another function) the local variables of the function that contains it.

So for example (in Python, but many other languages ​​work the same!):

 def outer(haystack): def inner(needle): eldeen = needle[::-1] return (needle in haystack) or (eldeen in haystack) return [x for x in ['gold','silver','diamond','straw'] if inner(x)] 

inner can “see” the haystack without having to consider it as an argument, simply because its containing outer function has the haystack “in scope” (ie, “visible”). So far, I have been so clear, and I hope that this is not the closure, it concerns the lexical scope.

Now suppose that an external function (when processing the language as first-class objects and, in particular, allows them to return as the results of other functions), the external function returns the internal, and not just calling it internally (in Python, this, for example, is what usually happens when you use @something decorator @something ).

How does the inner function (returned as a result) still relate to the outer function variable since the outer function is complete?

The answer is precisely this “closing” business — those variables from an external function that may still need an internal (returned) function are stored and attached as attributes of the returned object of the internal function.

+1
source

All Articles