CSC/ECE 517 Fall 2012/ch1 1w4 aj: Difference between revisions
(Created page with "Test") |
No edit summary |
||
Line 1: | Line 1: | ||
== 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: | |||
<pre> | |||
{ puts "Hello World!" } | |||
</pre> | |||
or | |||
<pre> | |||
do | |||
3.times(puts "Hello") | |||
object1.call | |||
end | |||
</pre> | |||
We can further segment generic algorithms down into the following examples: | |||
* [[ #algos | basic algorithm syntax ]] | |||
* [[ #curry | specialization or parameter currying ]] | |||
* [[ #parallel | parallel programming (e.g. the map-reduce problem) ]] | |||
==== <div id="algos">Basic Algorithms</div> ==== | |||
==== <div id="curry">Specialization / Currying Parameters</div> ==== | |||
[http://en.wikipedia.org/wiki/Currying Currying] | |||
==== <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:46, 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
We can further segment generic algorithms down into the following examples:
- basic algorithm syntax
- specialization or parameter currying
- parallel programming (e.g. the map-reduce problem)
Basic Algorithms
Specialization / Currying Parameters
Parallel Programming - Map-Reduce
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>
Callbacks
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 behavior Injection
Generic Exception Handling
Resource Management / Policy Enforcement
Maintaining Resources
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>
Resource Policy
Deferred execution
Continuation Passing Style
<ref>Miller, Jeremy. "Functional Programming for Everyday .NET Development", MSDN Magazine. http://msdn.microsoft.com/en-us/magazine/ee309512.aspx</ref>.
Declarative syntax leads to less code
Topical References
<references/>
Further Reading
Martin Fowler - Closure : provides a basic definition of Closures and gives a few very basic examples.
Neal Gafter - A Definition of Closures : gives a high-level, historical definition of Closures.
Jim Weirich - Top Ten Reasons I Like Ruby - Blocks and Closures : provides several examples of how Closures can be useful in Ruby.
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.
C# in Depth - The Beauty of Closures : demonstrates Closures in C#.
Wikipedia - Closures : everything there is to know about Closures on one page.
ynniv - Closures in Python : examples implementing Closures in Python.
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.
Reg Braithwaite - Closures and Higher-Order Functions : gives Ruby examples of specialized Closures.
Javascript Closures : demonstrates Javascript Closures in callbacks.