Closure (computer science)

From Wikipedia, the free encyclopedia
Jump to: navigation, search

In computer science, a closure is a function that has an environment of its own. Inside this environment, there is at least one bound variable. Closures were first used in programming languages such as ML and Lisp.

Closures keep the state of their bound variables over several invocations.

The term closure is often mistakenly used to mean anonymous function. 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 different 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 objects are sometimes also called closures.

Closures and first-class functions[change | change source]

Closures typically appear in languages in which functions are first-class values–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 (JavaScript), another popular language with support for closures:

// Return a list of all books with at least 'threshold' copies sold.
function bestSellingBooks(threshold) {
  return bookList.filter(
      function(book) { return book.sales >= threshold; }
    );
}

The function keyword is used here instead of lambda, and an Array.filter method [1] 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) {
  return function(x) {
    return (f(x + dx) - f(x)) / dx;
  };
}

Because the closure in this case outlives the scope 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.

While this is not always clarified, a closure need not be formed using an anonymous function. The Python 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:

# Return a function that approximates the derivative of f
# 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. In fact, closures in Python must often be formed using named functions, where an anonymous function might be equally appropriate in other languages, because of the restrictions on lambda forms.[2]

Uses of closures[change | change source]

Closures have many uses:

  • Designers of software libraries can allow users to customize behavior by passing closures as arguments to important functions. For example, a function that sorts 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'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 systems.[3]

Note: Some speakers call any data structure that binds a lexical environment a closure, but the term usually refers specifically to functions.

References[change | change source]