DenkzeitWiki

Suchen:

Aktuelle Änderungen Printable View Änderungen Bearbeiten

CharlesFGoldfarb > CharlesPetzold > CheckedExceptions > Exceptions > ClearType > ClojureBatteries > ClojureClr > ClojureGuerilla > ClojureIdomaticSnippets > ClojureInDeutschland > ClojureRepls > ClojureScript > ClojureStreams > ClojureSequences > ClojureUpdateToVersion13 > Closure > ClosuresClear Trail
Main /

Closures

(weitergeleitet von Main.Closure)

VariableBinding
ObjectOrientedProgramming

Functor
Scope

LanguageDesign

Lisp

Continuations
CSharp#delegates


The key thing to understand about closures is that it's the binding, not the value of the variable, that's captured. Thus, a closure can not only access the value of the variables it closes over but can also assign new values that will persist between calls to the closure.[1]




When you create a closure, you're creating a function that 'closes over' its lexical environment as it exists when the closure is created. This means that the name/variable bindings that are active _at the time the closure is created_ continue to exist for that closure.[2]


a closure, continuation’s more general cousin

Continuations are bookmarks — they tell you where to pick up after you last left off. Closures are simply the current lexical stack — they represent the current local context and its immediate parent, recursively. [3]


Closures are typically implemented with a special data structure that contains a pointer to the function code, plus a representation of the function's lexical environment (i.e., the set of available variables and their values) at the time when the function was created. [4]


Essentially a closure is a block of code that can be passed as an argument to a function call.


Thus closing a value is a mean to extend the lifetime of a variable and allow other code to change variables closed in functions.[5]

In a OO language like C# are useful when a simple state should be closed into a function without bothering introducing a new class (as a matter of fact this is done by the compiler for you).




The venerable master Qc Na was walking with his student, Anton. Hoping to prompt the master into a discussion, Anton said "Master, I have heard that objects are a very good thing – is this true?" Qc Na looked pityingly at his student and replied, "Foolish pupil – objects are merely a poor man’s closures."
Chastised, Anton took his leave from his master and returned to his cell, intent on studying closures. He carefully read the entire "Lambda: The Ultimate…" series of papers and its cousins, and implemented a small Scheme interpreter with a closure-based object system. He learned much, and looked forward to informing his master of his progress.
On his next walk with Qc Na, Anton attempted to impress his master by saying "Master, I have diligently studied the matter, and now understand that objects are truly a poor man’s closures." Qc Na responded by hitting Anton with his stick, saying "When will you learn? Closures are a poor man’s object." At that moment, Anton became enlightened.[8]




A "closure" is an expression (typically a function) that can have free variables together with an environment that binds those variables (that "closes" the expression).[9]


Closures in C#







Implementation

'closure' == 'function pointer + stack pointer', essentially.[10]


Currying

Closures and FirstClassFunctions make Currying easy.

In functional programming, currying is a way to bind arguments with a function and wait for the rest of the arguments to show up later.[11]



Edit - BackLinks - Tags - Page Hist - Print - Changes - Home - Orphans - Help

Zuletzt geändert am 14.04.2010 17:40 Uhr und seit 7. April 2005 3044 aufgerufen.