Headline

A reference to a function with a referencing environment

Illustration

As an example, take Language:Perl's anonymous subroutines, which act as closures over all lexical variables in their scope:

#!/usr/bin/perl
use strict;
use warnings;

sub make_anonymous_sub {
    my $greeting = shift;   # lexical, block-scoped variable
    return sub {
        my $who = shift;
        print "$greeting $who!\n" # $greeting refers to above lexical variable
    };
    # $greeting goes out of scope here, but is still referenced by the returned
    # anonymous subroutine. The subroutine "remembers" its environment.
}

my $hello = make_anonymous_sub('Hello');
my $greet = make_anonymous_sub('Greetings');

$hello->('World'  ); # prints "Hello World!"
$greet->('Terrans'); # prints "Greetings Terrans!"

Citation

(http://en.wikipedia.org/wiki/Closure_(computer_science), 4 June 2013)

In computer science, a closure (also lexical closure or function closure) is a function or reference to a function together with a referencing environment—a table storing a reference to each of the non-local variables (also called free variables) of that function. ...

The concept of closures was developed in the 1960s and was first fully implemented in 1975 ... as a language feature in the Scheme programming language to support lexically scoped first-class functions.


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.