CSC/ECE 517 Fall 2010/ch1 n 00

From Expertiza_Wiki
Revision as of 22:01, 7 September 2010 by Cgoogc (talk | contribs) (→‎Javascript)
Jump to navigation Jump to search

Closures in Ruby vs. closures in other languages - Introduction to Closure

What is Closure?
There are multiple definitions of Closures given by creators of different languages. Wikipedia defines Closure as follows:

A Closure is a first-class function with free variables that are bound in the lexical environment.

One of the earliest known programming language that implemented Closure, Lisp defines Closure as follows:

A closure is a function that captures the bindings of free variables in its lexical context.

SmallTalk an object oriented language defines closure differently as follows:

A closure is a function that binds all the free variables appearing in methods to the scope of the object that the method is a member of.

A not so valid, but an easy to understand definition for Closure is given as:

Closure is a special function pointer which has access to the local variables of the enclosing function, where the Closure is created. The variables which holds the function pointer (Closure) can be passed around just like variables and can refer to local variables even after the life time of the context where, Closure was created.

More convincing, easy to understand definition is from 5:

  • 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").

Are you lost? But don’t worry, there is a lot of debate over what closure should be, how it should work and how it should be implemented? You will find lot of discussions and debates about this topic in the Internet. For more details and historic development of Closure is available here 1

What Closure is not

This section attempts to clarify some of the general misconceptions about Closure. Those who have never experienced Closure, might relate things close to Closure as Closure.

  • C Programmer: Might tend to think Closures are like Function Pointers. But that is not true, Closure is more than function pointers, because function pointers does not close local variables in the context. Function pointers are not considered First-Class Objects.
  • Java Programmer: Inner Classes are similar to Closures but not Closures exactly. Inner class can access only instance variables of the surrounding class and has only READ-ONLY access to the local variables. The local variables must be marked final to make them available for the inner class to use them.

Why Closure is needed?

If you think, Closure is not a mandatory requirement for a language. Many popular programming languages like C, Java, C++ does not have Closure. Closure adds more convenience to a language, at the same time it makes it difficult for first time users. With Closure, you can really write short code and avoid passing too many arguments, since Closure closes the local variables.

When to use Closure?

Closure is a very handy tool which makes the code less verbose. You can use Closure where you used Function Pointers, Anonymous Functions(), Anonymous Inner Classes. This is a very common piece of code in Java for creating a Thread.

Thread t = new Thread(new Runnable() {

  public void run() {
  // thread logic
  }

});

The above piece of code if re-written with Closures take a very simple form as follows:

Thread t = new Thread(#() {

   // thread logic

});

Above statement is written using one of proposed Closure syntax addition for Java 7 (Java does not have Closures yet). You can see that the code is lot less verbose and neat. When Closures are available in Java, you can see that you can do away with most of the Callback classes used in Java.

Closures in the form of Delegates are very actively used in C#. Delegates are used to add and remove event listener in C# elegantly. To read more about the use of Delegates in C# see 3.

Closure vs Lambda

Closure and Lambda expressions are very similar concepts. Both represents a block of executable code, but lambda expression does not have to close the context. Lambda expression can be viewed as unnamed anonymous function. Unlike Closure, Lambda expressions are very loosely defined in Computer Science.

Closure too represents a block of executable code, but Closure closes the context and makes available the variables that are used inside the closure, even after the life time of those stack variables.

We will see later that there is a very subtle difference between Closure and Lambda in Ruby, though both Closure and Lambda encloses the context.

Closure in Ruby

A closure in Ruby as mentioned by Yukihiro Matsumoto4, the creator of Ruby "A closure is a nameless function the way it is done in Lisp. A closure object has the code to run, the executable, state around the code and the scope.So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away."

Since we spent considerable amount of time in class discussing about Ruby, We will spend more time on discussing closure in Ruby too.

Probably when you are reading this, you might not be very clear about what Closure, Blocks and Proc. Are they different? How do they work? This section tries to answer those questions. If, we have done a good job, probably you should understand what they are after reading this section.

Blocks is a unnamed piece of code, which can be called. Blocks is in fact a closure since it has access to the local variables in the context where it was defined.

def thrice_x

   x = 50
   yield
   yield
   yield
   puts "x inside thrice_x: #{x}"

end

def test_closure

 x = 5
 thrice_x { x += 1 }
 puts "value of outer x after: #{x}"

end

test_closure()

Output is: x inside thrice_x: 50 value of outer x after: 8

From the above output we can understand that x in thrice_x { x += 1 } refers to the x that is defined inside test_closure function, ie., the context where Block is defined and not from where it is executed.

