CSC/ECE 517 Fall 2012/ch2a 2w26 aj

From Expertiza_Wiki
Revision as of 23:14, 26 October 2012 by Avranade (talk | contribs)
Jump to navigation Jump to search

SaaS 4.6 Enhancing Rotten Potatoes again

Introduction

Code Blocks

Closures in Ruby

What is a Closure?

How does it work?

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.