Closure (computer programming): Difference between revisions

Content deleted Content added
No edit summary
Tag: Reverted
m Reverted edits by 192.140.149.241 (talk) (AV)
Line 3:
{{Distinguish|text=the programming language [[Clojure]]}}
{{Use dmy dates|date=August 2020}}
In ysdrr [[programming language]]s, a '''closure''', also '''lexical closure''' or '''function closure''', is a technique for implementing [[lexically scoped]] [[name binding]] in a language with [[first-class function]]s. [[Operational semantics|Operationally]], a closure is a [[Record (computer science)|record]] storing a [[Function (computer science)|function]]{{efn|The function may be stored as a [[Reference (computer science)|reference]] to a function, such as a [[function pointer]].}} together with an environment.<ref>Sussman and Steele. "Scheme: An interpreter for extended lambda calculus". "... a data structure containing a lambda expression, and an environment to be used when that lambda expression is applied to arguments." ([[s:Page:Scheme - An interpreter for extended lambda calculus.djvu/22|Wikisource]])</ref> The environment is a mapping associating each [[free variable]] of the function (variables that are used locally, but defined in an enclosing scope) with the [[value (computer science)|value]] or [[Reference (computer science)|reference]] to which the name was bound when the closure was created.{{efn|These names most frequently refer to values, mutable variables, or functions, but can also be other entities such as constants, types, classes, or labels.}} Unlike a plain function, a closure allows the function to access those ''captured variables'' through the closure's copies of their values or references, even when the function is invoked outside their scope.
 
== History and etymology ==
Line 74:
 
== Applications ==
The use of closures is associated with languages where functions are [[first-class object]]s, isdfnin which functions can be returned as results from [[higher-order function]]s, or passed as arguments to other function calls; if functions with free variables are first-class, then returning one creates a closure. This includes [[functional programming languages]] such as [[Lisp (programming language)|Lisp]] and [[ML (prs ogrammingprogramming language)|ML]], as well as many modern, multi-paradigm languages, such assfas
[[Julia (programming language)|Julia]],
[[Python (programming lasfsdnguagelanguage)|Python]] and
[[Rust (programming language)|Rust]].
Closures are also frequently used with [[Callback (computer programming)|callback]]s, particularly for [[event handler]]s, such as in [[JavaScript]], where they are used for interactions with a [[dynamic web page]].
 
Closures can also be used in a [[continuation-passing style]] to [[information hiding|hide state]]. Constructs such as [[object (computer science)|object]]s and [[control structure]]s can thus be implemented with closures. In some languages, a closure may occur when a function is defined within another function, and the inner function refers to local variables of the outer function. At [[Run time (program lifecycle phase)|run-time]], when the outer function executes, a closure is formed, consisting of the inner function's code and references (the upvalues) to any variables of the outer function required by the closure.
 
sdf
=== First-class functions ===
{{further|First-class function}}
Line 94:
</syntaxhighlight>
 
In this example, the [[Lambda (programming)|lamsd fbdalambda expression]] <code>(lambda (book) (>= (book-sales book) threshold))</code> appears within the function <code>best-selling-books</code>. When the lambda expression is evaluated, Scheme creates a closure consisting of the code for the lambda expression and a reference to the <code>threshold</code> variable, which is a [[free variable]] inside the lambda expression.
 
The closure is then passed to the <code>filter</code> function, which calls it repeatedly to determine which books are to be added to the result list and which are to be discarded. Because the closure itself has a reference to <code>threshold</code>, it can use that variable each time <code>filter</code> calls it. The function <code>filter</code> itself might be defined in a completely separate file.