Just as a side note, block can also be defined using do..end instead of { .. } it means the same.

Consider another example below for Closure. We all know about the synchronized block in Java, but with Closure in Ruby, we have lot more freedom of designing our own blocks.

def lock_and_run

 # try locking
 success = false
 yield(success)
 # unlock

end

lock_and_run { |x|

 if x==true
   # do operation
   puts "Doing dangerous operation!"
 else
   puts "Unable to lock resource"
 end

}

The block of code, enclosed in lock_and_run function, first attemps to lock an resource, if it were able to lock it successfully (reflected by success flag), it calls the block of code with the success flag. Change success to true and false to see what we are talking about.

Difference between Ruby Block and Ruby Proc

Ruby philosophy is everything in Ruby is an object, but not a Block of code. Both Block and Proc represents a block of executable code, but Proc is presented by a class and hence a block of proc is an object and hence can be passed around as arguments, whereas Blocks are not objects, so they cannot be based as argument. [6]

dash_printer = Proc.new { puts "-------------" };

dash_printer.call

The above is a perfectly valid Proc, but below code is not valid. Since block is not an object, you don’t have any functions like call to execute the block of code.

dash_printer = { puts "-------------" };

dash_printer.[ no function to call?? ]

Procs and Lambdas

As defined in [6], the relationship between procs, blocks and lambdas can be described as below: “Blocks are syntactic structures in Ruby; they are not objects, and cannot be manipulated as objects. It is possible, however, to create an object that represents a block. Depending on how the object is created, it is called a proc or a lambda. Procs have block-like behavior and lambdas have method-like behavior. Both, however, are instances of class Proc.” - David Flanagan; Yukihiro Matsumoto

Another way of creating a proc block is as follows. Taken from [6]:

  1. This method creates a proc from a block

def makeproc(&p) # block can be received as argument using the & symbol.

  p

end

We can create the proc object as follows:

adder = makeproc {|x,y| x+y }

Lambda:

Lambda expressions are very similar to Procs. There are created using the following syntax;

doubler = lambda {|x| x+x}

You can call doubler using doubler.call(10) returning 20

The above can also be created using the below syntax from Ruby 1.9 [6]:

doubler = ->(x) { x+x }

Arity of proc

Arity of a proc or lambda is defined as the number of arguments expected by the proc or lambda.

lambda { | | }.arity --- is 0

lambda { | a, b | }.arity --- is 2

Another important difference between lambda and proc is the way return works and as a Ruby programmer, we all must be aware of the difference.

Lambda block is evaluated as though its call to the function. Very similar to the way function call works. Proc block is evaluated as though the function is inlined. We can better understand the above with the below example. When a return statement is executed inside a Proc block, it actually returns from the function which called the proc block.

def level2Builder(message)

 lambda { puts message; return }

end

def level1

 puts "level1"
 p = level2Builder("call level2")
 p.call                 
 puts "back to level1" 

end level1

def level2Builder(message)

 proc { puts message; return }

end

def level1

 puts "level1"
 p = level2Builder("call level2")
 p.call                 
 puts "back to level1" 

end level1

Output level1 call level2 back to level1

Output level1 call level2 understanding_blocks.rb:26:in `block in level2Builder': unexpected return (LocalJumpError)

Perl

Perl has extensive support for Closure with a syntax very similar to the Proc block of Ruby.

sub create_incrementer {

       my $step = shift;
       my $inc = sub {
               my $num = shift; 
               return $num + $step; // closes local variable step
       };
       return $inc;

}

my $by5 = create_incrementer(5);

print "10 Increment by 5 is ";

print $by5->(10);

You can see that create_incrementer recreates a closure which closes the local variable step. the output of the above program is 10 Increment by 5 is 15

Javascript

Javascript supports Closure too. Below is an example of how closure can be created in Javascript

function createIncrementer(step) {

    return function(val) {
        return val + step;
    }

}


It is important to understand that Closure can sometime hide Circular Dependencies which can cause memory leaks in Javascript. 7 discusses lot more about memory leaks in Javascript.

C#

C# supports full Closure and Functional Programming starting from version 3.0. Earlier versions of C# had something similar to Closure called Delegates. Annonymous delegates introduced in version 3.0 supports Closure.

namespace closure_csharp {

   class Program
   {
       delegate int incrementer(int i);
       static incrementer createIncrementer(int step)
       {
           incrementer incr = i => (i + step); // closes local variable step
           return incr;
       }
       static void Main(string[] args)
       {
           incrementer by5 = createIncrementer(5);
           Console.WriteLine(by5(10));
       }
   }

}

We can see the createIncrementer creates a delegates and closes the local variable step.


External links