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

From Expertiza_Wiki
Jump to navigation Jump to search
Line 17: Line 17:
==Why do we need closures and what are its uses?==
==Why do we need closures and what are its uses?==


 
DRY(Don't Repeat Yourself) is a popular software development principle, formulated by Andy Hunt and Dave Thomas, which stresses the importance of not duplicating code. Closures help in implementing the DRY principle and make the code easy to maintain.Closures increase considerably the level of a language by mixing access to local variables with remote execution of a set of locally-defined statements.
==

Revision as of 02:46, 8 September 2011

Closures for Statically Typed Languages

Introduction

This wiki gives an introduction to language constructs called closures, it’s usage and discusses about challenges involved in implementing them in statically typed languages.

Closures

A closure is a kind of routine that can be assigned to a variable or passed as a parameter to another routine. It can access the local state (local variables, parameters, methods, and instance variables) which is visible in the place it was defined.

To rephrase, a closure is a block of code which meets the following three criteria

1. It can be passed around as a value.

2. It can executed on demand by anyone who has that value, at which time.

3. It can refer to variables from the context in which it was created.

Why do we need closures and what are its uses?

DRY(Don't Repeat Yourself) is a popular software development principle, formulated by Andy Hunt and Dave Thomas, which stresses the importance of not duplicating code. Closures help in implementing the DRY principle and make the code easy to maintain.Closures increase considerably the level of a language by mixing access to local variables with remote execution of a set of locally-defined statements.