CSC/ECE 517 Fall 2011/ch1 1c cm: Difference between revisions
m (Re-align with topic in e-mail) |
|||
Line 1: | Line 1: | ||
== Introduction == | == Introduction == | ||
Previous 517 classes have written about closures as implemented in several o-o languages, but this topic asks you to identify practical advantages of using closures over using methods. Give examples of programming problems that can be solved more concisely, more extensibly, or more elegantly using closures than using methods. Make sure the examples are organized into a coherent progression. | |||
high-level overview, who we are trying to target | high-level overview, who we are trying to target | ||
Line 13: | Line 15: | ||
graphical breakdown of the two and the features we think are important for each | graphical breakdown of the two and the features we think are important for each | ||
== Closures == | === Definitions === | ||
==== Closures ==== | |||
A closure is a block of executable code together with a reference to the local variables from the environment in which it was created. The local variables are captured by the closure, and their lifetime is extended throughout the lifetime of the closure. Closures can be created by defining a function within the body of another function as the following example illustrates. Note that the scope of the parameter <code>n</code> is limited to the inside of the <code>addGen</code> function. However, because <code>addGen</code> returns a closure, that closure has access to <code>n</code> whenever it is evoked. | A closure is a block of executable code together with a reference to the local variables from the environment in which it was created. The local variables are captured by the closure, and their lifetime is extended throughout the lifetime of the closure. Closures can be created by defining a function within the body of another function as the following example illustrates. Note that the scope of the parameter <code>n</code> is limited to the inside of the <code>addGen</code> function. However, because <code>addGen</code> returns a closure, that closure has access to <code>n</code> whenever it is evoked. | ||
Line 62: | Line 66: | ||
</pre></code> | </pre></code> | ||
=== First-class functions === | ===== First-class functions ===== | ||
Closures are closely related to first-class functions, because typically a closure is a first-class function. A first-class function is a function that can appear anywhere in a program that other first-class values (such as a number or a string) can appear. Namely, a first-class function can be assigned to a variable, passed as an argument to another function, and returned as the return value from a function. Closures are first-class functions that capture the local variables (or free variables) from the site of their definition. | Closures are closely related to first-class functions, because typically a closure is a first-class function. A first-class function is a function that can appear anywhere in a program that other first-class values (such as a number or a string) can appear. Namely, a first-class function can be assigned to a variable, passed as an argument to another function, and returned as the return value from a function. Closures are first-class functions that capture the local variables (or free variables) from the site of their definition. | ||
=== Execution-time environment contains free variables === | ===== Execution-time environment contains free variables ===== | ||
closures capture variables referenced at the time they are created and the lifetime of those captured variables is extended to at least as long as the lifetime of the closure | closures capture variables referenced at the time they are created and the lifetime of those captured variables is extended to at least as long as the lifetime of the closure | ||
Line 119: | Line 123: | ||
</code> | </code> | ||
== Methods == | ==== Methods ==== | ||
Object-oriented programming dates back to at least the middle 1960's. One of the fundamental principals of objects is the notion of encapsulation. One of the earliest descriptions of the methodology, dates back to a 1966 paper describing the SIMULA, a language designed to ease simulation of large systems. The SIMULA language used classes to encapsulate coupled procedures and data into an object, “an object is a self-contained program (block instance) having its own local data and actions defined by a 'class declaration'” [pg 6, Simula]. This 50 year-old concept of fields and procedures is central to the design of today's object-oriented languages such as Smalltalk, Java and C++. The method will be defined using the following criteria: | Object-oriented programming dates back to at least the middle 1960's. One of the fundamental principals of objects is the notion of encapsulation. One of the earliest descriptions of the methodology, dates back to a 1966 paper describing the SIMULA, a language designed to ease simulation of large systems. The SIMULA language used classes to encapsulate coupled procedures and data into an object, “an object is a self-contained program (block instance) having its own local data and actions defined by a 'class declaration'” [pg 6, Simula]. This 50 year-old concept of fields and procedures is central to the design of today's object-oriented languages such as Smalltalk, Java and C++. The method will be defined using the following criteria: | ||
Line 130: | Line 132: | ||
#an ability to access methods across objects (data sharing) | #an ability to access methods across objects (data sharing) | ||
=== Instance or static class functions === | ===== Instance or static class functions ===== | ||
Our definition of a method requires functions which can be bound to a first-class object. All object-oriented languages provide this construct. We can create objects and pass those objects around to be executed by other objects or in the global environment. | Our definition of a method requires functions which can be bound to a first-class object. All object-oriented languages provide this construct. We can create objects and pass those objects around to be executed by other objects or in the global environment. | ||
===== Execution-time environment contains instance or static variables ===== | |||
=== Execution-time environment contains instance or static variables === | |||
The second point of the method definition is that the we can bind static or instance data to our first-class data object. All object-oriented language also provide this construct. | The second point of the method definition is that the we can bind static or instance data to our first-class data object. All object-oriented language also provide this construct. | ||
== Object-oriented vs. Functional Programming - Benefits of Closures == | |||
“Object orientation enhances reusability because it promotes reuse of code at the class level rather than at the level of the individual subroutine.” [pg 66, Fun] | |||
Where the benefits of methods have been demonstrated in object-oriented systems, closures are finding their way into languages which haven't had them in the past, like C++ and Java. The C++ proposal for extending the language lists some of the benefits of supporting closures in the traditionally OO language, including notational simplicity, code sharing of common algorithms, and generality [Lamba]. | |||
=== Variable Scoping === | |||
Let the compiler do the work | |||
=== Functional Generality === | |||
=== | === Callbacks === | ||
=== Map Reduce - Parallelism === | |||
== References == | == Topical References == | ||
{{reflist}} | {{reflist}} | ||
== External Links == | == External Links == |
Revision as of 10:56, 5 September 2011
Introduction
Previous 517 classes have written about closures as implemented in several o-o languages, but this topic asks you to identify practical advantages of using closures over using methods. Give examples of programming problems that can be solved more concisely, more extensibly, or more elegantly using closures than using methods. Make sure the examples are organized into a coherent progression.
high-level overview, who we are trying to target
Closures vs. Methods
introduction of the battle of functional versus object oriented history of the closures with scheme and future languages history of OO methods with C++ and other languages
Summary Table
graphical breakdown of the two and the features we think are important for each
Definitions
Closures
A closure is a block of executable code together with a reference to the local variables from the environment in which it was created. The local variables are captured by the closure, and their lifetime is extended throughout the lifetime of the closure. Closures can be created by defining a function within the body of another function as the following example illustrates. Note that the scope of the parameter n
is limited to the inside of the addGen
function. However, because addGen
returns a closure, that closure has access to n
whenever it is evoked.
An example of a function returning a closure in JavaScript.
function addGen(n) {
return function (x) {
return x + n;
};
}
var add5 = addGen(5);
var add10 = addGen(10);
alert(add5(6)); // 11
alert(add10(6)); // 16
Here is the same example in C#.
public Func<int, int> addGen(int n)
{
return x => x + n;
}
var add5 = addGen(5);
var add10 = addGen(10);
Console.WriteLine(add5(6)); // 11
Console.WriteLine(add10(6)); // 16
And again in Ruby
def addGen(n)
Proc.new { |x| x + n }
end
add5 = addGen(5)
add10 = addGen(10)
puts add5(6) # 11
puts add10(6) # 16
First-class functions
Closures are closely related to first-class functions, because typically a closure is a first-class function. A first-class function is a function that can appear anywhere in a program that other first-class values (such as a number or a string) can appear. Namely, a first-class function can be assigned to a variable, passed as an argument to another function, and returned as the return value from a function. Closures are first-class functions that capture the local variables (or free variables) from the site of their definition.
Execution-time environment contains free variables
closures capture variables referenced at the time they are created and the lifetime of those captured variables is extended to at least as long as the lifetime of the closure
function multipleClosures() {
var i = 0;
return [
function () { return ++i; },
function () { return --i; }
];
}
var closures = multipleClosures();
alert(closures[0]()); // 1
alert(closures[0]()); // 2
alert(closures[1]()); // 1
public Func<int>[] multipleClosures()
{
int i = 0;
return new Func<int>[] { () => ++i, () => --i };
}
var closures = multipleClosures();
Console.WriteLine(closures[0]()); // 1
Console.WriteLine(closures[0]()); // 2
Console.WriteLine(closures[1]()); // 1
def multipleClosures
i = 0
[ Proc.new { i = i + 1 }, Proc.new { i = i - 1 } ]
end
closures = multipleClosures
puts closures[0].call # 1
puts closures[0].call # 2
puts closures[1].call # 1
Methods
Object-oriented programming dates back to at least the middle 1960's. One of the fundamental principals of objects is the notion of encapsulation. One of the earliest descriptions of the methodology, dates back to a 1966 paper describing the SIMULA, a language designed to ease simulation of large systems. The SIMULA language used classes to encapsulate coupled procedures and data into an object, “an object is a self-contained program (block instance) having its own local data and actions defined by a 'class declaration'” [pg 6, Simula]. This 50 year-old concept of fields and procedures is central to the design of today's object-oriented languages such as Smalltalk, Java and C++. The method will be defined using the following criteria:
- An object as first-class data which allows binding of
- finite set of static or instance data (commonly referred to as fields) created by the programmer which hold the state of the object
- procedures or actions modifying the data to change the state of the object
- an ability to access methods across objects (data sharing)
Instance or static class functions
Our definition of a method requires functions which can be bound to a first-class object. All object-oriented languages provide this construct. We can create objects and pass those objects around to be executed by other objects or in the global environment.
Execution-time environment contains instance or static variables
The second point of the method definition is that the we can bind static or instance data to our first-class data object. All object-oriented language also provide this construct.
Object-oriented vs. Functional Programming - Benefits of Closures
“Object orientation enhances reusability because it promotes reuse of code at the class level rather than at the level of the individual subroutine.” [pg 66, Fun]
Where the benefits of methods have been demonstrated in object-oriented systems, closures are finding their way into languages which haven't had them in the past, like C++ and Java. The C++ proposal for extending the language lists some of the benefits of supporting closures in the traditionally OO language, including notational simplicity, code sharing of common algorithms, and generality [Lamba].
Variable Scoping
Let the compiler do the work