All Topics  
Closure (computer science)

 

   Email Print
   Bookmark   Link






 

Closure (computer science)



 
 
In computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables. The explicit use of closures is associated with functional programming
Functional programming

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of function s and avoids program state and immutable object data....
 and with languages such as ML
ML programming language

ML is a general-purpose functional programming language developed by Robin Milner and others in the late 1970s at the University of Edinburgh, whose syntax is inspired by ISWIM....
, Lisp and Perl. Constructs such as objects in other languages can also be modeled 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.






Discussion
Ask a question about 'Closure (computer science)'
Start a new discussion about 'Closure (computer science)'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, a closure is a function that is evaluated in an environment containing one or more bound variables. When called, the function can access these variables. The explicit use of closures is associated with functional programming
Functional programming

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of function s and avoids program state and immutable object data....
 and with languages such as ML
ML programming language

ML is a general-purpose functional programming language developed by Robin Milner and others in the late 1970s at the University of Edinburgh, whose syntax is inspired by ISWIM....
, Lisp and Perl. Constructs such as objects in other languages can also be modeled 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 runtime
Runtime

In computer science, runtime or run time describes the operation of a computer program, the duration of its execution, from beginning to termination ....
, when the outer function executes, a closure is formed, consisting of the inner function’s code and references to any variables of the outer function required by the closure.

A closure can be used to associate a function with a set of "private" variables, which persist over several invocations of the function. The scope
Scope (programming)

In computer programming, scope is an enclosing context where values and expressions are associated. Various programming languages have various types of scopes....
 of the variable encompasses only the closed-over function, so it cannot be accessed from other program code. However, the variable is of indefinite extent
Variable

A variable is a symbol that stands for a value that may vary; the term usually occurs in opposition to constant, which is a symbol for a non-varying value, i.e....
, so a value established in one invocation remains available in the next. As a consequence, closures can be used to hide state
Information hiding

Information hiding in computer science is the principle of hiding of design decisions in a computer program that are most likely to change, thus protecting other parts of the program from change if the design decision is changed....
, and thus to implement object-oriented programming
Object-oriented programming

Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs....
.

The term closure is often mistakenly used to mean anonymous function
Anonymous function

In computing, an anonymous function is a function defined, and possibly called, without being bound to a name. In lambda calculus, all functions are anonymous....
. This is probably because most languages implementing anonymous functions allow them to form closures and programmers are usually introduced to both concepts at the same time. These are, however, distinct concepts.

The concept of closures was developed in the 1960s and was first fully implemented as a language feature in the programming language Scheme. Since then, many languages have been designed to support closures.

Function object
Function object

A function object, also called a functor, functional or functionoid, is a computer programming construct allowing an object to be invoked or called as if it were an ordinary function , usually with the same syntax....
s are sometimes also called closures.

Closures and first-class functions

Closures typically appear in languages in which functions are first-class values
First-class function

In computer science, a programming language is said to support first-class functions if it treats function s as first-class objects. Specifically, this means that the language supports constructing new functions during the execution of a program, storing them in data structures, passing them as arguments to other functions, and returning the...
—in other words, such languages allow functions to be passed as arguments, returned from function calls, bound to variable names, etc., just like simpler types such as strings and integers. For example, consider the following Scheme function: Return a list of all books with at least THRESHOLD copies sold. (define (best-selling-books threshold) (filter (lambda (book) (>= (book-sales book) threshold)) book-list))

In this example, the lambda expression (lambda (book) (>= (book-sales book) threshold)) appears within the function best-selling-books. When the lambda expression is evaluated, Scheme creates a closure consisting of the code for the lambda and a reference to the threshold variable, which is a free variable inside the lambda.

The closure is then passed to the filter 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 threshold, it can use that variable each time filter calls it. The function filter itself might be defined in a completely separate file.

Here is the same example rewritten in ECMAScript
ECMAScript

