CSC/ECE 517 Fall 2010/ch1 1b YL: Difference between revisions
Line 11: | Line 11: | ||
Closure concept can be illustrated with the example in JavaScript. | Closure concept can be illustrated with the example in JavaScript. | ||
<code> | <code> | ||
<source lang="javascript"> | <source lang="javascript"> | ||
// Return a list of all employees with at least 'threshold' salary. | // Return a list of all employees with at least 'threshold' salary. | ||
Line 18: | Line 19: | ||
} | } | ||
</source> | </source> | ||
</code> | </code> | ||
In above javascript function <code>highlyPaidEmployees</code>, another anonymous function is defined. This function is passed to the <code>Array.filter</code> method with free variable threshold. This free variable is assigned the value that is passed during the function call of highlyPaidEmployees. Thus the anonymous function defined inside is called repeatedly by filter method with free variable threshold having assigned the value passed to the highlyPaidEmployees. This follows both the properties defined in the closure definition. One is that the function is passed as an argument and function is able to access free variable out of its context. | In above javascript function <code>highlyPaidEmployees</code>, another anonymous function is defined. This function is passed to the <code>Array.filter</code> method with free variable threshold. This free variable is assigned the value that is passed during the function call of highlyPaidEmployees. Thus the anonymous function defined inside is called repeatedly by filter method with free variable threshold having assigned the value passed to the highlyPaidEmployees. This follows both the properties defined in the closure definition. One is that the function is passed as an argument and function is able to access free variable out of its context. | ||
Revision as of 05:31, 7 September 2010
Closures in Ruby vs. closures in other languages
Closures
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. However, in closures these variables can be referred out of the scope of the local functions. This ability of accessing original context makes closure different from normal objects.
Closure concept can be illustrated with the example in JavaScript.
// Return a list of all employees with at least 'threshold' salary.
function highlyPaidEmployees(threshold)
{
return empList.filter( function (emp) { return emp.salary >= threshold; } );
}
In above javascript function highlyPaidEmployees
, another anonymous function is defined. This function is passed to the Array.filter
method with free variable threshold. This free variable is assigned the value that is passed during the function call of highlyPaidEmployees. Thus the anonymous function defined inside is called repeatedly by filter method with free variable threshold having assigned the value passed to the highlyPaidEmployees. This follows both the properties defined in the closure definition. One is that the function is passed as an argument and function is able to access free variable out of its context.
Closure is supported by many languages. Ruby is famous for extensive use of closure. In next few sections comparison of closure implementation is different languages is done.