<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Tmakhij</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Tmakhij"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Tmakhij"/>
	<updated>2026-09-30T09:48:57Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_at&amp;diff=8444</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 at</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_at&amp;diff=8444"/>
		<updated>2007-11-14T03:49:13Z</updated>

		<summary type="html">&lt;p&gt;Tmakhij: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Programming by contract==&lt;br /&gt;
&lt;br /&gt;
Programming by Contract is a way of specifying the behavior of a function, and the name arises from its formal similarity to a legal contract. The preconditions define the conditions whose truth the caller guarantees to ensure before calling the function, and the postconditions define the conditions whose truth the function guarantees to establish by virtue of its execution. One of the purposes in this is to avoid redundant validity checks at each level in a stack of called functions. &lt;br /&gt;
&lt;br /&gt;
=== Example 1 ===&lt;br /&gt;
Consider the following example for counting the number of vowles [1]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int is_vowelpair (const char *p)  &lt;br /&gt;
{&lt;br /&gt;
    return is_vowel(*p) &amp;amp;&amp;amp; is_vowel (*(p+1));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int count_vowelpairs (char *s)&lt;br /&gt;
{ &lt;br /&gt;
    int sum = 0;&lt;br /&gt;
    &lt;br /&gt;
    for (; *s != '\0'; s++)&lt;br /&gt;
        if (is_vowelpair(s))&lt;br /&gt;
            sum++;&lt;br /&gt;
    return sum;&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The functions count_vowelpairs receive as input argument a pointer to a C string. Neither function applies any validity check to the pointer, so there is an implicit precondition that the caller supply a valid pointer. To follow the rules of programming by contract we should make this an explicit precondition, albeit one that is so common and obvious that it hardly seems to need stating. &lt;br /&gt;
&lt;br /&gt;
There are four possibilities for the pointer passed in as argument: &lt;br /&gt;
&lt;br /&gt;
It contains a valid address that is indeed the address of a valid C string (including the null string). &lt;br /&gt;
It contains a valid address but the memory at that address does not comprise a valid C string. &lt;br /&gt;
It contains a bit pattern that is not a valid address (e.g., it is outside the addressing range, or the memory is not readable). &lt;br /&gt;
It contains the null pointer. &lt;br /&gt;
Our contract imposes the precondition that the pointer must be valid as specified in the first case above. There is no reasonable way to detect the second case, and for the third case it is usual to delegate detection (and handling) to the exception mechanism of the operating system11. &lt;br /&gt;
&lt;br /&gt;
The fourth case is the interesting one. Null is a valid value for a pointer, but it is (by definition) an invalid address that will generally cause an addressing exception if dereferenced. It is, however, trivial for the function to detect that a null pointer argument has been passed. Therefore, we can propose a general rule that makes life easier for callers of a function: If a pointer to a null object is a valid argument, a null pointer should also be a valid argument with the same meaning. In the specific examples we are studying here, we should change the contract by weakening the precondition to allow the first and fourth cases, and implement the function to immediately return the value 0 if a null pointer is passed as the argument. The purpose of this rule is to relieve callers of the need to make the test for a null pointer; for a language like C the gain is not obvious, but for functional style languages like Lisp the gain is significant. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Example 2 ===&lt;br /&gt;
Consider the following sort function in python [3]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This tells us everything we need to know about sort. During debugging, these statements are actually executed. If any of the pre-conditions are false, an exception is raised so the caller can be debugged. If any of the post-conditions are false, a similar exception is raised so the sort function can be debugged.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A programming language called Eiffel was created to facilitate Design by Contract. Eiffel provides built-in features to support the implementation of Design by Contract. The example below illustrates those features: [4]&lt;br /&gt;
&lt;br /&gt;
=== Example 3 === &lt;br /&gt;
&lt;br /&gt;
The following example shows a partial implementation of a bounded queue, with methods put and remove. The pre- and post-conditions for those methods are coded explicitly with the &amp;quot;require&amp;quot; and &amp;quot;ensure&amp;quot; features of Eiffel.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                class BoundedQueue[G] feature&lt;br /&gt;
                    put(x:G) is&lt;br /&gt;
                       -- add x as newest element&lt;br /&gt;
                       require&lt;br /&gt;
                           not full&lt;br /&gt;
                       do&lt;br /&gt;
                           -- implementation of put&lt;br /&gt;
                           . . .&lt;br /&gt;
                       ensure&lt;br /&gt;
                           not empty&lt;br /&gt;
                       end;&lt;br /&gt;
                    remove is&lt;br /&gt;
                       -- remove oldest element&lt;br /&gt;
                       require&lt;br /&gt;
                           not empty&lt;br /&gt;
                       do&lt;br /&gt;
                           -- implementation of remove&lt;br /&gt;
                           . . .&lt;br /&gt;
                       ensure&lt;br /&gt;
                           not full&lt;br /&gt;
                       end;&lt;br /&gt;
                           empty: BOOLEAN is&lt;br /&gt;
                               -- is the queue empty?&lt;br /&gt;
                               do Result:=. . . end;&lt;br /&gt;
                           full: BOOLEAN is&lt;br /&gt;
                               -- is the queue full?&lt;br /&gt;
                               do Result:=. . . end;&lt;br /&gt;
                       end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The assertions are checked at run-time. This checking can be turned on or off as a result of a compilation switch.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Programming by contract places responsibility squarely on the shoulders of the caller for ensuring that specified preconditions for a function are met, and relieves the function of any responsibility for verifying that preconditions are met. As intended, this has the desirable effect of eliminating redundant validity checking. In real life, however, it also has an undesirable side effect: If the caller makes a mistake and does not in fact meet the preconditions, the function is likely to fail, and the cause of the failure may be difficult to track down. A pointer that is null or contains an invalid address is usually easy to diagnose; since an exception will be caused immediately an attempt is made to deference it, but other errors may cause failures far removed from the root cause.&lt;br /&gt;
&lt;br /&gt;
=== Example 4 === &lt;br /&gt;
The following is an example of how Programming by Contract can be implemented in C++:&lt;br /&gt;
&lt;br /&gt;
The first precondition could be called a default precondition. It should be possible to remove such preconditions from object code. However, the second and the third precondition must always be part of the object code. &lt;br /&gt;
&lt;br /&gt;
 int not_ok( int&amp;amp; );&lt;br /&gt;
 int ok( int );&lt;br /&gt;
 struct Foo { int foo(); int bar() const; };&lt;br /&gt;
 Foo f;&lt;br /&gt;
 Foo* f_ptr;&lt;br /&gt;
 ...&lt;br /&gt;
 void foo( int i )&lt;br /&gt;
 in&lt;br /&gt;
 {&lt;br /&gt;
 not_ok( i ); // error: ’not_ok()’ takes a reference argument&lt;br /&gt;
 ok( i ); // ok: ’ok()’ takes a value argument&lt;br /&gt;
 f.foo(); // error: cannot call a non-const member&lt;br /&gt;
 f_ptr-&amp;gt;foo(); // error: not even through a pointer&lt;br /&gt;
 f_ptr-&amp;gt;bar(); // ok: bar is a const member function&lt;br /&gt;
 f_ptr; // ok: conversion to bool&lt;br /&gt;
 &amp;quot;a comment&amp;quot;; // ok: conversion to bool&lt;br /&gt;
 if( ... ); // error: statements not allowed&lt;br /&gt;
 i = 2; // error: assignment not possible&lt;br /&gt;
 i &amp;gt; 0; return FAILURE_CODE; // error: ’return’ not allowed&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Postconditions are much like preconditions: (1) they are optional, (2) can include throw clauses (which are never compiled away), and (3) has the same rules regarding const-correctness. Note that postconditions are only checked when the function exits normally.&lt;br /&gt;
&lt;br /&gt;
 int foo( int&amp;amp; i )&lt;br /&gt;
 out&lt;br /&gt;
 {&lt;br /&gt;
 i == in i + 1; // keep track of changes to ’i’&lt;br /&gt;
 return == 5: terminate(); // call ’terminate()’ on failure&lt;br /&gt;
 }&lt;br /&gt;
 do&lt;br /&gt;
 {&lt;br /&gt;
 ++i;&lt;br /&gt;
 if( i % 2 == 0 )&lt;br /&gt;
 return 5;&lt;br /&gt;
 else&lt;br /&gt;
 return 4;&lt;br /&gt;
 }&lt;br /&gt;
== See Also ==&lt;br /&gt;
http://en.wikibooks.org/wiki/Computer_programming/Design_by_Contract&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.eventhelix.com/RealtimeMantra/Object_Oriented/design_by_contract.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.cs.uno.edu/~c1581/Labs2006/lab7/lab7.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.phpunit.de/pocket_guide/3.2/en/test-first-programming.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.python.org/dev/peps/pep-0316/&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.artima.com/cppsource/deepspace2.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.csc.calpoly.edu/~dstearns/SeniorProjectsWWW/Rideg/dbc.html&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
[1] http://www.ibm.com/developerworks/rational/library/455.html#N10324 &amp;lt;br&amp;gt;&lt;br /&gt;
[2] http://archive.eiffel.com/doc/manuals/technology/contract/page.html &amp;lt;br&amp;gt;&lt;br /&gt;
[3] http://www.wayforward.net/pycontract/ &amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://www.patentstorm.us/patents/6442750-description.html&amp;lt;br&amp;gt;&lt;br /&gt;
[5] http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2004/n1613.pdf&lt;/div&gt;</summary>
		<author><name>Tmakhij</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_at&amp;diff=8443</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 at</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_at&amp;diff=8443"/>
		<updated>2007-11-14T03:48:30Z</updated>

		<summary type="html">&lt;p&gt;Tmakhij: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Programming by contract==&lt;br /&gt;
&lt;br /&gt;
Programming by Contract is a way of specifying the behavior of a function, and the name arises from its formal similarity to a legal contract. The preconditions define the conditions whose truth the caller guarantees to ensure before calling the function, and the postconditions define the conditions whose truth the function guarantees to establish by virtue of its execution. One of the purposes in this is to avoid redundant validity checks at each level in a stack of called functions. &lt;br /&gt;
&lt;br /&gt;
=== Example 1 ===&lt;br /&gt;
Consider the following example for counting the number of vowles [1]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int is_vowelpair (const char *p)  &lt;br /&gt;
{&lt;br /&gt;
    return is_vowel(*p) &amp;amp;&amp;amp; is_vowel (*(p+1));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int count_vowelpairs (char *s)&lt;br /&gt;
{ &lt;br /&gt;
    int sum = 0;&lt;br /&gt;
    &lt;br /&gt;
    for (; *s != '\0'; s++)&lt;br /&gt;
        if (is_vowelpair(s))&lt;br /&gt;
            sum++;&lt;br /&gt;
    return sum;&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The functions count_vowelpairs receive as input argument a pointer to a C string. Neither function applies any validity check to the pointer, so there is an implicit precondition that the caller supply a valid pointer. To follow the rules of programming by contract we should make this an explicit precondition, albeit one that is so common and obvious that it hardly seems to need stating. &lt;br /&gt;
&lt;br /&gt;
There are four possibilities for the pointer passed in as argument: &lt;br /&gt;
&lt;br /&gt;
It contains a valid address that is indeed the address of a valid C string (including the null string). &lt;br /&gt;
It contains a valid address but the memory at that address does not comprise a valid C string. &lt;br /&gt;
It contains a bit pattern that is not a valid address (e.g., it is outside the addressing range, or the memory is not readable). &lt;br /&gt;
It contains the null pointer. &lt;br /&gt;
Our contract imposes the precondition that the pointer must be valid as specified in the first case above. There is no reasonable way to detect the second case, and for the third case it is usual to delegate detection (and handling) to the exception mechanism of the operating system11. &lt;br /&gt;
&lt;br /&gt;
The fourth case is the interesting one. Null is a valid value for a pointer, but it is (by definition) an invalid address that will generally cause an addressing exception if dereferenced. It is, however, trivial for the function to detect that a null pointer argument has been passed. Therefore, we can propose a general rule that makes life easier for callers of a function: If a pointer to a null object is a valid argument, a null pointer should also be a valid argument with the same meaning. In the specific examples we are studying here, we should change the contract by weakening the precondition to allow the first and fourth cases, and implement the function to immediately return the value 0 if a null pointer is passed as the argument. The purpose of this rule is to relieve callers of the need to make the test for a null pointer; for a language like C the gain is not obvious, but for functional style languages like Lisp the gain is significant. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Example 2 ===&lt;br /&gt;
Consider the following sort function in python [3]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This tells us everything we need to know about sort. During debugging, these statements are actually executed. If any of the pre-conditions are false, an exception is raised so the caller can be debugged. If any of the post-conditions are false, a similar exception is raised so the sort function can be debugged.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A programming language called Eiffel was created to facilitate Design by Contract. Eiffel provides built-in features to support the implementation of Design by Contract. The example below illustrates those features: [4]&lt;br /&gt;
&lt;br /&gt;
=== Example 3 === &lt;br /&gt;
&lt;br /&gt;
The following example shows a partial implementation of a bounded queue, with methods put and remove. The pre- and post-conditions for those methods are coded explicitly with the &amp;quot;require&amp;quot; and &amp;quot;ensure&amp;quot; features of Eiffel.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                class BoundedQueue[G] feature&lt;br /&gt;
                    put(x:G) is&lt;br /&gt;
                       -- add x as newest element&lt;br /&gt;
                       require&lt;br /&gt;
                           not full&lt;br /&gt;
                       do&lt;br /&gt;
                           -- implementation of put&lt;br /&gt;
                           . . .&lt;br /&gt;
                       ensure&lt;br /&gt;
                           not empty&lt;br /&gt;
                       end;&lt;br /&gt;
                    remove is&lt;br /&gt;
                       -- remove oldest element&lt;br /&gt;
                       require&lt;br /&gt;
                           not empty&lt;br /&gt;
                       do&lt;br /&gt;
                           -- implementation of remove&lt;br /&gt;
                           . . .&lt;br /&gt;
                       ensure&lt;br /&gt;
                           not full&lt;br /&gt;
                       end;&lt;br /&gt;
                           empty: BOOLEAN is&lt;br /&gt;
                               -- is the queue empty?&lt;br /&gt;
                               do Result:=. . . end;&lt;br /&gt;
                           full: BOOLEAN is&lt;br /&gt;
                               -- is the queue full?&lt;br /&gt;
                               do Result:=. . . end;&lt;br /&gt;
                       end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The assertions are checked at run-time. This checking can be turned on or off as a result of a compilation switch.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Programming by contract places responsibility squarely on the shoulders of the caller for ensuring that specified preconditions for a function are met, and relieves the function of any responsibility for verifying that preconditions are met. As intended, this has the desirable effect of eliminating redundant validity checking. In real life, however, it also has an undesirable side effect: If the caller makes a mistake and does not in fact meet the preconditions, the function is likely to fail, and the cause of the failure may be difficult to track down. A pointer that is null or contains an invalid address is usually easy to diagnose; since an exception will be caused immediately an attempt is made to deference it, but other errors may cause failures far removed from the root cause.&lt;br /&gt;
&lt;br /&gt;
=== Example 4 === &lt;br /&gt;
The following is an example of how Programming by Contract can be implemented in C++:&lt;br /&gt;
&lt;br /&gt;
The first precondition could be called a default precondition. It should be possible to remove such preconditions from object code. However, the second and the third precondition must always be part of the object code. &lt;br /&gt;
&lt;br /&gt;
 int not_ok( int&amp;amp; );&lt;br /&gt;
 int ok( int );&lt;br /&gt;
 struct Foo { int foo(); int bar() const; };&lt;br /&gt;
 Foo f;&lt;br /&gt;
 Foo* f_ptr;&lt;br /&gt;
 ...&lt;br /&gt;
 void foo( int i )&lt;br /&gt;
 in&lt;br /&gt;
 {&lt;br /&gt;
 not_ok( i ); // error: ’not_ok()’ takes a reference argument&lt;br /&gt;
 ok( i ); // ok: ’ok()’ takes a value argument&lt;br /&gt;
 f.foo(); // error: cannot call a non-const member&lt;br /&gt;
 f_ptr-&amp;gt;foo(); // error: not even through a pointer&lt;br /&gt;
 f_ptr-&amp;gt;bar(); // ok: bar is a const member function&lt;br /&gt;
 f_ptr; // ok: conversion to bool&lt;br /&gt;
 &amp;quot;a comment&amp;quot;; // ok: conversion to bool&lt;br /&gt;
 if( ... ); // error: statements not allowed&lt;br /&gt;
 i = 2; // error: assignment not possible&lt;br /&gt;
 i &amp;gt; 0; return FAILURE_CODE; // error: ’return’ not allowed&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
Postconditions are much like preconditions: (1) they are optional, (2) can include throw clauses (which are never compiled away), and (3) has the same rules regarding const-correctness. Note that postconditions are only checked when the function exits normally.&lt;br /&gt;
&lt;br /&gt;
 int foo( int&amp;amp; i )&lt;br /&gt;
 out&lt;br /&gt;
 {&lt;br /&gt;
 i == in i + 1; // keep track of changes to ’i’&lt;br /&gt;
 return == 5: terminate(); // call ’terminate()’ on failure&lt;br /&gt;
 }&lt;br /&gt;
 do&lt;br /&gt;
 {&lt;br /&gt;
 ++i;&lt;br /&gt;
 if( i % 2 == 0 )&lt;br /&gt;
 return 5;&lt;br /&gt;
 else&lt;br /&gt;
 return 4;&lt;br /&gt;
 }&lt;br /&gt;
== See Also ==&lt;br /&gt;
http://en.wikibooks.org/wiki/Computer_programming/Design_by_Contract&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.eventhelix.com/RealtimeMantra/Object_Oriented/design_by_contract.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.cs.uno.edu/~c1581/Labs2006/lab7/lab7.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.phpunit.de/pocket_guide/3.2/en/test-first-programming.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.python.org/dev/peps/pep-0316/&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.artima.com/cppsource/deepspace2.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.csc.calpoly.edu/~dstearns/SeniorProjectsWWW/Rideg/dbc.html&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
[1] http://www.ibm.com/developerworks/rational/library/455.html#N10324 &amp;lt;br&amp;gt;&lt;br /&gt;
[2] http://archive.eiffel.com/doc/manuals/technology/contract/page.html &amp;lt;br&amp;gt;&lt;br /&gt;
[3] http://www.wayforward.net/pycontract/ &amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://www.patentstorm.us/patents/6442750-description.html&lt;/div&gt;</summary>
		<author><name>Tmakhij</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_at&amp;diff=8421</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 at</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_at&amp;diff=8421"/>
		<updated>2007-11-14T01:00:47Z</updated>

		<summary type="html">&lt;p&gt;Tmakhij: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Programming by contract==&lt;br /&gt;
&lt;br /&gt;
Programming by Contract is a way of specifying the behavior of a function, and the name arises from its formal similarity to a legal contract. The preconditions define the conditions whose truth the caller guarantees to ensure before calling the function, and the postconditions define the conditions whose truth the function guarantees to establish by virtue of its execution. One of the purposes in this is to avoid redundant validity checks at each level in a stack of called functions. &lt;br /&gt;
&lt;br /&gt;
=== Example 1 ===&lt;br /&gt;
Consider the following example for counting the number of vowles [1]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int is_vowelpair (const char *p)  &lt;br /&gt;
{&lt;br /&gt;
    return is_vowel(*p) &amp;amp;&amp;amp; is_vowel (*(p+1));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int count_vowelpairs (char *s)&lt;br /&gt;
{ &lt;br /&gt;
    int sum = 0;&lt;br /&gt;
    &lt;br /&gt;
    for (; *s != '\0'; s++)&lt;br /&gt;
        if (is_vowelpair(s))&lt;br /&gt;
            sum++;&lt;br /&gt;
    return sum;&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The functions count_vowelpairs receive as input argument a pointer to a C string. Neither function applies any validity check to the pointer, so there is an implicit precondition that the caller supply a valid pointer. To follow the rules of programming by contract we should make this an explicit precondition, albeit one that is so common and obvious that it hardly seems to need stating. &lt;br /&gt;
&lt;br /&gt;
There are four possibilities for the pointer passed in as argument: &lt;br /&gt;
&lt;br /&gt;
It contains a valid address that is indeed the address of a valid C string (including the null string). &lt;br /&gt;
It contains a valid address but the memory at that address does not comprise a valid C string. &lt;br /&gt;
It contains a bit pattern that is not a valid address (e.g., it is outside the addressing range, or the memory is not readable). &lt;br /&gt;
It contains the null pointer. &lt;br /&gt;
Our contract imposes the precondition that the pointer must be valid as specified in the first case above. There is no reasonable way to detect the second case, and for the third case it is usual to delegate detection (and handling) to the exception mechanism of the operating system11. &lt;br /&gt;
&lt;br /&gt;
The fourth case is the interesting one. Null is a valid value for a pointer, but it is (by definition) an invalid address that will generally cause an addressing exception if dereferenced. It is, however, trivial for the function to detect that a null pointer argument has been passed. Therefore, we can propose a general rule that makes life easier for callers of a function: If a pointer to a null object is a valid argument, a null pointer should also be a valid argument with the same meaning. In the specific examples we are studying here, we should change the contract by weakening the precondition to allow the first and fourth cases, and implement the function to immediately return the value 0 if a null pointer is passed as the argument. The purpose of this rule is to relieve callers of the need to make the test for a null pointer; for a language like C the gain is not obvious, but for functional style languages like Lisp the gain is significant. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Example 2 ===&lt;br /&gt;
Consider the following sort function in python [3]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This tells us everything we need to know about sort. During debugging, these statements are actually executed. If any of the pre-conditions are false, an exception is raised so the caller can be debugged. If any of the post-conditions are false, a similar exception is raised so the sort function can be debugged.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A programming language called Eiffel was created to facilitate Design by Contract. Eiffel provides built-in features to support the implementation of Design by Contract. The example below illustrates those features: [4]&lt;br /&gt;
&lt;br /&gt;
=== Example 3 === &lt;br /&gt;
&lt;br /&gt;
The following example shows a partial implementation of a bounded queue, with methods put and remove. The pre- and post-conditions for those methods are coded explicitly with the &amp;quot;require&amp;quot; and &amp;quot;ensure&amp;quot; features of Eiffel.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                class BoundedQueue[G] feature&lt;br /&gt;
                    put(x:G) is&lt;br /&gt;
                       -- add x as newest element&lt;br /&gt;
                       require&lt;br /&gt;
                           not full&lt;br /&gt;
                       do&lt;br /&gt;
                           -- implementation of put&lt;br /&gt;
                           . . .&lt;br /&gt;
                       ensure&lt;br /&gt;
                           not empty&lt;br /&gt;
                       end;&lt;br /&gt;
                    remove is&lt;br /&gt;
                       -- remove oldest element&lt;br /&gt;
                       require&lt;br /&gt;
                           not empty&lt;br /&gt;
                       do&lt;br /&gt;
                           -- implementation of remove&lt;br /&gt;
                           . . .&lt;br /&gt;
                       ensure&lt;br /&gt;
                           not full&lt;br /&gt;
                       end;&lt;br /&gt;
                           empty: BOOLEAN is&lt;br /&gt;
                               -- is the queue empty?&lt;br /&gt;
                               do Result:=. . . end;&lt;br /&gt;
                           full: BOOLEAN is&lt;br /&gt;
                               -- is the queue full?&lt;br /&gt;
                               do Result:=. . . end;&lt;br /&gt;
                       end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The assertions are checked at run-time. This checking can be turned on or off as a result of a compilation switch.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Programming by contract places responsibility squarely on the shoulders of the caller for ensuring that specified preconditions for a function are met, and relieves the function of any responsibility for verifying that preconditions are met. As intended, this has the desirable effect of eliminating redundant validity checking. In real life, however, it also has an undesirable side effect: If the caller makes a mistake and does not in fact meet the preconditions, the function is likely to fail, and the cause of the failure may be difficult to track down. A pointer that is null or contains an invalid address is usually easy to diagnose; since an exception will be caused immediately an attempt is made to deference it, but other errors may cause failures far removed from the root cause.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
http://en.wikibooks.org/wiki/Computer_programming/Design_by_Contract&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.eventhelix.com/RealtimeMantra/Object_Oriented/design_by_contract.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.cs.uno.edu/~c1581/Labs2006/lab7/lab7.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.phpunit.de/pocket_guide/3.2/en/test-first-programming.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.python.org/dev/peps/pep-0316/&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.artima.com/cppsource/deepspace2.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.csc.calpoly.edu/~dstearns/SeniorProjectsWWW/Rideg/dbc.html&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
[1] http://www.ibm.com/developerworks/rational/library/455.html#N10324 &amp;lt;br&amp;gt;&lt;br /&gt;
[2] http://archive.eiffel.com/doc/manuals/technology/contract/page.html &amp;lt;br&amp;gt;&lt;br /&gt;
[3] http://www.wayforward.net/pycontract/ &amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://www.patentstorm.us/patents/6442750-description.html&lt;/div&gt;</summary>
		<author><name>Tmakhij</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_at&amp;diff=8418</id>
		<title>CSC/ECE 517 Fall 2007/wiki3 2 at</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki3_2_at&amp;diff=8418"/>
		<updated>2007-11-14T00:09:09Z</updated>

		<summary type="html">&lt;p&gt;Tmakhij: /* See Also */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Programming by contract==&lt;br /&gt;
&lt;br /&gt;
Programming by Contract is a way of specifying the behavior of a function, and the name arises from its formal similarity to a legal contract. The preconditions define the conditions whose truth the caller guarantees to ensure before calling the function, and the postconditions define the conditions whose truth the function guarantees to establish by virtue of its execution. One of the purposes in this is to avoid redundant validity checks at each level in a stack of called functions. &lt;br /&gt;
&lt;br /&gt;
=== Example 1 ===&lt;br /&gt;
Consider the following example for counting the number of vowles [1]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
int is_vowelpair (const char *p)  &lt;br /&gt;
{&lt;br /&gt;
    return is_vowel(*p) &amp;amp;&amp;amp; is_vowel (*(p+1));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int count_vowelpairs (char *s)&lt;br /&gt;
{ &lt;br /&gt;
    int sum = 0;&lt;br /&gt;
    &lt;br /&gt;
    for (; *s != '\0'; s++)&lt;br /&gt;
        if (is_vowelpair(s))&lt;br /&gt;
            sum++;&lt;br /&gt;
    return sum;&lt;br /&gt;
} &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The functions count_vowelpairs receive as input argument a pointer to a C string. Neither function applies any validity check to the pointer, so there is an implicit precondition that the caller supply a valid pointer. To follow the rules of programming by contract we should make this an explicit precondition, albeit one that is so common and obvious that it hardly seems to need stating. &lt;br /&gt;
&lt;br /&gt;
There are four possibilities for the pointer passed in as argument: &lt;br /&gt;
&lt;br /&gt;
It contains a valid address that is indeed the address of a valid C string (including the null string). &lt;br /&gt;
It contains a valid address but the memory at that address does not comprise a valid C string. &lt;br /&gt;
It contains a bit pattern that is not a valid address (e.g., it is outside the addressing range, or the memory is not readable). &lt;br /&gt;
It contains the null pointer. &lt;br /&gt;
Our contract imposes the precondition that the pointer must be valid as specified in the first case above. There is no reasonable way to detect the second case, and for the third case it is usual to delegate detection (and handling) to the exception mechanism of the operating system11. &lt;br /&gt;
&lt;br /&gt;
The fourth case is the interesting one. Null is a valid value for a pointer, but it is (by definition) an invalid address that will generally cause an addressing exception if dereferenced. It is, however, trivial for the function to detect that a null pointer argument has been passed. Therefore, we can propose a general rule that makes life easier for callers of a function: If a pointer to a null object is a valid argument, a null pointer should also be a valid argument with the same meaning. In the specific examples we are studying here, we should change the contract by weakening the precondition to allow the first and fourth cases, and implement the function to immediately return the value 0 if a null pointer is passed as the argument. The purpose of this rule is to relieve callers of the need to make the test for a null pointer; for a language like C the gain is not obvious, but for functional style languages like Lisp the gain is significant. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Example 2 ===&lt;br /&gt;
Consider the following sort function in python [3]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
def sort(a):&lt;br /&gt;
    &amp;quot;&amp;quot;&amp;quot;Sort a list *IN PLACE*.&lt;br /&gt;
&lt;br /&gt;
    pre:&lt;br /&gt;
        # must be a list&lt;br /&gt;
        isinstance(a, list)&lt;br /&gt;
&lt;br /&gt;
        # all elements must be comparable with all other items&lt;br /&gt;
        forall(range(len(a)),&lt;br /&gt;
               lambda i: forall(range(len(a)),&lt;br /&gt;
                                lambda j: (a[i] &amp;lt; a[j]) ^ (a[i] &amp;gt;= a[j])))&lt;br /&gt;
&lt;br /&gt;
    post[a]:&lt;br /&gt;
        # length of array is unchanged&lt;br /&gt;
        len(a) == len(__old__.a)&lt;br /&gt;
&lt;br /&gt;
        # all elements given are still in the array&lt;br /&gt;
        forall(__old__.a, lambda e: __old__.a.count(e) == a.count(e))&lt;br /&gt;
&lt;br /&gt;
        # the array is sorted&lt;br /&gt;
        forall([a[i] &amp;gt;= a[i-1] for i in range(1, len(a))])&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This tells us everything we need to know about sort. During debugging, these statements are actually executed. If any of the pre-conditions are false, an exception is raised so the caller can be debugged. If any of the post-conditions are false, a similar exception is raised so the sort function can be debugged.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
A programming language called Eiffel was created to facilitate Design by Contract. Eiffel provides built-in features to support the implementation of Design by Contract. The example below illustrates those features: [4]&lt;br /&gt;
&lt;br /&gt;
=== Example 3 === &lt;br /&gt;
&lt;br /&gt;
The following example shows a partial implementation of a bounded queue, with methods put and remove. The pre- and post-conditions for those methods are coded explicitly with the &amp;quot;require&amp;quot; and &amp;quot;ensure&amp;quot; features of Eiffel.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
                class BoundedQueue[G] feature&lt;br /&gt;
                    put(x:G) is&lt;br /&gt;
                       -- add x as newest element&lt;br /&gt;
                       require&lt;br /&gt;
                           not full&lt;br /&gt;
                       do&lt;br /&gt;
                           -- implementation of put&lt;br /&gt;
                           . . .&lt;br /&gt;
                       ensure&lt;br /&gt;
                           not empty&lt;br /&gt;
                       end;&lt;br /&gt;
                    remove is&lt;br /&gt;
                       -- remove oldest element&lt;br /&gt;
                       require&lt;br /&gt;
                           not empty&lt;br /&gt;
                       do&lt;br /&gt;
                           -- implementation of remove&lt;br /&gt;
                           . . .&lt;br /&gt;
                       ensure&lt;br /&gt;
                           not full&lt;br /&gt;
                       end;&lt;br /&gt;
                           empty: BOOLEAN is&lt;br /&gt;
                               -- is the queue empty?&lt;br /&gt;
                               do Result:=. . . end;&lt;br /&gt;
                           full: BOOLEAN is&lt;br /&gt;
                               -- is the queue full?&lt;br /&gt;
                               do Result:=. . . end;&lt;br /&gt;
                       end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The assertions are checked at run-time. This checking can be turned on or off as a result of a compilation switch.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
Programming by contract places responsibility squarely on the shoulders of the caller for ensuring that specified preconditions for a function are met, and relieves the function of any responsibility for verifying that preconditions are met. As intended, this has the desirable effect of eliminating redundant validity checking. In real life, however, it also has an undesirable side effect: If the caller makes a mistake and does not in fact meet the preconditions, the function is likely to fail, and the cause of the failure may be difficult to track down. A pointer that is null or contains an invalid address is usually easy to diagnose; since an exception will be caused immediately an attempt is made to deference it, but other errors may cause failures far removed from the root cause.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
http://en.wikibooks.org/wiki/Computer_programming/Design_by_Contract&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.eventhelix.com/RealtimeMantra/Object_Oriented/design_by_contract.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.cs.uno.edu/~c1581/Labs2006/lab7/lab7.htm&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.phpunit.de/pocket_guide/3.2/en/test-first-programming.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.python.org/dev/peps/pep-0316/&amp;lt;br&amp;gt;&lt;br /&gt;
http://www.artima.com/cppsource/deepspace2.html&amp;lt;br&amp;gt;&lt;br /&gt;
http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
[1] http://www.ibm.com/developerworks/rational/library/455.html#N10324 &amp;lt;br&amp;gt;&lt;br /&gt;
[2] http://archive.eiffel.com/doc/manuals/technology/contract/page.html &amp;lt;br&amp;gt;&lt;br /&gt;
[3] http://www.wayforward.net/pycontract/ &amp;lt;br&amp;gt;&lt;br /&gt;
[4] http://www.patentstorm.us/patents/6442750-description.html&lt;/div&gt;</summary>
		<author><name>Tmakhij</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1_2_316&amp;diff=4271</id>
		<title>CSC/ECE 517 Fall 2007/wiki1 2 316</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1_2_316&amp;diff=4271"/>
		<updated>2007-09-21T18:00:07Z</updated>

		<summary type="html">&lt;p&gt;Tmakhij: /* Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The Question ==&lt;br /&gt;
&lt;br /&gt;
A callback is code that one wants to be called from within a method after that method has been called. That is, it is a method of the caller that is invoked by the callee. Give an example using a closure in Ruby, and show how this is more elegant than similar code in Smalltalk.&lt;br /&gt;
&lt;br /&gt;
== Understanding of the question ==&lt;br /&gt;
&lt;br /&gt;
The question requires us to compare callback using a closure code constructed in RUBY with a similar code constructed in Smalltalk and then show how the RUBY code is more elegant.&lt;br /&gt;
&lt;br /&gt;
== What is Callback? ==&lt;br /&gt;
&lt;br /&gt;
A function passed as an argument is called a callback, since it is &amp;quot;called back&amp;quot; by the other function.[2] In general a callback is a piece of &amp;quot;your&amp;quot; code you ask &amp;quot;some other piece&amp;quot; of code to invoke in certain events, e.g. some code to be executed if a button is clicked on the screen.&lt;br /&gt;
&lt;br /&gt;
In most languages you specify a call back by passing the address of your subroutine to the system you're requesting the callback from.&lt;br /&gt;
&lt;br /&gt;
In RUBY callback is implemented using blocks. A nameless block of code may be passed as an argument to a method for implementation of callback.&lt;br /&gt;
&lt;br /&gt;
=== Blocks and Closures ===&lt;br /&gt;
&lt;br /&gt;
Blocks are basically nameless functions. You can pass a nameless function to another function, and then that function can invoke the passed-in nameless function. For example, a function could perform iteration by passing one item at a time to the nameless function. In Ruby, any method can be called with a block as an implicit argument. Inside the method, you can call the block using the yield keyword with a value. We can create a closure out of a block. A closure is a nameless function. You can pass around a nameless function object, the closure, to another method to customize the behavior of the method. As another example, if you have a sort method to sort an array or list, you can pass a block to define how to compare the elements. This is not iteration. This is not a loop. But it is using blocks. A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away.[3]&lt;br /&gt;
&lt;br /&gt;
===Example===&lt;br /&gt;
&lt;br /&gt;
In RUBY any chunk of code between braces {} or between do ... end is a block. This can be associated with a function call also.&lt;br /&gt;
&lt;br /&gt;
Eg.{ puts &amp;quot;Hello&amp;quot; }  &amp;lt;- block&lt;br /&gt;
&lt;br /&gt;
 def about_block&lt;br /&gt;
  puts &amp;quot;In the function&amp;quot;&lt;br /&gt;
  yield&lt;br /&gt;
 end&lt;br /&gt;
 &lt;br /&gt;
 about_block {puts &amp;quot;This is the block&amp;quot;} &amp;lt;- Associated with a function call&lt;br /&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;br /&gt;
 In the function&amp;lt;br&amp;gt;&lt;br /&gt;
 This is the block&lt;br /&gt;
&lt;br /&gt;
''&amp;quot;yield&amp;quot;'' is the keyword which invokes the block of code associated with the function call.&lt;br /&gt;
&lt;br /&gt;
Detailed Explanation of the program : The function about_blocks is defined and called at the end. When it is called the control passes to the first ''&amp;quot;puts&amp;quot;'' in the function. When the next line is executed (''yield'') it refers to the block associated with the function call. So, it prints &amp;quot;This is the block&amp;quot;&lt;br /&gt;
&lt;br /&gt;
== Functor ==&lt;br /&gt;
&lt;br /&gt;
A functor is nothing more than an object that behaves like a function (which is the case in RUBY - everything is an object).[2]&lt;br /&gt;
&lt;br /&gt;
== Approach to the Question ==&lt;br /&gt;
&lt;br /&gt;
Suppose we need to associate actions to a particular button so that it does a required function. &lt;br /&gt;
Eg. In a form if we press CLEAR, it should clear the form and SUBMIT should submit the form. Ruby’s blocks are a convenient way to do this.&lt;br /&gt;
We assume that on pressing the button a callback method, button_pressed, will be invoked. The obvious way of adding functionality to these buttons is to create subclasses of Button(which is the class) and have each subclass implement its own button_pressed method.&lt;br /&gt;
Eg. &lt;br /&gt;
 class FormButton &amp;lt; Button&lt;br /&gt;
     def initialize&lt;br /&gt;
         super(&amp;quot;Submit&amp;quot;) # invoke Button's initialize&lt;br /&gt;
        end&lt;br /&gt;
    def button_pressed&lt;br /&gt;
    # do submit actions...&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
 submit_button = FormButton.new&lt;br /&gt;
This will lead to a large number of subclasses. We can fix this problem using blocks in RUBY.&lt;br /&gt;
pressbutton = PressButton.new&lt;br /&gt;
   class FormButton &amp;lt; Button&lt;br /&gt;
     def initialize(label, &amp;amp;action)&lt;br /&gt;
        super(label)&lt;br /&gt;
        @action = action&lt;br /&gt;
     end&lt;br /&gt;
     def button_pressed&lt;br /&gt;
       @action.call(self)&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
 submit_button = FormButton.new(&amp;quot;Submit&amp;quot;) { pressbutton.submit }&lt;br /&gt;
 reset_button = FormButton.new(&amp;quot;Clear&amp;quot;) { pressbutton.reset }&lt;br /&gt;
&lt;br /&gt;
The key to all this is the second parameter to FormButton#initialize. If the last parameter in a method definition is prefixed with an ampersand (such as &amp;amp;action), Ruby looks for a code block whenever that method is called. That code block is converted to an object of class Proc and assigned to the parameter. You can then treat the parameter as any other variable. When the callback method button_pressed is invoked, we use the Proc#call method on that object to invoke the block.&lt;br /&gt;
&lt;br /&gt;
== Comparisons of Smalltalk and Ruby code ==&lt;br /&gt;
&lt;br /&gt;
Smalltalk&lt;br /&gt;
&lt;br /&gt;
 foo&lt;br /&gt;
    | xs |&lt;br /&gt;
    xs := #(1 2 3 4).&lt;br /&gt;
    xs do: [:x | ^x].&lt;br /&gt;
    ^0&lt;br /&gt;
  bar&lt;br /&gt;
    Transcript show: (self foo) &amp;quot;prints 1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
In Smalltalk, ^ will return from method foo, and not just from the closure. do: is defined as a plain method, and there is nothing special about it. &lt;br /&gt;
&lt;br /&gt;
 foo&lt;br /&gt;
    ^[ x: | ^x ]&lt;br /&gt;
  bar&lt;br /&gt;
    | f |&lt;br /&gt;
    f := self foo.&lt;br /&gt;
    f value: 123 &amp;quot;error!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
When block returned by method foo is invoked, it attempts to return a value from foo. Since the call to foo has already completed, this operation results in an error.&lt;br /&gt;
&lt;br /&gt;
Ruby&lt;br /&gt;
&lt;br /&gt;
 def foo&lt;br /&gt;
    f = Proc.new { return &amp;quot;return from foo from inside proc&amp;quot; }&lt;br /&gt;
    f.call # control leaves foo here&lt;br /&gt;
    return &amp;quot;return from foo&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def bar&lt;br /&gt;
    f = lambda { return &amp;quot;return from lambda&amp;quot; }&lt;br /&gt;
    f.call # control does not leave bar here&lt;br /&gt;
    return &amp;quot;return from bar&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  puts foo # prints &amp;quot;return from foo from inside proc&amp;quot;&lt;br /&gt;
  puts bar # prints &amp;quot;return from bar&amp;quot;&lt;br /&gt;
&lt;br /&gt;
It allows the programmer to choose the way he wants &amp;quot;return&amp;quot; to be captured.Both Proc.new and lambda in this example are ways to create a closure.[4]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://mccammon.ucsd.edu/~oompaa/Oompaa/oompaa/global/doc/callback.html&amp;lt;br&amp;gt;&lt;br /&gt;
2. http://forum.java.sun.com/thread.jspa?threadID=485166&amp;amp;messageID=2269118&amp;lt;br&amp;gt;&lt;br /&gt;
3. http://www.artima.com/intv/closures.html&amp;lt;br&amp;gt;&lt;br /&gt;
4. http://en.wikipedia.org/wiki/Closure_(computer_science)&amp;lt;br&amp;gt;&lt;br /&gt;
5. The Pragmatic Programmers - Programming Ruby&lt;/div&gt;</summary>
		<author><name>Tmakhij</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1_2_316&amp;diff=4254</id>
		<title>CSC/ECE 517 Fall 2007/wiki1 2 316</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1_2_316&amp;diff=4254"/>
		<updated>2007-09-20T02:58:26Z</updated>

		<summary type="html">&lt;p&gt;Tmakhij: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The Question ==&lt;br /&gt;
&lt;br /&gt;
A callback is code that one wants to be called from within a method after that method has been called. That is, it is a method of the caller that is invoked by the callee. Give an example using a closure in Ruby, and show how this is more elegant than similar code in Smalltalk.&lt;br /&gt;
&lt;br /&gt;
== Understanding of the question ==&lt;br /&gt;
&lt;br /&gt;
The question requires us to compare callback using a closure code constructed in RUBY with a similar code constructed in Smalltalk and then show how the RUBY code is more elegant.&lt;br /&gt;
&lt;br /&gt;
== What is Callback? ==&lt;br /&gt;
&lt;br /&gt;
A function passed as an argument is called a callback, since it is &amp;quot;called back&amp;quot; by the other function.[2] In general a callback is a piece of &amp;quot;your&amp;quot; code you ask &amp;quot;some other piece&amp;quot; of code to invoke in certain events, e.g. some code to be executed if a button is clicked on the screen.&lt;br /&gt;
&lt;br /&gt;
In most languages you specify a call back by passing the address of your subroutine to the system you're requesting the callback from.&lt;br /&gt;
&lt;br /&gt;
In RUBY callback is implemented using blocks. A nameless block of code may be passed as an argument to a method for implementation of callback.&lt;br /&gt;
&lt;br /&gt;
=== Blocks and Closures ===&lt;br /&gt;
&lt;br /&gt;
Blocks are basically nameless functions. You can pass a nameless function to another function, and then that function can invoke the passed-in nameless function. For example, a function could perform iteration by passing one item at a time to the nameless function. In Ruby, any method can be called with a block as an implicit argument. Inside the method, you can call the block using the yield keyword with a value. We can create a closure out of a block. A closure is a nameless function. You can pass around a nameless function object, the closure, to another method to customize the behavior of the method. As another example, if you have a sort method to sort an array or list, you can pass a block to define how to compare the elements. This is not iteration. This is not a loop. But it is using blocks. A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away.[3]&lt;br /&gt;
&lt;br /&gt;
===Example===&lt;br /&gt;
&lt;br /&gt;
 def count(x)&lt;br /&gt;
  a1, a2 = 1, 1&lt;br /&gt;
   while a1 &amp;lt;= x&lt;br /&gt;
   yield a1&lt;br /&gt;
   a1, a2 = a2, a1+a2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 count(1000) {|f| print f, &amp;quot; &amp;quot; }&lt;br /&gt;
&lt;br /&gt;
== Functor ==&lt;br /&gt;
&lt;br /&gt;
A functor is nothing more than an object that behaves like a function (which is the case in RUBY - everything is an object).[2]&lt;br /&gt;
&lt;br /&gt;
== Approach to the Question ==&lt;br /&gt;
&lt;br /&gt;
Suppose we need to associate actions to a particular button so that it does a required function. &lt;br /&gt;
Eg. In a form if we press CLEAR, it should clear the form and SUBMIT should submit the form. Ruby’s blocks are a convenient way to do this.&lt;br /&gt;
We assume that on pressing the button a callback method, button_pressed, will be invoked. The obvious way of adding functionality to these buttons is to create subclasses of Button(which is the class) and have each subclass implement its own button_pressed method.&lt;br /&gt;
Eg. &lt;br /&gt;
 class FormButton &amp;lt; Button&lt;br /&gt;
     def initialize&lt;br /&gt;
         super(&amp;quot;Submit&amp;quot;) # invoke Button's initialize&lt;br /&gt;
        end&lt;br /&gt;
    def button_pressed&lt;br /&gt;
    # do submit actions...&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
 submit_button = FormButton.new&lt;br /&gt;
This will lead to a large number of subclasses. We can fix this problem using blocks in RUBY.&lt;br /&gt;
pressbutton = PressButton.new&lt;br /&gt;
   class FormButton &amp;lt; Button&lt;br /&gt;
     def initialize(label, &amp;amp;action)&lt;br /&gt;
        super(label)&lt;br /&gt;
        @action = action&lt;br /&gt;
     end&lt;br /&gt;
     def button_pressed&lt;br /&gt;
       @action.call(self)&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
 submit_button = FormButton.new(&amp;quot;Submit&amp;quot;) { pressbutton.submit }&lt;br /&gt;
 reset_button = FormButton.new(&amp;quot;Clear&amp;quot;) { pressbutton.reset }&lt;br /&gt;
&lt;br /&gt;
The key to all this is the second parameter to FormButton#initialize. If the last parameter in a method definition is prefixed with an ampersand (such as &amp;amp;action), Ruby looks for a code block whenever that method is called. That code block is converted to an object of class Proc and assigned to the parameter. You can then treat the parameter as any other variable. When the callback method button_pressed is invoked, we use the Proc#call method on that object to invoke the block.&lt;br /&gt;
&lt;br /&gt;
== Comparisons of Smalltalk and Ruby code ==&lt;br /&gt;
&lt;br /&gt;
Smalltalk&lt;br /&gt;
&lt;br /&gt;
 foo&lt;br /&gt;
    | xs |&lt;br /&gt;
    xs := #(1 2 3 4).&lt;br /&gt;
    xs do: [:x | ^x].&lt;br /&gt;
    ^0&lt;br /&gt;
  bar&lt;br /&gt;
    Transcript show: (self foo) &amp;quot;prints 1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
In Smalltalk, ^ will return from method foo, and not just from the closure. do: is defined as a plain method, and there is nothing special about it. &lt;br /&gt;
&lt;br /&gt;
 foo&lt;br /&gt;
    ^[ x: | ^x ]&lt;br /&gt;
  bar&lt;br /&gt;
    | f |&lt;br /&gt;
    f := self foo.&lt;br /&gt;
    f value: 123 &amp;quot;error!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
When block returned by method foo is invoked, it attempts to return a value from foo. Since the call to foo has already completed, this operation results in an error.&lt;br /&gt;
&lt;br /&gt;
Ruby&lt;br /&gt;
&lt;br /&gt;
 def foo&lt;br /&gt;
    f = Proc.new { return &amp;quot;return from foo from inside proc&amp;quot; }&lt;br /&gt;
    f.call # control leaves foo here&lt;br /&gt;
    return &amp;quot;return from foo&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def bar&lt;br /&gt;
    f = lambda { return &amp;quot;return from lambda&amp;quot; }&lt;br /&gt;
    f.call # control does not leave bar here&lt;br /&gt;
    return &amp;quot;return from bar&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  puts foo # prints &amp;quot;return from foo from inside proc&amp;quot;&lt;br /&gt;
  puts bar # prints &amp;quot;return from bar&amp;quot;&lt;br /&gt;
&lt;br /&gt;
It allows the programmer to choose the way he wants &amp;quot;return&amp;quot; to be captured.Both Proc.new and lambda in this example are ways to create a closure.[4]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://mccammon.ucsd.edu/~oompaa/Oompaa/oompaa/global/doc/callback.html&amp;lt;br&amp;gt;&lt;br /&gt;
2. http://forum.java.sun.com/thread.jspa?threadID=485166&amp;amp;messageID=2269118&amp;lt;br&amp;gt;&lt;br /&gt;
3. http://www.artima.com/intv/closures.html&amp;lt;br&amp;gt;&lt;br /&gt;
4. http://en.wikipedia.org/wiki/Closure_(computer_science)&amp;lt;br&amp;gt;&lt;br /&gt;
5. The Pragmatic Programmers - Programming Ruby&lt;/div&gt;</summary>
		<author><name>Tmakhij</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1_2_316&amp;diff=4253</id>
		<title>CSC/ECE 517 Fall 2007/wiki1 2 316</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1_2_316&amp;diff=4253"/>
		<updated>2007-09-20T02:55:42Z</updated>

		<summary type="html">&lt;p&gt;Tmakhij: /* Example */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The Question ==&lt;br /&gt;
&lt;br /&gt;
A callback is code that one wants to be called from within a method after that method has been called. That is, it is a method of the caller that is invoked by the callee. Give an example using a closure in Ruby, and show how this is more elegant than similar code in Smalltalk.&lt;br /&gt;
&lt;br /&gt;
== Understanding of the question ==&lt;br /&gt;
&lt;br /&gt;
The question requires us to compare callback using a closure code constructed in RUBY with a similar code constructed in Smalltalk and then show how the RUBY code is more elegant.&lt;br /&gt;
&lt;br /&gt;
== What is Callback? ==&lt;br /&gt;
&lt;br /&gt;
A function passed as an argument is called a callback, since it is &amp;quot;called back&amp;quot; by the other function.[2] In general a callback is a piece of &amp;quot;your&amp;quot; code you ask &amp;quot;some other piece&amp;quot; of code to invoke in certain events, e.g. some code to be executed if a button is clicked on the screen.&lt;br /&gt;
&lt;br /&gt;
In most languages you specify a call back by passing the address of your subroutine to the system you're requesting the callback from.&lt;br /&gt;
&lt;br /&gt;
In RUBY callback is implemented using blocks. A nameless block of code may be passed as an argument to a method for implementation of callback.&lt;br /&gt;
&lt;br /&gt;
=== Blocks and Closures ===&lt;br /&gt;
&lt;br /&gt;
Blocks are basically nameless functions. You can pass a nameless function to another function, and then that function can invoke the passed-in nameless function. For example, a function could perform iteration by passing one item at a time to the nameless function. In Ruby, any method can be called with a block as an implicit argument. Inside the method, you can call the block using the yield keyword with a value. We can create a closure out of a block. A closure is a nameless function. You can pass around a nameless function object, the closure, to another method to customize the behavior of the method. As another example, if you have a sort method to sort an array or list, you can pass a block to define how to compare the elements. This is not iteration. This is not a loop. But it is using blocks. A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away.[3]&lt;br /&gt;
&lt;br /&gt;
===Example===&lt;br /&gt;
&lt;br /&gt;
 def count(x)&lt;br /&gt;
  a1, a2 = 1, 1&lt;br /&gt;
   while a1 &amp;lt;= x&lt;br /&gt;
   yield a1&lt;br /&gt;
   a1, a2 = a2, a1+a2&lt;br /&gt;
  end&lt;br /&gt;
 end&lt;br /&gt;
 count(1000) {|f| print f, &amp;quot; &amp;quot; }&lt;br /&gt;
&lt;br /&gt;
== Functor ==&lt;br /&gt;
&lt;br /&gt;
A functor is nothing more than an object that behaves like a function (which is the case in RUBY - everything is an object).[2]&lt;br /&gt;
&lt;br /&gt;
== Approach to the Question ==&lt;br /&gt;
&lt;br /&gt;
Suppose we need to associate actions to a particular button so that it does a required function. &lt;br /&gt;
Eg. In a form if we press CLEAR, it should clear the form and SUBMIT should submit the form. Ruby’s blocks are a convenient way to do this.&lt;br /&gt;
We assume that on pressing the button a callback method, button_pressed, will be invoked. The obvious way of adding functionality to these buttons is to create subclasses of Button(which is the class) and have each subclass implement its own button_pressed method.&lt;br /&gt;
Eg. &lt;br /&gt;
 class FormButton &amp;lt; Button&lt;br /&gt;
     def initialize&lt;br /&gt;
         super(&amp;quot;Submit&amp;quot;) # invoke Button's initialize&lt;br /&gt;
        end&lt;br /&gt;
    def button_pressed&lt;br /&gt;
    # do submit actions...&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
 submit_button = FormButton.new&lt;br /&gt;
This will lead to a large number of subclasses. We can fix this problem using blocks in RUBY.&lt;br /&gt;
pressbutton = PressButton.new&lt;br /&gt;
   class FormButton &amp;lt; Button&lt;br /&gt;
     def initialize(label, &amp;amp;action)&lt;br /&gt;
        super(label)&lt;br /&gt;
        @action = action&lt;br /&gt;
     end&lt;br /&gt;
     def button_pressed&lt;br /&gt;
       @action.call(self)&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
 submit_button = FormButton.new(&amp;quot;Submit&amp;quot;) { pressbutton.submit }&lt;br /&gt;
 reset_button = FormButton.new(&amp;quot;Clear&amp;quot;) { pressbutton.reset }&lt;br /&gt;
&lt;br /&gt;
The key to all this is the second parameter to FormButton#initialize. If the last parameter in a method definition is prefixed with an ampersand (such as &amp;amp;action), Ruby looks for a code block whenever that method is called. That code block is converted to an object of class Proc and assigned to the parameter. You can then treat the parameter as any other variable. When the callback method button_pressed is invoked, we use the Proc#call method on that object to invoke the block.&lt;br /&gt;
&lt;br /&gt;
== Comparisons of Smalltalk and Ruby code ==&lt;br /&gt;
&lt;br /&gt;
Smalltalk&lt;br /&gt;
&lt;br /&gt;
 foo&lt;br /&gt;
    | xs |&lt;br /&gt;
    xs := #(1 2 3 4).&lt;br /&gt;
    xs do: [:x | ^x].&lt;br /&gt;
    ^0&lt;br /&gt;
  bar&lt;br /&gt;
    Transcript show: (self foo) &amp;quot;prints 1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
In Smalltalk, ^ will return from method foo, and not just from the closure. do: is defined as a plain method, and there is nothing special about it. &lt;br /&gt;
&lt;br /&gt;
 foo&lt;br /&gt;
    ^[ x: | ^x ]&lt;br /&gt;
  bar&lt;br /&gt;
    | f |&lt;br /&gt;
    f := self foo.&lt;br /&gt;
    f value: 123 &amp;quot;error!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
When block returned by method foo is invoked, it attempts to return a value from foo. Since the call to foo has already completed, this operation results in an error.&lt;br /&gt;
&lt;br /&gt;
Ruby&lt;br /&gt;
&lt;br /&gt;
 def foo&lt;br /&gt;
    f = Proc.new { return &amp;quot;return from foo from inside proc&amp;quot; }&lt;br /&gt;
    f.call # control leaves foo here&lt;br /&gt;
    return &amp;quot;return from foo&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def bar&lt;br /&gt;
    f = lambda { return &amp;quot;return from lambda&amp;quot; }&lt;br /&gt;
    f.call # control does not leave bar here&lt;br /&gt;
    return &amp;quot;return from bar&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  puts foo # prints &amp;quot;return from foo from inside proc&amp;quot;&lt;br /&gt;
  puts bar # prints &amp;quot;return from bar&amp;quot;&lt;br /&gt;
&lt;br /&gt;
It allows the programmer to choose the way he wants &amp;quot;return&amp;quot; to be captured.Both Proc.new and lambda in this example are ways to create a closure.[4]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://mccammon.ucsd.edu/~oompaa/Oompaa/oompaa/global/doc/callback.html&amp;lt;br&amp;gt;&lt;br /&gt;
2. http://forum.java.sun.com/thread.jspa?threadID=485166&amp;amp;messageID=2269118&amp;lt;br&amp;gt;&lt;br /&gt;
3. http://www.artima.com/intv/closures.html&amp;lt;br&amp;gt;&lt;br /&gt;
4. http://en.wikipedia.org/wiki/Closure_(computer_science)&lt;/div&gt;</summary>
		<author><name>Tmakhij</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1_2_316&amp;diff=4251</id>
		<title>CSC/ECE 517 Fall 2007/wiki1 2 316</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1_2_316&amp;diff=4251"/>
		<updated>2007-09-20T02:54:54Z</updated>

		<summary type="html">&lt;p&gt;Tmakhij: /* Blocks and Closures */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The Question ==&lt;br /&gt;
&lt;br /&gt;
A callback is code that one wants to be called from within a method after that method has been called. That is, it is a method of the caller that is invoked by the callee. Give an example using a closure in Ruby, and show how this is more elegant than similar code in Smalltalk.&lt;br /&gt;
&lt;br /&gt;
== Understanding of the question ==&lt;br /&gt;
&lt;br /&gt;
The question requires us to compare callback using a closure code constructed in RUBY with a similar code constructed in Smalltalk and then show how the RUBY code is more elegant.&lt;br /&gt;
&lt;br /&gt;
== What is Callback? ==&lt;br /&gt;
&lt;br /&gt;
A function passed as an argument is called a callback, since it is &amp;quot;called back&amp;quot; by the other function.[2] In general a callback is a piece of &amp;quot;your&amp;quot; code you ask &amp;quot;some other piece&amp;quot; of code to invoke in certain events, e.g. some code to be executed if a button is clicked on the screen.&lt;br /&gt;
&lt;br /&gt;
In most languages you specify a call back by passing the address of your subroutine to the system you're requesting the callback from.&lt;br /&gt;
&lt;br /&gt;
In RUBY callback is implemented using blocks. A nameless block of code may be passed as an argument to a method for implementation of callback.&lt;br /&gt;
&lt;br /&gt;
=== Blocks and Closures ===&lt;br /&gt;
&lt;br /&gt;
Blocks are basically nameless functions. You can pass a nameless function to another function, and then that function can invoke the passed-in nameless function. For example, a function could perform iteration by passing one item at a time to the nameless function. In Ruby, any method can be called with a block as an implicit argument. Inside the method, you can call the block using the yield keyword with a value. We can create a closure out of a block. A closure is a nameless function. You can pass around a nameless function object, the closure, to another method to customize the behavior of the method. As another example, if you have a sort method to sort an array or list, you can pass a block to define how to compare the elements. This is not iteration. This is not a loop. But it is using blocks. A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away.[3]&lt;br /&gt;
&lt;br /&gt;
===Example===&lt;br /&gt;
&lt;br /&gt;
def count(x)&lt;br /&gt;
 a1, a2 = 1, 1&lt;br /&gt;
  while a1 &amp;lt;= x&lt;br /&gt;
  yield a1&lt;br /&gt;
  a1, a2 = a2, a1+a2&lt;br /&gt;
 end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
count(1000) {|f| print f, &amp;quot; &amp;quot; }&lt;br /&gt;
&lt;br /&gt;
== Functor ==&lt;br /&gt;
&lt;br /&gt;
A functor is nothing more than an object that behaves like a function (which is the case in RUBY - everything is an object).[2]&lt;br /&gt;
&lt;br /&gt;
== Approach to the Question ==&lt;br /&gt;
&lt;br /&gt;
Suppose we need to associate actions to a particular button so that it does a required function. &lt;br /&gt;
Eg. In a form if we press CLEAR, it should clear the form and SUBMIT should submit the form. Ruby’s blocks are a convenient way to do this.&lt;br /&gt;
We assume that on pressing the button a callback method, button_pressed, will be invoked. The obvious way of adding functionality to these buttons is to create subclasses of Button(which is the class) and have each subclass implement its own button_pressed method.&lt;br /&gt;
Eg. &lt;br /&gt;
 class FormButton &amp;lt; Button&lt;br /&gt;
     def initialize&lt;br /&gt;
         super(&amp;quot;Submit&amp;quot;) # invoke Button's initialize&lt;br /&gt;
        end&lt;br /&gt;
    def button_pressed&lt;br /&gt;
    # do submit actions...&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
 submit_button = FormButton.new&lt;br /&gt;
This will lead to a large number of subclasses. We can fix this problem using blocks in RUBY.&lt;br /&gt;
pressbutton = PressButton.new&lt;br /&gt;
   class FormButton &amp;lt; Button&lt;br /&gt;
     def initialize(label, &amp;amp;action)&lt;br /&gt;
        super(label)&lt;br /&gt;
        @action = action&lt;br /&gt;
     end&lt;br /&gt;
     def button_pressed&lt;br /&gt;
       @action.call(self)&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
 submit_button = FormButton.new(&amp;quot;Submit&amp;quot;) { pressbutton.submit }&lt;br /&gt;
 reset_button = FormButton.new(&amp;quot;Clear&amp;quot;) { pressbutton.reset }&lt;br /&gt;
&lt;br /&gt;
The key to all this is the second parameter to FormButton#initialize. If the last parameter in a method definition is prefixed with an ampersand (such as &amp;amp;action), Ruby looks for a code block whenever that method is called. That code block is converted to an object of class Proc and assigned to the parameter. You can then treat the parameter as any other variable. When the callback method button_pressed is invoked, we use the Proc#call method on that object to invoke the block.&lt;br /&gt;
&lt;br /&gt;
== Comparisons of Smalltalk and Ruby code ==&lt;br /&gt;
&lt;br /&gt;
Smalltalk&lt;br /&gt;
&lt;br /&gt;
 foo&lt;br /&gt;
    | xs |&lt;br /&gt;
    xs := #(1 2 3 4).&lt;br /&gt;
    xs do: [:x | ^x].&lt;br /&gt;
    ^0&lt;br /&gt;
  bar&lt;br /&gt;
    Transcript show: (self foo) &amp;quot;prints 1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
In Smalltalk, ^ will return from method foo, and not just from the closure. do: is defined as a plain method, and there is nothing special about it. &lt;br /&gt;
&lt;br /&gt;
 foo&lt;br /&gt;
    ^[ x: | ^x ]&lt;br /&gt;
  bar&lt;br /&gt;
    | f |&lt;br /&gt;
    f := self foo.&lt;br /&gt;
    f value: 123 &amp;quot;error!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
When block returned by method foo is invoked, it attempts to return a value from foo. Since the call to foo has already completed, this operation results in an error.&lt;br /&gt;
&lt;br /&gt;
Ruby&lt;br /&gt;
&lt;br /&gt;
 def foo&lt;br /&gt;
    f = Proc.new { return &amp;quot;return from foo from inside proc&amp;quot; }&lt;br /&gt;
    f.call # control leaves foo here&lt;br /&gt;
    return &amp;quot;return from foo&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def bar&lt;br /&gt;
    f = lambda { return &amp;quot;return from lambda&amp;quot; }&lt;br /&gt;
    f.call # control does not leave bar here&lt;br /&gt;
    return &amp;quot;return from bar&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  puts foo # prints &amp;quot;return from foo from inside proc&amp;quot;&lt;br /&gt;
  puts bar # prints &amp;quot;return from bar&amp;quot;&lt;br /&gt;
&lt;br /&gt;
It allows the programmer to choose the way he wants &amp;quot;return&amp;quot; to be captured.Both Proc.new and lambda in this example are ways to create a closure.[4]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://mccammon.ucsd.edu/~oompaa/Oompaa/oompaa/global/doc/callback.html&amp;lt;br&amp;gt;&lt;br /&gt;
2. http://forum.java.sun.com/thread.jspa?threadID=485166&amp;amp;messageID=2269118&amp;lt;br&amp;gt;&lt;br /&gt;
3. http://www.artima.com/intv/closures.html&amp;lt;br&amp;gt;&lt;br /&gt;
4. http://en.wikipedia.org/wiki/Closure_(computer_science)&lt;/div&gt;</summary>
		<author><name>Tmakhij</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1_2_316&amp;diff=4006</id>
		<title>CSC/ECE 517 Fall 2007/wiki1 2 316</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1_2_316&amp;diff=4006"/>
		<updated>2007-09-15T02:10:25Z</updated>

		<summary type="html">&lt;p&gt;Tmakhij: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The Question ==&lt;br /&gt;
&lt;br /&gt;
A callback is code that one wants to be called from within a method after that method has been called. That is, it is a method of the caller that is invoked by the callee. Give an example using a closure in Ruby, and show how this is more elegant than similar code in Smalltalk.&lt;br /&gt;
&lt;br /&gt;
== Understanding of the question ==&lt;br /&gt;
&lt;br /&gt;
The question requires us to compare callback using a closure code constructed in RUBY with a similar code constructed in Smalltalk and then show how the RUBY code is more elegant.&lt;br /&gt;
&lt;br /&gt;
== What is Callback? ==&lt;br /&gt;
&lt;br /&gt;
A function passed as an argument is called a callback, since it is &amp;quot;called back&amp;quot; by the other function.[2] In general a callback is a piece of &amp;quot;your&amp;quot; code you ask &amp;quot;some other piece&amp;quot; of code to invoke in certain events, e.g. some code to be executed if a button is clicked on the screen.&lt;br /&gt;
&lt;br /&gt;
In most languages you specify a call back by passing the address of your subroutine to the system you're requesting the callback from.&lt;br /&gt;
&lt;br /&gt;
In RUBY callback is implemented using blocks. A nameless block of code may be passed as an argument to a method for implementation of callback.&lt;br /&gt;
&lt;br /&gt;
=== Blocks and Closures ===&lt;br /&gt;
&lt;br /&gt;
Blocks are basically nameless functions. You can pass a nameless function to another function, and then that function can invoke the passed-in nameless function. For example, a function could perform iteration by passing one item at a time to the nameless function. In Ruby, any method can be called with a block as an implicit argument. Inside the method, you can call the block using the yield keyword with a value. We can create a closure out of a block. A closure is a nameless function. You can pass around a nameless function object, the closure, to another method to customize the behavior of the method. As another example, if you have a sort method to sort an array or list, you can pass a block to define how to compare the elements. This is not iteration. This is not a loop. But it is using blocks. A closure object has code to run, the executable, and state around the code, the scope. So you capture the environment, namely the local variables, in the closure. As a result, you can refer to the local variables inside a closure. Even after the function has returned, and its local scope has been destroyed, the local variables remain in existence as part of the closure object. When no one refers to the closure anymore, it's garbage collected, and the local variables go away.[3]&lt;br /&gt;
&lt;br /&gt;
== Functor ==&lt;br /&gt;
&lt;br /&gt;
A functor is nothing more than an object that behaves like a function (which is the case in RUBY - everything is an object).[2]&lt;br /&gt;
&lt;br /&gt;
== Approach to the Question ==&lt;br /&gt;
&lt;br /&gt;
Suppose we need to associate actions to a particular button so that it does a required function. &lt;br /&gt;
Eg. In a form if we press CLEAR, it should clear the form and SUBMIT should submit the form. Ruby’s blocks are a convenient way to do this.&lt;br /&gt;
We assume that on pressing the button a callback method, button_pressed, will be invoked. The obvious way of adding functionality to these buttons is to create subclasses of Button(which is the class) and have each subclass implement its own button_pressed method.&lt;br /&gt;
Eg. &lt;br /&gt;
 class FormButton &amp;lt; Button&lt;br /&gt;
     def initialize&lt;br /&gt;
         super(&amp;quot;Submit&amp;quot;) # invoke Button's initialize&lt;br /&gt;
        end&lt;br /&gt;
    def button_pressed&lt;br /&gt;
    # do submit actions...&lt;br /&gt;
    end&lt;br /&gt;
 end&lt;br /&gt;
 submit_button = FormButton.new&lt;br /&gt;
This will lead to a large number of subclasses. We can fix this problem using blocks in RUBY.&lt;br /&gt;
pressbutton = PressButton.new&lt;br /&gt;
   class FormButton &amp;lt; Button&lt;br /&gt;
     def initialize(label, &amp;amp;action)&lt;br /&gt;
        super(label)&lt;br /&gt;
        @action = action&lt;br /&gt;
     end&lt;br /&gt;
     def button_pressed&lt;br /&gt;
       @action.call(self)&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
 submit_button = FormButton.new(&amp;quot;Submit&amp;quot;) { pressbutton.submit }&lt;br /&gt;
 reset_button = FormButton.new(&amp;quot;Clear&amp;quot;) { pressbutton.reset }&lt;br /&gt;
&lt;br /&gt;
The key to all this is the second parameter to FormButton#initialize. If the last parameter in a method definition is prefixed with an ampersand (such as &amp;amp;action), Ruby looks for a code block whenever that method is called. That code block is converted to an object of class Proc and assigned to the parameter. You can then treat the parameter as any other variable. When the callback method button_pressed is invoked, we use the Proc#call method on that object to invoke the block.&lt;br /&gt;
&lt;br /&gt;
== Comparisons of Smalltalk and Ruby code ==&lt;br /&gt;
&lt;br /&gt;
Smalltalk&lt;br /&gt;
&lt;br /&gt;
 foo&lt;br /&gt;
    | xs |&lt;br /&gt;
    xs := #(1 2 3 4).&lt;br /&gt;
    xs do: [:x | ^x].&lt;br /&gt;
    ^0&lt;br /&gt;
  bar&lt;br /&gt;
    Transcript show: (self foo) &amp;quot;prints 1&amp;quot;&lt;br /&gt;
&lt;br /&gt;
In Smalltalk, ^ will return from method foo, and not just from the closure. do: is defined as a plain method, and there is nothing special about it. &lt;br /&gt;
&lt;br /&gt;
 foo&lt;br /&gt;
    ^[ x: | ^x ]&lt;br /&gt;
  bar&lt;br /&gt;
    | f |&lt;br /&gt;
    f := self foo.&lt;br /&gt;
    f value: 123 &amp;quot;error!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
When block returned by method foo is invoked, it attempts to return a value from foo. Since the call to foo has already completed, this operation results in an error.&lt;br /&gt;
&lt;br /&gt;
Ruby&lt;br /&gt;
&lt;br /&gt;
 def foo&lt;br /&gt;
    f = Proc.new { return &amp;quot;return from foo from inside proc&amp;quot; }&lt;br /&gt;
    f.call # control leaves foo here&lt;br /&gt;
    return &amp;quot;return from foo&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def bar&lt;br /&gt;
    f = lambda { return &amp;quot;return from lambda&amp;quot; }&lt;br /&gt;
    f.call # control does not leave bar here&lt;br /&gt;
    return &amp;quot;return from bar&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  puts foo # prints &amp;quot;return from foo from inside proc&amp;quot;&lt;br /&gt;
  puts bar # prints &amp;quot;return from bar&amp;quot;&lt;br /&gt;
&lt;br /&gt;
It allows the programmer to choose the way he wants &amp;quot;return&amp;quot; to be captured.Both Proc.new and lambda in this example are ways to create a closure.[4]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://mccammon.ucsd.edu/~oompaa/Oompaa/oompaa/global/doc/callback.html&amp;lt;br&amp;gt;&lt;br /&gt;
2. http://forum.java.sun.com/thread.jspa?threadID=485166&amp;amp;messageID=2269118&amp;lt;br&amp;gt;&lt;br /&gt;
3. http://www.artima.com/intv/closures.html&amp;lt;br&amp;gt;&lt;br /&gt;
4. http://en.wikipedia.org/wiki/Closure_(computer_science)&lt;/div&gt;</summary>
		<author><name>Tmakhij</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1_2_316&amp;diff=3998</id>
		<title>CSC/ECE 517 Fall 2007/wiki1 2 316</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1_2_316&amp;diff=3998"/>
		<updated>2007-09-15T00:26:21Z</updated>

		<summary type="html">&lt;p&gt;Tmakhij: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The Question ==&lt;br /&gt;
A callback is code that one wants to be called from within a method after that method has been called. That is, it is a method of the caller that is invoked by the callee. Give an example using a closure in Ruby, and show how this is more elegant than similar code in Smalltalk.&lt;br /&gt;
&lt;br /&gt;
== Understanding of the question ==&lt;br /&gt;
The question requires us to compare callback using a closure code constructed in RUBY with a similar code constructed in Smalltalk and then show how the RUBY code is more elegant.&lt;br /&gt;
&lt;br /&gt;
== Approach ==&lt;br /&gt;
Suppose we need to associate actions to a particular button so that it does a required function. Eg. In a form if we press CLEAR, it should clear the form and SUBMIT should submit the form. Ruby’s blocks are a convenient way to do this.&lt;br /&gt;
We assume that on pressing the button a callback method, button_pressed, will be invoked. The obvious way of adding functionality to these buttons is to create subclasses of Button(which is the class) and have each subclass implement its own button_pressed method.&lt;br /&gt;
Eg. &lt;br /&gt;
 class FormButton &amp;lt; Button&lt;br /&gt;
     def initialize&lt;br /&gt;
         super(&amp;quot;Submit&amp;quot;) # invoke Button's initialize&lt;br /&gt;
        end&lt;br /&gt;
    def button_pressed&lt;br /&gt;
    # do submit actions...&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
submit_button = FormButton.new&lt;br /&gt;
This will lead to a large number of subclasses. We can fix this problem using blocks in RUBY.&lt;br /&gt;
pressbutton = PressButton.new&lt;br /&gt;
   class FormButton &amp;lt; Button&lt;br /&gt;
     def initialize(label, &amp;amp;action)&lt;br /&gt;
        super(label)&lt;br /&gt;
        @action = action&lt;br /&gt;
     end&lt;br /&gt;
     def button_pressed&lt;br /&gt;
       @action.call(self)&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
submit_button = FormButton.new(&amp;quot;Submit&amp;quot;) { pressbutton.submit }&lt;br /&gt;
reset_button = FormButton.new(&amp;quot;Clear&amp;quot;) { pressbutton.reset }&lt;br /&gt;
&lt;br /&gt;
The key to all this is the second parameter to FormButton#initialize. If the last parameter in a method definition is prefixed with an ampersand (such as &amp;amp;action), Ruby looks for a code block whenever that method is called. That code block is converted to an object of class Proc and assigned to the parameter. You can then treat the parameter as any other variable. When the callback method button_pressed is invoked, we use the&lt;br /&gt;
Proc#call method on that object to invoke the block.&lt;br /&gt;
&lt;br /&gt;
Now if we want to do the same task in Smalltalk,&lt;/div&gt;</summary>
		<author><name>Tmakhij</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1_2_316&amp;diff=3994</id>
		<title>CSC/ECE 517 Fall 2007/wiki1 2 316</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2007/wiki1_2_316&amp;diff=3994"/>
		<updated>2007-09-15T00:16:56Z</updated>

		<summary type="html">&lt;p&gt;Tmakhij: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== The Question ==&lt;br /&gt;
A callback is code that one wants to be called from within a method after that method has been called. That is, it is a method of the caller that is invoked by the callee. Give an example using a closure in Ruby, and show how this is more elegant than similar code in Smalltalk.&lt;br /&gt;
&lt;br /&gt;
Suppose we need to associate actions to a particular button so that it does a required function. Eg. In a form if we press CLEAR, it should clear the form and SUBMIT should submit the form. Ruby’s blocks are a convenient way to do this.&lt;br /&gt;
We assume that on pressing the button a callback method, button_pressed, will be invoked. The obvious way of adding functionality to these buttons is to create subclasses of Button(which is the class) and have each subclass implement its own button_pressed method.&lt;br /&gt;
Eg. &lt;br /&gt;
 class FormButton &amp;lt; Button&lt;br /&gt;
     def initialize&lt;br /&gt;
         super(&amp;quot;Submit&amp;quot;) # invoke Button's initialize&lt;br /&gt;
        end&lt;br /&gt;
    def button_pressed&lt;br /&gt;
    # do submit actions...&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
submit_button = FormButton.new&lt;br /&gt;
This will lead to a large number of subclasses. We can fix this problem using blocks in RUBY.&lt;br /&gt;
pressbutton = PressButton.new&lt;br /&gt;
   class FormButton &amp;lt; Button&lt;br /&gt;
     def initialize(label, &amp;amp;action)&lt;br /&gt;
        super(label)&lt;br /&gt;
        @action = action&lt;br /&gt;
     end&lt;br /&gt;
     def button_pressed&lt;br /&gt;
       @action.call(self)&lt;br /&gt;
     end&lt;br /&gt;
  end&lt;br /&gt;
submit_button = FormButton.new(&amp;quot;Submit&amp;quot;) { pressbutton.submit }&lt;br /&gt;
reset_button = FormButton.new(&amp;quot;Clear&amp;quot;) { pressbutton.reset }&lt;br /&gt;
&lt;br /&gt;
The key to all this is the second parameter to FormButton#initialize. If the last parameter in a method definition is prefixed with an ampersand (such as &amp;amp;action), Ruby looks for a code block whenever that method is called. That code block is converted to an object of class Proc and assigned to the parameter. You can then treat the parameter as any other variable. When the callback method button_pressed is invoked, we use the&lt;br /&gt;
Proc#call method on that object to invoke the block.&lt;br /&gt;
&lt;br /&gt;
Now if we want to do the same task in Smalltalk,&lt;/div&gt;</summary>
		<author><name>Tmakhij</name></author>
	</entry>
</feed>