CSC/ECE 517 Fall 2011/ch1 1d sr: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 3: Line 3:
==Introduction==
==Introduction==
==Closures==
==Closures==
===What are Closures===
===What are Closures===
Let us try to define what a closure as a programming construct means.
A Closure is basically a function or a method (or a block, in the context of ruby) that has the following two properties -
• One can pass it around like an object or as a value parameter to other functions/methods/blocks
• It takes the snapshot and effectively "remembers" the values of all the variables that were in scope when the function was created
    and it is because of this property that it is able to access those variables when it is called even though they may no longer in scope. [http://www.skorks.com/2010/05/closures-a-simple-explanation-using-ruby/]
===Why Closures (Motivation, Uses, Advantages)===
===Why Closures (Motivation, Uses, Advantages)===
===Closures in Dynamically Typed Languages===
===Closures in Dynamically Typed Languages===
Line 10: Line 20:
=====Example in JS=====
=====Example in JS=====
=====Example in Python=====
=====Example in Python=====
==Closures in Statically Typed Languages==
==Closures in Statically Typed Languages==
===The Problem===
===The Problem===

Revision as of 15:22, 2 September 2011

Wiki Chapter: CSC/ECE 517 Fall 2011/ch1 1d sr

Introduction

Closures

What are Closures

Let us try to define what a closure as a programming construct means.

A Closure is basically a function or a method (or a block, in the context of ruby) that has the following two properties -

• One can pass it around like an object or as a value parameter to other functions/methods/blocks

• It takes the snapshot and effectively "remembers" the values of all the variables that were in scope when the function was created

    and it is because of this property that it is able to access those variables when it is called even though they may no longer in scope. [1]

Why Closures (Motivation, Uses, Advantages)

Closures in Dynamically Typed Languages

Example use of Closures in Dynamically Typed Langauges

Example in Ruby
Example in JS
Example in Python

Closures in Statically Typed Languages

The Problem

Limitations of Statically Typed Languages

Lexical Scope

Functions not as first class citizens of the language

Implementing Closures in Statically Typed Languages

C : function pointers

C++ : function objects

Java : anonymous inner classes

Closures and Static Scoping

(Explanation)

Case study of Scheme

References

1. Matz discussion on Closures

2. Closures in C++

3. Closures in C

4. Closures in Java

External Links

http://www.artima.com/intv/closures.html

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1968.pdf

http://jens.mooseyard.com/2008/08/blocksclosures-for-c/

http://tronicek.blogspot.com/2007/12/closures-closure-is-form-of-anonymous_28.html