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
Line 27: Line 27:
</pre>
</pre>


==Closures in Ruby<ref>D. Thomas, C. Fowler, A. Hunt, "Programming Ruby 1.9, The Pragmatic Programmers' Guide", The Pragmatic Programmers,</ref>==
==Closures in Ruby<ref>D. Thomas, C. Fowler, A. Hunt, "Programming Ruby 1.9, The Pragmatic Programmers' Guide", The Pragmatic Programmers,LLC (2009)</ref>==


===What is a Closure?===
===What is a Closure?===

Revision as of 22:20, 14 September 2012

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 the closures in all 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<ref>D. Thomas, C. Fowler, A. Hunt, "Programming Ruby 1.9, The Pragmatic Programmers' Guide", The Pragmatic Programmers,LLC (2009)</ref>

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:

def closurefunc()
     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"

To avoid jargon, this is a more pertinent definition of Closures :

"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()
     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 and 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 (more info here) references the variables in the surrounding scope. Such a referenced variable is neither a parameter of the function nor a local variable. 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:

  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 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 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 'incremento' function using closures :

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

return inc

inc5 = incremento(5)
inc10 = incremento(10)

inc5 (5) # returns 10

As mentioned above, closures in Python are executed as function calls. The call to incremento 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 incremento function from the global namespace,

del incremento

even then, 
>>inc10(5) # returns 15

Why does this happen? Each call to incremento 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(anonymous functions).

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 incremento function using the lambda construct, we will see the following


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

>>f = incremento(2)
>>g = incremento(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 incremento(22)(33)
>>55

Comparison between Closure implementations

Topical References

<references/>

Further Reading