ECMAScript is a scripting language, standardized by Ecma International in the ECMA-262 Specification . The language is widely used on the World Wide Web, and is often confused with JavaScript or JScript, the two major Programming language dialect from which ECMAScript was standardized....
 (JavaScript), another popular language with support for closures: // Return a list of all books with at least 'threshold' copies sold. function bestSellingBooks(threshold)

The function keyword is used here instead of lambda, and an Array.filter method instead of a global filter function, but otherwise the structure and the effect of the code are the same.

A function may create a closure and return it. The following example is a function that returns a function.

In Scheme: Return a function that approximates the derivative of f using an interval of dx, which should be appropriately small. (define (derivative f dx) (lambda (x) (/ (- (f (+ x dx)) (f x)) dx)))

In ECMAScript: // Return a function that approximates the derivative of f // using an interval of dx, which should be appropriately small. function derivative(f, dx)

Because the closure in this case outlives the scope
Scope (programming)

In computer programming, scope is an enclosing context where values and expressions are associated. Various programming languages have various types of scopes....
 of the function that creates it, the variables f and dx live on after the function derivative returns. In languages without closures, the lifetime of a local variable coincides with the execution of the scope where that variable is declared. In languages with closures, variables must continue to exist as long as any existing closures have references to them. This is most commonly implemented using some form of garbage collection
Garbage collection (computer science)

In computer science, garbage collection is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage , or memory used by Object that will never be accessed or mutated again by the Application software....
.

While this is not always clarified, a closure need not be formed using an anonymous function
Anonymous function

In computing, an anonymous function is a function defined, and possibly called, without being bound to a name. In lambda calculus, all functions are anonymous....
. The Python
Python (programming language)

Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python's core syntax and semantics are Minimalism , while the standard library is large and comprehensive....
 programming language, for example, has very limited support for anonymous functions but fully supports closures. For example, one way the above ECMAScript example could be implemented in Python is:
  1. Return a function that approximates the derivative of f
  2. using an interval of dx, which should be appropriately small.
def derivative(f, dx): def gradient(x): return (f(x + dx) - f(x)) / dx return gradient In this example, the function named gradient forms a closure together with the variables f and dx. This closure is then returned by the outer function named derivative.

Although lambda expressions in Python are limited to being a single expression, they are still powerful enough to duplicate the above functionality: >>> from math import sin, cos >>> derivative = lambda f, dx: lambda x: (f(x + dx) - f(x)) / dx >>> dx = derivative(sin, 1e-8) >>> dx(.5) 0.8775825621754052 >>> cos(.5) 0.87758256189037276 >>>

Uses of closures

Closures have many uses:
  • Designers of software libraries
    Library (computer science)

    In computer science, a library is a collection of subroutines or Class used to develop software. Libraries contain code and data that provide services to independent programs....
     can allow users to customize behavior by passing closures as arguments to important functions. For example, a function that sort
    Sorting algorithm

    In computer science and mathematics, a sorting algorithm is an algorithm that puts elements of a List in a certain Total order. The most-used orders are numerical order and lexicographical order....
    s values can accept a closure argument that compares the values to be sorted according to a user-defined criterion.
  • Because closures delay evaluation—i.e., they do not "do" anything until they are called—they can be used to define control structures. For example, all Smalltalk
    Smalltalk

    Smalltalk is an Object-oriented programming, Type system, reflection computer programming programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human?computer symbiosis." It was designed and created in part for educational use, more so for constructionist learning, at PARC by Al...
    's standard control structures, including branches (if/then/else) and loops (while and for), are defined using objects whose methods accept closures. Users can easily define their own control structures as well.
  • Multiple functions can be produced which close over the same environment, enabling them to communicate privately by altering that environment (in languages that allow assignment). In Scheme:


