CSC/ECE 517 Fall 2012/ch1 1w4 aj: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(48 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=Closures in Ruby Vs. Closures in other Languages=
== Introduction ==
== Introduction ==


This article aims to explore the closure implementations, primarily in Ruby and also in other dynamic scripting languages like C#, JavaScript, Perl and Python. At the end of this article, we intend to present a comparison in the implementations of closures in all the languages, since each language handles it differently. The comparison is provided only to show how the closures are implemented. The article is not intended to be a reference guide for closures, but more like a quick start guide to understanding closures. References and additional learning resources are provided for the reader to go and explore the world of closures in much more detail


===Code Blocks===
===Code Blocks===
Line 29: Line 32:


===What is a Closure?===
===What is a Closure?===
Now, a block as shown before can use local variables from the surrounding scope. Such blocks are called <b>Closures</b>. Let us look at a simple example:
Now, a block as shown before can use local variables from the surrounding scope. Such blocks are called <b>Closures</b>. Let us look at a simple example<ref>Thomas,D. , Fowler, C. , Hunt, A. , "Programming Ruby 1.9, The Pragmatic Programmers' Guide", The Pragmatic Programmers,LLC (2009)</ref>:


<pre>
<pre>
def closurefunc()
def closurefunc(inc)
     lambda {|val| val + inc }
     lambda {|val| val + inc }
   end
   end
Line 44: Line 47:


In the above example, the value ‘3’ is assigned to the local variable <code>inc</code> of method <code>closurefunc</code> and value ‘1’ is assigned to inner variable <code>val</code>.
In the above example, the value ‘3’ is assigned to the local variable <code>inc</code> of method <code>closurefunc</code> and value ‘1’ is assigned to inner variable <code>val</code>.
A few definitions of closures are provided below :
<br>
<i>"A Closure is a block of executable code that is bunched together with references to the certain variables in scope at the site of its creation, which are retained even when the variables and referenced values go out of scope" </i><br>
<i>"A closure is a function that captures the bindings of free variables in its lexical context" </i><ref>Gafter, Neal. "A Definition of Closures" http://gafter.blogspot.com/2007/01/definition-of-closures.html</ref><br>
To avoid jargon, this is a more pertinent definition of Closures<ref>Cantrell, Paul."Closures in Ruby" http://innig.net/software/ruby/closures-in-ruby</ref> :
<i>"A closure is a block of code which meets three criteria: It can be passed around as a value and executed on demand by anyone who has that value, at which time it can refer to variables from the context in which it was created (i.e. it is closed with respect to variable access, in the mathematical sense of the word "closed")" </i><br>


===How does it work?===
===How does it work?===
Line 50: Line 63:


<pre>  
<pre>  
def concat()
def concat(param)
     lambda { |greet| greet + param }
     lambda { |greet| greet + param }
   end
   end
Line 61: Line 74:
</pre>
</pre>


The method <code>concat</code> returns a <code>Proc</code> object that references the method’s parameter, <code>param<code>.
The method <code>concat</code> returns a <code>Proc</code> object that references the method’s parameter, <code>param</code>.
We need to use the call method of the <code>Proc</code> object to execute it. Even though the parameter <code>param</code> is out of scope when the block is called, the parameter is still accessible to the block. This is called a closure; where in the variables in the surrounding scope referenced in a block remain accessible for the life of that block and the life of any <code>Proc</code> object created from that block.
We need to use the call method of the <code>Proc</code> object to execute it. Even though the parameter <code>param</code> is out of scope when the block is called, the parameter is still accessible to the block. This is called a closure; where in the variables in the surrounding scope referenced in a block remain accessible for the life of that block and the life of any <code>Proc</code> object created from that block.




===Why Closure is needed?===
===Why are closures needed?===
 
Although Closures are not mandatory requirements for a language, Closures provide an elegant way of binding the environment implicitly. Some of the popular languages like C, C++ and Java don't support Closures. Closures help in writing compact codes by avoiding the use of many arguments, as it closes the local variables in its surrounding.


===Other ways to use a closure===
===Other ways to use a closure===


Rather than calling the method <code>lambda</code>, following ways can be used to create <code>Proc</code> objects:<br>
Rather than calling the method <code>lambda</code>, following ways can be used to create <code>Proc</code> objects:<br><br><br>
<b>Using the ( -> ) syntax </b> <br>
<b>(i)</b> Using the <code>( -> )</code> syntax <br>
<code>->params  {  . . .  }</code><br>
<code>->params  {  . . .  } </code><br>
Example: <br>
Example: <br>
<code>
<code>
Line 77: Line 92:
obj.call(2,3)  # => 5
obj.call(2,3)  # => 5
</code><br>
</code><br>
Note: There cannot be any space between > and opening paranthesis. <br>
Note: There cannot be any space between > and opening paranthesis. <br><br>


By passing a block to the method whose last parameter is prefixed with and ampersand
<b>(ii)</b> By passing a block to the method whose last parameter is prefixed with an ampersand (&).
That parameter will receive the block as a Proc object.
That parameter will receive the block as a Proc object.
<pre>def sampmeth(v1, &block)
<pre>def sampmeth(v1, &block)
Line 91: Line 106:
   #<Proc:0x0b5f5e@/tmp/prog1.rb:4>
   #<Proc:0x0b5f5e@/tmp/prog1.rb:4>
   Nil
   Nil
</pre>
</pre><br>
By calling <code>Proc.new</code> by associating it with a block<br>
<b>(iii)</b> By calling <code>Proc.new</code> by associating it with a block<br>
<pre>obj = Proc.new { "a block" }  
<pre>obj = Proc.new { "a block" }  
obj
obj   #=> #<Proc:0x0a5e5a@/tmp/prog1.rb:1>
=>   #<Proc:0x0a5e5a@/tmp/prog1.rb:1>
</pre><br>
</pre>


===Scope of a Closure===
===Scope of a Closure===
Line 122: Line 136:
  block.call    # => [ 1, 2, 3, 4, "dummy" ]
  block.call    # => [ 1, 2, 3, 4, "dummy" ]
  ex.update_values
  ex.update_values
  block.call   # => [ 1, 2, 4, 5, "dummy" ]
  block.call   # => [ 1, 2, 4, 5, "dummy" ]


</pre>
</pre>


In the above example, the return_closure method returns a lambda that accesses local variable local_var, instance variable instance_var, class variable class_var and constant <code>CONST</code>. Even if the block is not in the scope of the object that contains these values, it is still accessible via the closure. So, when we update these values calling the update_values method, the values accessed via the closure also get updated.
In the above example, the return_closure method returns a lambda that accesses local variable <code>local_var</code>, instance variable <code>instance_var</code>, class variable <code>class_var</code> and constant <code>CONST</code>. Even if the block is not in the scope of the object that contains these values, it is still accessible via the closure. So, when we update these values calling the update_values method, the values accessed via the closure also get updated.
Closure is a very handy feature for Ruby developers and is used extensively. The rest of the chapter describes the implementation of Closures in different popular programming languages.
Closure is a very handy feature for Ruby developers and is used extensively. The rest of the chapter describes the implementation of Closures in different popular programming languages.
<br>
<br>
==Closures in other languages==
A closure or a closure-like implementation is available in most programming languages. Now that we have seen the implementation of closures in Ruby, let us explore the use of closures in other programming languages.<br>
===Closures in C#===
A closure in C# can be done using Lambda expressions and delegates, where a first-class function[http://en.wikipedia.org/wiki/First-class_function] references the variables in the surrounding scope. Such a referenced variable is neither a parameter of the function nor a local variable<ref>Hilyard, J. and Teilhet,S., "C# 3.0 Cookbook 3rd edition", O'reilly Pub. (2007)</ref>.
For example,
<pre>
  static void Main(string[] args)
{
var obj = ClosureFunc(1);
Console.Writeline("Output 1= " + obj(2));
Console.Writeline("Output 2= " + obj(3));
}
public static Func<int, int> ClosureFunc(inc)
{
Func<int,int> SampleFunc = delegate(int val)
{
inc = inc + 1;
return val + inc;
};
return SampleFunc;
}   
</pre>
The result obtained is:
<pre>
Output 1= 4
Output 2= 6
</pre>
Now, when we call <code>ClosureFunc</code> from <code>Main()</code>,we get a method back that increments a local variable inside the method. <code>inc</code> is a local variable of <code>ClosureFunc()</code> which is accessed inside of the delegate. So, when we call the method twice, the local variable <code>inc</code> gets incremented twice outside of its original scope.
What actually happens here is the C# compiler encapsulates the delegate and the associated local variables into a compiler generated class. So, on invoking the delegate, it creates a lambda expression to increment the value of <code>inc</code> and results in calling a method on this class.
Another way to write the ClosureFunc is using <code><b>lambda</b> ( => ) </code>
  <pre>Func<string,string> ClosureFunc = val => val + inc;</pre>
===Closures in JavaScript===
JavaScript is a dynamic and powerful scripting language which allows the use of Closures. An example of a closure in JavaScript is as follows<ref>Bhattacharya, A. , Sunder, K. , "Memory leak patterns in JavaScript", http://www.ibm.com/developerworks/web/library/wa-memleak/index.html</ref>:<pre>
  function ClosureFunc(inc)
  {
        return  SampleFunc (val) {
              return val + inc;
  }
  var obj = ClosureFunc(10);
  obj(5);  // returns 15
</pre>
Here, when a call is made to the outer function <code>ClosureFunc</code> with a parameter <code>inc</code>, the function returns with a pointer to the inner function <code>SampleFunc</code>, which is assigned to the variable <code>obj</code>.
===Closures in Perl===
Perl also supports closures extensively. Syntax for implementing closure in Perl<ref> Cozens, S., "Achieving Closure", http://www.perl.com/pub/2002/05/29/closure.html</ref> is similar to that of Ruby, which is as follows:
<pre>
    sub closurefunc {
my $inc = shift;
my $obj = sub  {
my $val = shift;
return $val + $inc;    # uses local variable inc
};
return $obj;
  }
 
  my $add3 = closurefunc(3);
  print "5 plus 3 is ":
  print $add3->(5);          #  =>  8
</pre>
Here, the <code>closurefunc</code> subroutine creates an anonymous function, which uses the local variable <code>inc</code> and returns the object <code>$obj</code>.
===Closures in Python===
Closures in python<ref>"Python Closures explained", http://www.shutupandship.com/2012/01/python-closures-explained.html</ref> are implemented as function calls. Support for closures has existed in Python since version 2.2, although the syntax of Python closures can actually confuse the user in thinking that the closures are not supported.
Consider following example code for creating an 'closurefunc' function using closures<ref>"Closures in Python : examples implementing Closures in Python", http://ynniv.com/blog/2007/08/closures-in-python.html</ref>:
<pre>
def closurefunc(x):
def inc(y):
    # x is "closed" in the definition of inc
    return y + x
return inc
inc5 = closurefunc(5)
inc10 = closurefunc(10)
inc5 (5) # returns 10
</pre>
As mentioned above, closures in Python are executed as function calls.
The call to <code><b>closurefunc</b></code> creates a binding for <code>x</code> which is referenced inside the function <code>inc</code>. In technical terms, the function <code>inc</code> is closed over the variable <code><b>x</b></code>.
If we go ahead and delete the <code><b>closurefunc</b></code> function from the global namespace,
<pre>
del closurefunc
</pre>
even then,
<pre>
>>inc10(5) # returns 15
</pre>
Why does this happen? Each call to <code><b>closurefunc</b></code> creates a new instance, returning an <code>inc</code> function object and assigning it to the <code>inc5</code> and <code>inc10</code>. Each instance has a link to a different binding of <code>x</code>. Hence, the function <code>inc</code> will have access to <code>x</code> even if the scope where <code>x</code> was defined doesn’t exist anymore.
The above example shows the closure of <code>x</code> being used to eliminate either a global or a constant, depending on the nature of <code>x</code>.
Python also supports the use of closures through the <code><b>lambda</b></code> constructs.
The difference between a normal function and a <code><b>lambda</b></code> function is shown below
<pre>
>>> def f (x): return x**2  #normal function definition
...
>>> print f(8)
64
>>> g = lambda x: x**2      #demo of lambda construct
>>>
>>> print g(8)
64
</pre>
As you can see, the <code><b>lambda</b></code> function is defined on the fly and always returns an expression. If we write a new <code><b>closurefunc</b></code> function using the <code><b>lambda</b></code> construct, we will see the following
<pre>
>>def closurefunc(n): return lambda x: x + n #creation of lambda function
>>f = closurefunc(2)
>>g = closurefunc(6)
>>print f(42), g(42)
>>44, 48
</pre>
The lambda function is created on the fly and returns an expression which is assigned to <code>f</code> and <code>g</code>. The <code><b>lambda</b></code> construct works even without assigning the returned expression, as shown next
<pre>
>>print closurefunc(22)(33)
>>55
</pre>
<br>
==Conclusion==
In conclusion, a brief introduction to closures and its usage in dynamic scripting languages has been provided above. The code examples illustrate the syntax and semantics supported by these languages. A concise comparison between the implementation in these languages is summarized below. References are provided for the users to enhance their understanding of the subject. <br>
{| class="wikitable"
|-
!  !! Ruby !! C# !! JavaScript !! Perl !! Python !! C / C++ / Java
|-
| <b>Support for Closures</b>|| Yes|| Yes|| Yes|| Yes|| Yes|| No
|-
| <b>Closure implementation using</b>|| Proc, block and Lambdas || Delegates and First Class functions || Unnamed functions || Anonymous functions || Named function calls, Lambda|| No
|-
| <b>Support for Lambda implementation</b>|| Yes|| Yes|| No|| No direct support, can be simulated using anonymous functions|| Yes|| No
|}
<br>
== Topical References ==
<references/>
<br>
== Further Reading ==
[http://en.wikipedia.org/wiki/Closure_(computer_science) Wikipedia - Closures] : everything there is to know about Closures on one page.
[http://weblog.raganwald.com/2007/01/closures-and-higher-order-functions.html Reg Braithwaite - Closures and Higher-Order Functions] : gives Ruby examples of specialized Closures.
[http://ruby.runpaint.org/closures Closures in Ruby] : gives a detailed explanation of Closures in Ruby.
[http://msdn.microsoft.com/en-us/magazine/ee309512.aspx MSDN Magazine - Functional Programming .NET Development] : examples of functional style with Closures in C#.
[http://www.randomhacks.net/articles/2007/02/01/some-useful-closures-in-ruby Eric Kidd - Some useful closures, in Ruby] : demonstrates more examples of curried and specialized Closures in Ruby.
[http://onestepback.org/articles/invitationtoruby/reason4.html Jim Weirich - Top Ten Reasons I Like Ruby - Blocks and Closures] : provides several examples of how Closures can be useful in Ruby.
[http://csharpindepth.com/Articles/Chapter5/Closures.aspx C# in Depth - The Beauty of Closures] : demonstrates Closures in C#.
[http://martinfowler.com/bliki/Closure.html Martin Fowler - Closure] : provides a basic definition of Closures and gives a few very basic
examples.
[http://jibbering.com/faq/notes/closures Javascript Closures] : demonstrates Javascript Closures in callbacks.
[http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/ Alan Skorkin - Closures - A Simple Explanation] : gives a high-level overview of Closures using Ruby examples.
[http://www.ibm.com/developerworks/java/library/j-cb01097/index.html IBM Developerworks - Crossing borders : Closures] : gives Ruby examples where Closures are useful.
[http://samdanielson.com/2007/9/6/an-introduction-to-closures-in-ruby Sam Danielson - An Introduction to Closures in Ruby] : high-level overview of using Closures in Ruby.

Latest revision as of 05:15, 20 September 2012

Closures in Ruby Vs. Closures in other Languages

Introduction

This article aims to explore the closure implementations, primarily in Ruby and also in other dynamic scripting languages like C#, JavaScript, Perl and Python. At the end of this article, we intend to present a comparison in the implementations of closures in all the languages, since each language handles it differently. The comparison is provided only to show how the closures are implemented. The article is not intended to be a reference guide for closures, but more like a quick start guide to understanding closures. References and additional learning resources are provided for the reader to go and explore the world of closures in much more detail

Code Blocks

Before understanding the concept of Closures, let us take a brief introduction on “Code Blocks” in Ruby. A code block is a chunk of code, Ruby statements and expressions written between curly braces { } or between “do…end”. For example:

 {  puts "Hello World!"  }

or

  do
     3.times(puts "Hello")
     object1.call
  end

Generally, as per Ruby standard, braces are used for single-line blocks and do...end for multiline blocks. Also, braces have a higher preference than do/end. A code block may appear only immediately after a method is invoked. If a method has parameters, then the block will look as follows:

 random_method("John") { puts "How you doing? " }

Closures in Ruby

What is a Closure?

Now, a block as shown before can use local variables from the surrounding scope. Such blocks are called Closures. Let us look at a simple example<ref>Thomas,D. , Fowler, C. , Hunt, A. , "Programming Ruby 1.9, The Pragmatic Programmers' Guide", The Pragmatic Programmers,LLC (2009)</ref>:

def closurefunc(inc)
     lambda {|val| val + inc }
  end

  p1 = closurefunc(3)
  p1.call(1)    # => 4
 
  p2 = closurefunc(8)
  p2.call(5)   # => 13

In the above example, the value ‘3’ is assigned to the local variable inc of method closurefunc and value ‘1’ is assigned to inner variable val.

A few definitions of closures are provided below :
"A Closure is a block of executable code that is bunched together with references to the certain variables in scope at the site of its creation, which are retained even when the variables and referenced values go out of scope"

"A closure is a function that captures the bindings of free variables in its lexical context" <ref>Gafter, Neal. "A Definition of Closures" http://gafter.blogspot.com/2007/01/definition-of-closures.html</ref>

To avoid jargon, this is a more pertinent definition of Closures<ref>Cantrell, Paul."Closures in Ruby" http://innig.net/software/ruby/closures-in-ruby</ref> :

"A closure is a block of code which meets three criteria: It can be passed around as a value and executed on demand by anyone who has that value, at which time it can refer to variables from the context in which it was created (i.e. it is closed with respect to variable access, in the mathematical sense of the word "closed")"

How does it work?

Let us look at a similar example, but this time, with strings.

 
def concat(param)
     lambda { |greet| greet + param }
  end

  p1 = concat("John")
  p1.call("Hello")    # => "HelloJohn"
 
  p2 = concat("Jim")
  p2.call("Good morning ")   # => "Good morning Jim"

The method concat returns a Proc object that references the method’s parameter, param. We need to use the call method of the Proc object to execute it. Even though the parameter param is out of scope when the block is called, the parameter is still accessible to the block. This is called a closure; where in the variables in the surrounding scope referenced in a block remain accessible for the life of that block and the life of any Proc object created from that block.


Why are closures needed?

Although Closures are not mandatory requirements for a language, Closures provide an elegant way of binding the environment implicitly. Some of the popular languages like C, C++ and Java don't support Closures. Closures help in writing compact codes by avoiding the use of many arguments, as it closes the local variables in its surrounding.

Other ways to use a closure

Rather than calling the method lambda, following ways can be used to create Proc objects:


(i) Using the ( -> ) syntax
->params { . . . }
Example:
obj = ->(v1,v2) { v1 + v2 }
obj.call(2,3) # => 5

Note: There cannot be any space between > and opening paranthesis.

(ii) By passing a block to the method whose last parameter is prefixed with an ampersand (&). That parameter will receive the block as a Proc object.

def sampmeth(v1, &block)
    puts block.inspect
 end
  sampmeth(1) { "a block" }
  sampmeth(3)

produces:

   #<Proc:0x0b5f5e@/tmp/prog1.rb:4>
  Nil


(iii) By calling Proc.new by associating it with a block

obj = Proc.new { "a block" } 
obj   #=>  #<Proc:0x0a5e5a@/tmp/prog1.rb:1>


Scope of a Closure

Closure remembers the context in which it is defined, and uses that context whenever it is called. The context may include the value of self, constants, local variables, class variables and any defined block.

class ClosureExample
  CONST = 1
  @@class_var = 4

  def return_closure
   local_var = 2
   @instance_var = 3
   lambda { [ CONST, local_var, @instance_var,  @@class_var, yield ] }
  end

  def update_values
    @instance_var += 1
    @@class_var += 1
  end
 end

 ex = ClosureExample.new 
 block = ex.return_closure { "dummy" }
 block.call    # => [ 1, 2, 3, 4, "dummy" ]
 ex.update_values
 block.call    # => [ 1, 2, 4, 5, "dummy" ]

In the above example, the return_closure method returns a lambda that accesses local variable local_var, instance variable instance_var, class variable class_var and constant CONST. Even if the block is not in the scope of the object that contains these values, it is still accessible via the closure. So, when we update these values calling the update_values method, the values accessed via the closure also get updated. Closure is a very handy feature for Ruby developers and is used extensively. The rest of the chapter describes the implementation of Closures in different popular programming languages.

Closures in other languages

A closure or a closure-like implementation is available in most programming languages. Now that we have seen the implementation of closures in Ruby, let us explore the use of closures in other programming languages.

Closures in C#

A closure in C# can be done using Lambda expressions and delegates, where a first-class function[1] references the variables in the surrounding scope. Such a referenced variable is neither a parameter of the function nor a local variable<ref>Hilyard, J. and Teilhet,S., "C# 3.0 Cookbook 3rd edition", O'reilly Pub. (2007)</ref>. For example,

  static void Main(string[] args)
 {
	var obj = ClosureFunc(1);
	Console.Writeline("Output 1= " + obj(2));
	Console.Writeline("Output 2= " + obj(3));
 }
 public static Func<int, int> ClosureFunc(inc)
 {
	Func<int,int> SampleFunc = delegate(int val)
		{
			inc = inc + 1;
			return val + inc;
		};
	return SampleFunc;
 }    

The result obtained is:

Output 1= 4
Output 2= 6

Now, when we call ClosureFunc from Main(),we get a method back that increments a local variable inside the method. inc is a local variable of ClosureFunc() which is accessed inside of the delegate. So, when we call the method twice, the local variable inc gets incremented twice outside of its original scope. What actually happens here is the C# compiler encapsulates the delegate and the associated local variables into a compiler generated class. So, on invoking the delegate, it creates a lambda expression to increment the value of inc and results in calling a method on this class. Another way to write the ClosureFunc is using lambda ( => )

Func<string,string> ClosureFunc = val => val + inc;

Closures in JavaScript

JavaScript is a dynamic and powerful scripting language which allows the use of Closures. An example of a closure in JavaScript is as follows<ref>Bhattacharya, A. , Sunder, K. , "Memory leak patterns in JavaScript", http://www.ibm.com/developerworks/web/library/wa-memleak/index.html</ref>:

  function ClosureFunc(inc)
  { 
        return  SampleFunc (val) {
               return val + inc;
  }
  var obj = ClosureFunc(10);
  obj(5);   // returns 15

Here, when a call is made to the outer function ClosureFunc with a parameter inc, the function returns with a pointer to the inner function SampleFunc, which is assigned to the variable obj.

Closures in Perl

Perl also supports closures extensively. Syntax for implementing closure in Perl<ref> Cozens, S., "Achieving Closure", http://www.perl.com/pub/2002/05/29/closure.html</ref> is similar to that of Ruby, which is as follows:

    sub closurefunc {
	 my $inc = shift;
	my $obj = sub  {
		my $val = shift;
		return $val + $inc;     # uses local variable inc
	};
	return $obj;
   }
   
   my $add3 = closurefunc(3);
   print "5 plus 3 is ":
   print $add3->(5);          #  =>  8

Here, the closurefunc subroutine creates an anonymous function, which uses the local variable inc and returns the object $obj.

Closures in Python

Closures in python<ref>"Python Closures explained", http://www.shutupandship.com/2012/01/python-closures-explained.html</ref> are implemented as function calls. Support for closures has existed in Python since version 2.2, although the syntax of Python closures can actually confuse the user in thinking that the closures are not supported.

Consider following example code for creating an 'closurefunc' function using closures<ref>"Closures in Python : examples implementing Closures in Python", http://ynniv.com/blog/2007/08/closures-in-python.html</ref>:

def closurefunc(x):
 def inc(y):
    # x is "closed" in the definition of inc
    return y + x

return inc

inc5 = closurefunc(5)
inc10 = closurefunc(10)

inc5 (5) # returns 10

As mentioned above, closures in Python are executed as function calls. The call to closurefunc creates a binding for x which is referenced inside the function inc. In technical terms, the function inc is closed over the variable x.

If we go ahead and delete the closurefunc function from the global namespace,

del closurefunc

even then,

>>inc10(5) # returns 15

Why does this happen? Each call to closurefunc creates a new instance, returning an inc function object and assigning it to the inc5 and inc10. Each instance has a link to a different binding of x. Hence, the function inc will have access to x even if the scope where x was defined doesn’t exist anymore.

The above example shows the closure of x being used to eliminate either a global or a constant, depending on the nature of x.

Python also supports the use of closures through the lambda constructs.

The difference between a normal function and a lambda function is shown below


>>> def f (x): return x**2  #normal function definition
... 
>>> print f(8)
64

>>> g = lambda x: x**2      #demo of lambda construct
>>> 
>>> print g(8)
64

As you can see, the lambda function is defined on the fly and always returns an expression. If we write a new closurefunc function using the lambda construct, we will see the following


>>def closurefunc(n): return lambda x: x + n #creation of lambda function	

>>f = closurefunc(2)
>>g = closurefunc(6)
 
>>print f(42), g(42)
>>44, 48
 

The lambda function is created on the fly and returns an expression which is assigned to f and g. The lambda construct works even without assigning the returned expression, as shown next

>>print closurefunc(22)(33)
>>55


Conclusion

In conclusion, a brief introduction to closures and its usage in dynamic scripting languages has been provided above. The code examples illustrate the syntax and semantics supported by these languages. A concise comparison between the implementation in these languages is summarized below. References are provided for the users to enhance their understanding of the subject.

Ruby C# JavaScript Perl Python C / C++ / Java
Support for Closures Yes Yes Yes Yes Yes No
Closure implementation using Proc, block and Lambdas Delegates and First Class functions Unnamed functions Anonymous functions Named function calls, Lambda No
Support for Lambda implementation Yes Yes No No direct support, can be simulated using anonymous functions Yes No


Topical References

<references/>

Further Reading

Wikipedia - Closures : everything there is to know about Closures on one page.

Reg Braithwaite - Closures and Higher-Order Functions : gives Ruby examples of specialized Closures.

Closures in Ruby : gives a detailed explanation of Closures in Ruby.

MSDN Magazine - Functional Programming .NET Development : examples of functional style with Closures in C#.

Eric Kidd - Some useful closures, in Ruby : demonstrates more examples of curried and specialized Closures in Ruby.

Jim Weirich - Top Ten Reasons I Like Ruby - Blocks and Closures : provides several examples of how Closures can be useful in Ruby.

C# in Depth - The Beauty of Closures : demonstrates Closures in C#.

Martin Fowler - Closure : provides a basic definition of Closures and gives a few very basic examples.

Javascript Closures : demonstrates Javascript Closures in callbacks.

Alan Skorkin - Closures - A Simple Explanation : gives a high-level overview of Closures using Ruby examples.

IBM Developerworks - Crossing borders : Closures : gives Ruby examples where Closures are useful.

Sam Danielson - An Introduction to Closures in Ruby : high-level overview of using Closures in Ruby.