CSC/ECE 517 Fall 2011/ch1 1d gs: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 58: Line 58:
     return emps.select {|e| e.isManager}
     return emps.select {|e| e.isManager}
   end
   end
==Statically Typed vs Dynamically Typed Languages==
==Closures in Ruby==
==Scheme Closures and Scoping==
==Closures in Statically Typed Languages[A Challenge, Why?]==
===Closure like constructs in C===
===Closures in Java===
===Java Lexical Scoping for Closures===
===Return from Closure===
===Safety===
==References==

Revision as of 03:00, 8 September 2011

Closures for Statically Typed Languages

Introduction

This wiki gives an introduction to language constructs called closures, it’s usage and discusses about challenges involved in implementing them in statically typed languages.

Closures

A closure is a kind of routine that can be assigned to a variable or passed as a parameter to another routine. It can access the local state (local variables, parameters, methods, and instance variables) which is visible in the place it was defined.

To rephrase, a closure is a block of code which meets the following three criteria

1. It can be passed around as a value.

2. It can executed on demand by anyone who has that value, at which time.

3. It can refer to variables from the context in which it was created.

Why do we need closures and what are its uses?

DRY(Don't Repeat Yourself)[1] is a popular software development principle, formulated by Andy Hunt and Dave Thomas, which stresses the importance of not duplicating code. Closures help in implementing the DRY principle and make the code easy to maintain.Closures increase considerably the level of a language by mixing access to local variables with remote execution of a set of locally-defined statements.

Lets see an example,

 def paidMore(amount)
 return Proc.new {|e| e.salary > amount}
 end

This function returns a closure, the behavior of which depends on the parameter amount passed to the enclosing function.

Amount can be fixed to a value like below, the variable highPaid contains a block of code (called a Proc in Ruby) that will return whether an employee’s salary is greater than 150.

highPaid = paidMore(150)

//Let’s check john’s salary

john = Employee.new john.salary = 200 print highPaid.call(john)

The expression highPaid.call(john) executes the e.salary > 150 block and prints the result of the execution. As long as a closure lives, all the free variables accessed by it are not eligible for garbage collection. So, the variable amount persists until the closure returned by paidMore does. Hence from the above sample code , it is clear that even if value 150 went out of scope, at the time of issuing the print call , the binding still remains.

The art of getting the best possible results by minimal optimisation of code and the ease with which a user can use closures makes the latter a big success in Ruby.

Another Example:

A function to determine if an employee is a manager.

Using C#, I'd probably write it like this.

public static IList Managers(IList emps) {
   IList result = new ArrayList();
   foreach(Employee e in emps)
     if (e.IsManager) result.Add(e);
   return result;
 }

In a language that has Closures, in this case Ruby, I'd write this.

 def managers(emps)
   return emps.select {|e| e.isManager}
 end

Statically Typed vs Dynamically Typed Languages

Closures in Ruby

Scheme Closures and Scoping

Closures in Statically Typed Languages[A Challenge, Why?]

Closure like constructs in C

Closures in Java

Java Lexical Scoping for Closures

Return from Closure

Safety

References