CSC/ECE 517 Fall 2011/ch1 1c cm

From Expertiza_Wiki
Jump to navigation Jump to search

Introduction

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

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

Methods

methods and classes are associated with object-oriented programming

Instance or static class functions

methods are functions associated with an object

Execution-time environment contains instance or static variables

methods operate on state associated with an object (environment is closed, but defined by the programmer at compile time)

Object-oriented vs. Functional Programming

Benefits of Closures

Benefits of Methods

References

External Links