CSC/ECE 517 Fall 2010/ch1 1b YL: Difference between revisions
Line 3: | Line 3: | ||
== Introduction == | == Introduction == | ||
Before discussing semantic and implementation differences of closure implementation in different languages, closures should be discussed first. A closure is a first-class function with free variables that are bound in the lexical environment. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Closures:Wikipedia] | Before discussing semantic and implementation differences of closure implementation in different languages, closures should be discussed first. | ||
<Blockquote>A closure is a first-class function with free variables that are bound in the lexical environment. [http://en.wikipedia.org/wiki/Closure_%28computer_science%29 Closures:Wikipedia]</Blockquote> | |||
Such a function is said to be "closed over" its free variables. In this definition first-class function is the function which is treated 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 them as the values of other functions. [http://en.wikipedia.org/wiki/First-class_function] So closure can be explained as nothing but the behavior or in programming language function which can be passed around like any other argument and still remembering the original context where these functions are declared first. This way it breaks out of the standard function concept where the variables inside functions can be referenced within the scope of the function. Instead in closures these variables can be referred out of the scope of the local functions. | Such a function is said to be "closed over" its free variables. In this definition first-class function is the function which is treated 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 them as the values of other functions. [http://en.wikipedia.org/wiki/First-class_function] So closure can be explained as nothing but the behavior or in programming language function which can be passed around like any other argument and still remembering the original context where these functions are declared first. This way it breaks out of the standard function concept where the variables inside functions can be referenced within the scope of the function. Instead in closures these variables can be referred out of the scope of the local functions. | ||
Revision as of 04:42, 7 September 2010
Closures in Ruby vs. closures in other languages
Introduction
Before discussing semantic and implementation differences of closure implementation in different languages, closures should be discussed first.
A closure is a first-class function with free variables that are bound in the lexical environment. Closures:Wikipedia
Such a function is said to be "closed over" its free variables. In this definition first-class function is the function which is treated 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 them as the values of other functions. [1] So closure can be explained as nothing but the behavior or in programming language function which can be passed around like any other argument and still remembering the original context where these functions are declared first. This way it breaks out of the standard function concept where the variables inside functions can be referenced within the scope of the function. Instead in closures these variables can be referred out of the scope of the local functions.