CSC/ECE 517 Fall 2012/ch1 1w4 aj
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.