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


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:


We can further segment generic algorithms down into the following examples:
<pre>
random_method("John") { puts "How you doing? " }
</pre>


* [[ #algos | basic algorithm syntax ]]
* [[ #curry | specialization or parameter currying ]]
* [[ #parallel | parallel programming (e.g. the map-reduce problem) ]]


==== <div id="algos">Basic Algorithms</div> ====
====What is a Closure?====
Now, a block can use local variables from the surrounding scope. Such blocks are called <b>Closures</b>. Let us look at a simple example:


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


==== <div id="curry">Specialization / Currying Parameters</div> ====
  p1 = closurefunc(3)
  p1.call(1)    # => 4
  p2 = closurefunc(8)
  p2.call(5)  # => 13
</pre>


[http://en.wikipedia.org/wiki/Currying Currying]
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>.
 
==== <div id="parallel">Parallel Programming - Map-Reduce</div> ====
 
The following Ruby thread example<ref>Flanagan, David and Yukihiro Matsumoto, The Ruby Programming Language. Sebastopol: O'Reilly Media, Inc., 2008. http://oreilly.com/catalog/9780596516178, pg 382</ref>
 
=== <div id="callbacks">Callbacks</div> ===
 
[http://en.wikipedia.org/wiki/Callback_(computer_programming) Callbacks] are often cited as the most desirable application of Closures<ref>"Learning more about Closures", StackOverflow.com. http://stackoverflow.com/questions/212401/javascript-how-do-i-learn-about-closures-usage</ref><ref>Morrison, Joe. "A Brief Introduction to Four Programming Language Concepts". http://joemorrison.org/projects/four-concepts/</ref>  
 
Below are two important callback forms that can demonstrate this capability in common use.
 
* [[ #gui_handlers | GUI behavior injection ]]
* [[ #exceptions | generic exception handling ]]
 
==== <div id="gui_handlers">GUI behavior Injection</div> ====
 
 
==== <div id="exceptions">Generic Exception Handling</div> ====
 
=== <div id="management">Resource Management / Policy Enforcement</div> ===
 
==== <div id="resources">Maintaining Resources</div> ====
 
This often cited example shows the encapsulation of file base policy using a C++ and a Ruby Closure.<ref>Wikipedia Resource Acquisition is Initialization: http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization</ref>
 
==== <div id="policy">Resource Policy</div> ====
 
 
=== <div id="deferred">Deferred execution</div> ===
 
 
=== <div id="continuations">Continuation Passing Style</div> ===
 
<ref>Miller, Jeremy. "Functional Programming for Everyday .NET Development", MSDN Magazine. http://msdn.microsoft.com/en-us/magazine/ee309512.aspx</ref>.
 
=== <div id="syntax">Declarative syntax leads to less code</div> ===
 
== Topical References ==
 
<references/>
 
== Further Reading ==
 
[http://martinfowler.com/bliki/Closure.html Martin Fowler - Closure] : provides a basic definition of Closures and gives a few very basic examples.
 
[http://gafter.blogspot.com/2007/01/definition-of-closures.html Neal Gafter - A Definition of Closures] : gives a high-level, historical definition of Closures.
 
[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://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.
 
[http://csharpindepth.com/Articles/Chapter5/Closures.aspx C# in Depth - The Beauty of Closures] : demonstrates Closures in C#.
 
[http://en.wikipedia.org/wiki/Closure_(computer_science) Wikipedia - Closures] : everything there is to know about Closures on one page.
 
[http://ynniv.com/blog/2007/08/closures-in-python.html ynniv - Closures in Python] : examples implementing Closures in Python.
 
[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://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://jibbering.com/faq/notes/closures Javascript Closures] : demonstrates Javascript Closures in callbacks.

Revision as of 20:50, 14 September 2012

Introduction

Closures

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? " }


What is a Closure?

Now, a block 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.