(define foo #f) (define bar #f)

(let ((secret-message "none")) (set! foo (lambda (msg) (set! secret-message msg))) (set! bar (lambda secret-message)))

(display (bar)) ; prints "none" (newline) (foo "meet me by the docks at midnight") (display (bar)) ; prints "meet me by the docks at midnight"

  • Closures can be used to implement object
    Object-oriented programming

    Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs....
     systems.


Note: Some speakers call any data structure that binds a lexical
Scope (programming)

In computer programming, scope is an enclosing context where values and expressions are associated. Various programming languages have various types of scopes....
 environment a closure, but the term usually refers specifically to functions.

Differences in semantics


As different languages do not always have a common definition of the lexical environment, their definitions of closure may vary as well. The commonly held minimalist definition of the lexical environment defines it as a set of all bindings of variables
Name binding

In programming languages, name binding is the association of Value s with identifiers. An identifier bound to a value is said to Reference that value....
 in the scope, and that is also what closures in any language have to capture. It should be noted though that the meaning of a variable
Variable

A variable is a symbol that stands for a value that may vary; the term usually occurs in opposition to constant, which is a symbol for a non-varying value, i.e....
 binding also differs. In imperative languages, variables bind to relative locations in memory that can store values. Although the relative location of a binding does not change at runtime, the value in the bound location can. In such languages, since closure captures the binding, any operation on the variable, whether done from the closure or not, is performed on the same relative memory location. Here is an example illustrating the concept in ECMAScript
ECMAScript

ECMAScript is a scripting language, standardized by Ecma International in the ECMA-262 Specification . The language is widely used on the World Wide Web, and is often confused with JavaScript or JScript, the two major Programming language dialect from which ECMAScript was standardized....
, which is one such language:

var f, g; function foo

foo; print(g); // "1" print(f); // "2" Note how function foo and the closures referred to by variables f and g all use the same relative memory location signified by local variable x.

On the other hand, many functional languages, such as ML, bind variables directly to values. In this case, since there is no way to change the value of the variable once it is bound, there is no need to share the state between closures - they just use the same values.

Yet another subset, lazy functional languages such as Haskell
Haskell (programming language)

Haskell is a standardized, purely functional programming language with non-strict programming language, named after logician Haskell Curry. The goals of the language are described as:...
, bind variables to a result of a computation in the future. Consider this example in Haskell:

foo x y = let r = x / y in (\z -> z + r) f = foo 1 0 main = do putStr (show (f 123))

The binding of r captured by the closure defined within function foo is to the computation (x / y) - which in this case results in division by zero. However, since it is the computation that is captured, and not the value, the error only manifests itself when the closure is invoked, and actually attempts to use the captured binding.

Yet more differences manifest themselves in the behavior of other lexically scoped constructs, such as return, break and continue statements. Such constructs can in general be considered in terms of invoking an escape continuation established by an enclosing control statement (in case of break and continue, such interpretation requires looping constructs to be considered in terms of recursive function calls). In some languages, such as ECMAScript, return refers to the continuation established by the closure lexically innermost with respect to the statement - thus, a return within a closure transfers control to the code that called it. In Smalltalk
Smalltalk

Smalltalk is an Object-oriented programming, Type system, reflection computer programming programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human?computer symbiosis." It was designed and created in part for educational use, more so for constructionist learning, at PARC by Al...
, however, the superficially similar ^ operator invokes the escape continuation established for the method invocation, ignoring the escape continuations of any intervening nested closures. The escape continuation of a particular closure can only be invoked in Smalltalk implicitly by reaching the end of the closure's code. The following examples in ECMAScript and Smalltalk highlight the difference:

"Smalltalk" foo | xs | xs := #(1 2 3 4). xs do: [:x | ^x]. ^0 bar Transcript show: (self foo printString) "prints 1"

// ECMAScript function foo print(foo); // prints 0

The above code snippets will behave differently because the Smalltalk ^ operator and the JavaScript return operator are not analogous. In the ECMAScript example, return x will leave the inner closure to begin a new iteration of the forEach loop, whereas in the Smalltalk example, ^x will abort the loop and return from the method foo.

Common Lisp
Common Lisp

Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in American National Standards Institute standard document Information Technology - Programming Language - Common Lisp, formerly X3.226-1994 ....
 provides a construct that can express either of the above actions: Smalltalk
Smalltalk

Smalltalk is an Object-oriented programming, Type system, reflection computer programming programming language. Smalltalk was created as the language to underpin the "new world" of computing exemplified by "human?computer symbiosis." It was designed and created in part for educational use, more so for constructionist learning, at PARC by Al...
 ^x behaves as (return-from foo x), while JavaScript
JavaScript

JavaScript is a scripting language widely used for client-side web development. It was the originating Programming language dialect of the ECMAScript standard....
 return x behaves as (return-from nil x). Hence, Smalltalk makes it possible for a captured escape continuation to outlive the extent in which it can be successfully invoked. Consider:

foo ^[ :x | ^x ] bar | f | f := self foo. f value: 123 "error!"

When the closure returned by the method foo is invoked, it attempts to return a value from the invocation of foo that created the closure. Since that call has already returned and the Smalltalk method invocation model does not follow the spaghetti stack
Spaghetti stack

A spaghetti stack in computer science is an N-ary tree data structure in which child nodes have pointers to the parent nodes . When a list of nodes is traversed from a leaf node to the root node by chasing these parent pointers, the structure looks like a linked list stack ....
 discipline to allow multiple returns, this operation results in an error.

Some languages, such as Ruby
Ruby (programming language)

Ruby is a dynamic programming language, reflection , general purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features....
, allow the programmer to choose the way return is captured. An example in Ruby:

  1. ruby
def foo f = Proc.new f.call # control leaves foo here return "return from foo" end

def bar f = lambda f.call # control does not leave bar here return "return from bar" end

puts foo # prints "return from foo from inside proc" puts bar # prints "return from bar"

Both Proc.new and lambda in this example are ways to create a closure, but semantics of the closures thus created are different with respect to the return statement.

In Scheme, definition and scope of the return control statement is explicit (and only arbitrarily named 'return' for the sake of the example). The following is a direct translation of the Ruby sample.

(define call/cc call-with-current-continuation)

(define (foo) (call/cc (lambda (return) (define (f) (return "return from foo from inside proc")) (f) ; control leaves foo here (return "return from foo"))))

(define (bar) (call/cc (lambda (return) (define (f) (call/cc (lambda (return) (return "return from lambda")))) (f) ; control does not leave bar here (return "return from bar"))))

(display (foo)) ; prints "return from foo from inside proc" (newline) (display (bar)) ; prints "return from bar"

Implementation and theory

Closures are typically implemented with a special data structure
Data structure

A data structure in computer science is a way of storing data in a computer so that it can be used efficiently. It is an organization of mathematical and logical concepts of data....
 that contains a pointer to the function code, plus a representation of the function's lexical environment (e.g., the set of available variables and their values) at the time when the closure was created.

A language implementation cannot easily support full closures if its run-time memory model allocates all local variables on a linear stack. In such languages, a function's local variables are deallocated when the function returns. However, a closure requires that the free variables it references survive the enclosing function's execution. Therefore those variables must be allocated so that they persist until no longer needed. This explains why typically languages that natively support closures also use garbage collection
Garbage collection (computer science)

In computer science, garbage collection is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage , or memory used by Object that will never be accessed or mutated again by the Application software....
. The alternative is for the language to accept that certain use cases will lead to undefined behaviour
Undefined behaviour

In computer science, undefined behavior is a feature of some programming languages — most famously C . In these languages, to simplify the specification and allow some flexibility in implementation, the specification leaves the results of certain operations specifically undefined....
, as in the proposal for lambda expressions in C++.

In ML, local variables are allocated on a linear stack. When a closure is created, it copies the values of those variables that are needed by the closure into the closure's data structure.

A typical modern Scheme implementation allocates local variables that might be used by closures dynamically and stores all other local variables on the stack.

Closures are closely related to Actors in the Actor model
Actor model

In computer science, the Actor model is a mathematical model of concurrent computation that treats "actors" as the universal primitives of concurrent digital computation: in response to a message that it receives, an actor can make local decisions, create more actors, send more messages, and determine how to respond to the next message receiv...
 of concurrent computation where the values in the function's lexical environment are called acquaintances. An important issue for closures in concurrent programming languages is whether the variables in a closure can be updated and if so how these updates can be synchronized. Actors provide one solution.

See also

  • Lambda calculus
    Lambda calculus

    In mathematical logic and computer science, lambda calculus, also written as ?-calculus, is a formal system designed to investigate function definition, function application and recursion....
  • Funarg problem
    Funarg problem

    Funarg is an abbreviation for "functional argument"; in computer science, the funarg problem relates to a difficulty in implementing function s as first-class objects in stack-oriented programming language implementations....
  • Continuation
    Continuation

    In computing and programming, a continuation is an abstract representation of Control flow, or the "rest of computation" or "rest of code to be executed"....
  • Spaghetti stack
    Spaghetti stack

    A spaghetti stack in computer science is an N-ary tree data structure in which child nodes have pointers to the parent nodes . When a list of nodes is traversed from a leaf node to the root node by chasing these parent pointers, the structure looks like a linked list stack ....
  • Value-level programming
    Value-level programming

    Value-level programming refers to one of the two contrasting programming paradigms identified by John Backus in his work on Programs as mathematical objects, the other being Function-level programming....
  • Command pattern
    Command pattern

    In object-oriented programming, the Command pattern is a Design pattern in which an object is used to represent and Information Hiding all the information needed to call a method at a later time....
  • Eiffel programming language
    Eiffel (programming language)

    Eiffel is an International Organization for Standardization-standardized, object-oriented programming language designed to enable programmers to efficiently develop extensible, reusable, reliable software....
  • Anonymous function
    Anonymous function

    In computing, an anonymous function is a function defined, and possibly called, without being bound to a name. In lambda calculus, all functions are anonymous....
  • Dependency injection
    Dependency injection

    Dependency Injection in computer programming refers to the process of supplying an Coupling to a software component. It is a specific form of inversion of control where the concern being inverted is the process of obtaining the needed dependency....
  • Currying (a function)
    Currying

    In computer science, currying, invented by Moses Sch?nfinkel and Gottlob Frege, and independently by Haskell Curry, is the technique of transforming a function that takes multiple parameter in such a way that it can be called as a chain of functions each with a single argument....


External links


  • : A classic series of papers by Guy Steele and Gerald Sussman discussing, among other things, the versatility of closures in the context of Scheme (where they appear as lambda
    Lambda calculus

    In mathematical logic and computer science, lambda calculus, also written as ?-calculus, is a formal system designed to investigate function definition, function application and recursion....
     expressions
    ).
  • : An article about closures in dynamically-typed
    Type system

    In computer science, a type system may be defined as "a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute."....
     imperative languages, by Martin Fowler
    Martin Fowler

    Martin Fowler is an author and international speaker on software development, specializing in object oriented programming analysis and design, Unified Modeling Language, Design pattern , and agile software development methodologies, including extreme programming....
    .
  • : example of a technical domain where using closures is convenient, by Martin Fowler
    Martin Fowler

    Martin Fowler is an author and international speaker on software development, specializing in object oriented programming analysis and design, Unified Modeling Language, Design pattern , and agile software development methodologies, including extreme programming....
    .
  • : an article about using closures in Java and .NET
  • : an post on closures in Javascript

Closure in Delphi

  • Nick Hodges, "," October 2008, CodeGear Developer Network, CodeGear.
  • Craig Stuntz, "," October 2008
  • Dr. Bob, ""