<?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=Anadath</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=Anadath"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Anadath"/>
	<updated>2026-05-18T17:57:33Z</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_2010/ch7_4e_ak&amp;diff=43293</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43293"/>
		<updated>2010-12-02T04:35:30Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Simplicity */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
Throughout the rest of the text, we will see what a prototype based programming language is, its design goals, the broad list of features provided by such a language and information about some of the most successful 'prototype' based programming languages.&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that&amp;lt;blockquote&amp;gt;''Avoid classes. They are just too hard''&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
[[Image:clone.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, even though ''ClonedPerson'' was derived from ''OriginalPerson'', the OriginalPerson's name did not change even after we changed the 'Name' property of ClonedPerson to 'Clone1'. So they are not connected in anyway.&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
Delegation is one of the most important feature provided by a prototype-based programming language. It is a way of forwarding a request if the current object is not able to handle it. There are two types of delegations.&lt;br /&gt;
&lt;br /&gt;
# '''Implicit Delegation''': This is due to the inheritance mechanisms provided by a prototype-based programming language. When an object 'A' extends another object 'B', and if A has a method 'printHello()', B inherits it. In the sense, if B.printHello() is invoked, the interpreter, walks up the extension hierarchy and finds the correct printHello() method on object 'A' (which in this case is the parent of 'B')&lt;br /&gt;
&lt;br /&gt;
# '''Explicit Delegation''': A delegation is explicit if an object explicitly delegates a request to another object. Consider the example below:&lt;br /&gt;
      &lt;br /&gt;
        function buttonClicked(button){&lt;br /&gt;
            alert('you clicked ok button!');&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        $(':button#ok_button').click(function(){&lt;br /&gt;
             buttonClicked();&lt;br /&gt;
         });&lt;br /&gt;
&lt;br /&gt;
The above example uses [http://jquery.com/ jQuery] to add a click listener to a button with id 'ok_button'. The event listener, on receiving click event in turn invokes 'buttonClicked'. This is a simple explicit delegation example.&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
# '''Properties/Variables''': these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
# '''Methods''': responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
# '''Slots''': Everything including a property/method is a slot. Slots are placeholders which respond to messages. For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value. Likewise, a method also responds to messages.&lt;br /&gt;
# '''Keep them separate''': Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
[[Image:traits.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, both ''print'' and ''println'' are methods in the trait and the objects ''ConsoleWriter'' and ''FileWriter'' make use of those methods. Only the property ''out'' changes.&lt;br /&gt;
&lt;br /&gt;
==Languages==&lt;br /&gt;
===JavaScript===&lt;br /&gt;
One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including [http://firefox.com Firefox], [http://www.google.com/chrome Google Chrome], [http://www.opera.com/ Opera]). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. [http://jquery.com/ jQuery], [http://www.prototypejs.org/ PrototypeJS], [http://mootools.net/ Moo-tools] are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
===Self===&lt;br /&gt;
It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Marcus Arnstrom, Mikael Christiansen, Daniel Sehlberg (May 2003),  ''Prototype-based programming''.&lt;br /&gt;
# Borning A (1986), ''Classes versus Prototypes in Object-Oriented Languages'', IEEE Computer Society Press, [http://portal.acm.org/citation.cfm?id=324538 In Proceedings of the IEEE/ACM Fall Joint Conference].&lt;br /&gt;
# Wikipedia's Prototype article[http://en.wikipedia.org/wiki/Prototype-based_programming]&lt;br /&gt;
# Wikipedia's Self Language article[http://en.wikipedia.org/wiki/Self_(programming_language)]&lt;br /&gt;
# Wikipedia's JavaScript article[http://en.wikipedia.org/wiki/JavaScript]&lt;br /&gt;
# IO Language[http://www.iolanguage.com/]&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43290</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43290"/>
		<updated>2010-12-02T04:33:26Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Simplicity */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
Throughout the rest of the text, we will see what a prototype based programming language is, its design goals, the broad list of features provided by such a language and information about some of the most successful 'prototype' based programming languages.&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
          ::::''Avoid classes. They are just too hard''&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
[[Image:clone.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, even though ''ClonedPerson'' was derived from ''OriginalPerson'', the OriginalPerson's name did not change even after we changed the 'Name' property of ClonedPerson to 'Clone1'. So they are not connected in anyway.&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
Delegation is one of the most important feature provided by a prototype-based programming language. It is a way of forwarding a request if the current object is not able to handle it. There are two types of delegations.&lt;br /&gt;
&lt;br /&gt;
# '''Implicit Delegation''': This is due to the inheritance mechanisms provided by a prototype-based programming language. When an object 'A' extends another object 'B', and if A has a method 'printHello()', B inherits it. In the sense, if B.printHello() is invoked, the interpreter, walks up the extension hierarchy and finds the correct printHello() method on object 'A' (which in this case is the parent of 'B')&lt;br /&gt;
&lt;br /&gt;
# '''Explicit Delegation''': A delegation is explicit if an object explicitly delegates a request to another object. Consider the example below:&lt;br /&gt;
      &lt;br /&gt;
        function buttonClicked(button){&lt;br /&gt;
            alert('you clicked ok button!');&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        $(':button#ok_button').click(function(){&lt;br /&gt;
             buttonClicked();&lt;br /&gt;
         });&lt;br /&gt;
&lt;br /&gt;
The above example uses [http://jquery.com/ jQuery] to add a click listener to a button with id 'ok_button'. The event listener, on receiving click event in turn invokes 'buttonClicked'. This is a simple explicit delegation example.&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
# '''Properties/Variables''': these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
# '''Methods''': responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
# '''Slots''': Everything including a property/method is a slot. Slots are placeholders which respond to messages. For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value. Likewise, a method also responds to messages.&lt;br /&gt;
# '''Keep them separate''': Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
[[Image:traits.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, both ''print'' and ''println'' are methods in the trait and the objects ''ConsoleWriter'' and ''FileWriter'' make use of those methods. Only the property ''out'' changes.&lt;br /&gt;
&lt;br /&gt;
==Languages==&lt;br /&gt;
===JavaScript===&lt;br /&gt;
One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including [http://firefox.com Firefox], [http://www.google.com/chrome Google Chrome], [http://www.opera.com/ Opera]). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. [http://jquery.com/ jQuery], [http://www.prototypejs.org/ PrototypeJS], [http://mootools.net/ Moo-tools] are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
===Self===&lt;br /&gt;
It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Marcus Arnstrom, Mikael Christiansen, Daniel Sehlberg (May 2003),  ''Prototype-based programming''.&lt;br /&gt;
# Borning A (1986), ''Classes versus Prototypes in Object-Oriented Languages'', IEEE Computer Society Press, [http://portal.acm.org/citation.cfm?id=324538 In Proceedings of the IEEE/ACM Fall Joint Conference].&lt;br /&gt;
# Wikipedia's Prototype article[http://en.wikipedia.org/wiki/Prototype-based_programming]&lt;br /&gt;
# Wikipedia's Self Language article[http://en.wikipedia.org/wiki/Self_(programming_language)]&lt;br /&gt;
# Wikipedia's JavaScript article[http://en.wikipedia.org/wiki/JavaScript]&lt;br /&gt;
# IO Language[http://www.iolanguage.com/]&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43289</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43289"/>
		<updated>2010-12-02T04:32:54Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Simplicity */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
Throughout the rest of the text, we will see what a prototype based programming language is, its design goals, the broad list of features provided by such a language and information about some of the most successful 'prototype' based programming languages.&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that&lt;br /&gt;
&lt;br /&gt;
          ''Avoid classes. They are just too hard''&lt;br /&gt;
&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
[[Image:clone.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, even though ''ClonedPerson'' was derived from ''OriginalPerson'', the OriginalPerson's name did not change even after we changed the 'Name' property of ClonedPerson to 'Clone1'. So they are not connected in anyway.&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
Delegation is one of the most important feature provided by a prototype-based programming language. It is a way of forwarding a request if the current object is not able to handle it. There are two types of delegations.&lt;br /&gt;
&lt;br /&gt;
# '''Implicit Delegation''': This is due to the inheritance mechanisms provided by a prototype-based programming language. When an object 'A' extends another object 'B', and if A has a method 'printHello()', B inherits it. In the sense, if B.printHello() is invoked, the interpreter, walks up the extension hierarchy and finds the correct printHello() method on object 'A' (which in this case is the parent of 'B')&lt;br /&gt;
&lt;br /&gt;
# '''Explicit Delegation''': A delegation is explicit if an object explicitly delegates a request to another object. Consider the example below:&lt;br /&gt;
      &lt;br /&gt;
        function buttonClicked(button){&lt;br /&gt;
            alert('you clicked ok button!');&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        $(':button#ok_button').click(function(){&lt;br /&gt;
             buttonClicked();&lt;br /&gt;
         });&lt;br /&gt;
&lt;br /&gt;
The above example uses [http://jquery.com/ jQuery] to add a click listener to a button with id 'ok_button'. The event listener, on receiving click event in turn invokes 'buttonClicked'. This is a simple explicit delegation example.&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
# '''Properties/Variables''': these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
# '''Methods''': responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
# '''Slots''': Everything including a property/method is a slot. Slots are placeholders which respond to messages. For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value. Likewise, a method also responds to messages.&lt;br /&gt;
# '''Keep them separate''': Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
[[Image:traits.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, both ''print'' and ''println'' are methods in the trait and the objects ''ConsoleWriter'' and ''FileWriter'' make use of those methods. Only the property ''out'' changes.&lt;br /&gt;
&lt;br /&gt;
==Languages==&lt;br /&gt;
===JavaScript===&lt;br /&gt;
One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including [http://firefox.com Firefox], [http://www.google.com/chrome Google Chrome], [http://www.opera.com/ Opera]). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. [http://jquery.com/ jQuery], [http://www.prototypejs.org/ PrototypeJS], [http://mootools.net/ Moo-tools] are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
===Self===&lt;br /&gt;
It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Marcus Arnstrom, Mikael Christiansen, Daniel Sehlberg (May 2003),  ''Prototype-based programming''.&lt;br /&gt;
# Borning A (1986), ''Classes versus Prototypes in Object-Oriented Languages'', IEEE Computer Society Press, [http://portal.acm.org/citation.cfm?id=324538 In Proceedings of the IEEE/ACM Fall Joint Conference].&lt;br /&gt;
# Wikipedia's Prototype article[http://en.wikipedia.org/wiki/Prototype-based_programming]&lt;br /&gt;
# Wikipedia's Self Language article[http://en.wikipedia.org/wiki/Self_(programming_language)]&lt;br /&gt;
# Wikipedia's JavaScript article[http://en.wikipedia.org/wiki/JavaScript]&lt;br /&gt;
# IO Language[http://www.iolanguage.com/]&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43288</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43288"/>
		<updated>2010-12-02T04:32:35Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Simplicity */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
Throughout the rest of the text, we will see what a prototype based programming language is, its design goals, the broad list of features provided by such a language and information about some of the most successful 'prototype' based programming languages.&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&amp;lt;br/&amp;gt;&lt;br /&gt;
          '''Avoid classes. They are just too hard'''&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
[[Image:clone.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, even though ''ClonedPerson'' was derived from ''OriginalPerson'', the OriginalPerson's name did not change even after we changed the 'Name' property of ClonedPerson to 'Clone1'. So they are not connected in anyway.&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
Delegation is one of the most important feature provided by a prototype-based programming language. It is a way of forwarding a request if the current object is not able to handle it. There are two types of delegations.&lt;br /&gt;
&lt;br /&gt;
# '''Implicit Delegation''': This is due to the inheritance mechanisms provided by a prototype-based programming language. When an object 'A' extends another object 'B', and if A has a method 'printHello()', B inherits it. In the sense, if B.printHello() is invoked, the interpreter, walks up the extension hierarchy and finds the correct printHello() method on object 'A' (which in this case is the parent of 'B')&lt;br /&gt;
&lt;br /&gt;
# '''Explicit Delegation''': A delegation is explicit if an object explicitly delegates a request to another object. Consider the example below:&lt;br /&gt;
      &lt;br /&gt;
        function buttonClicked(button){&lt;br /&gt;
            alert('you clicked ok button!');&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        $(':button#ok_button').click(function(){&lt;br /&gt;
             buttonClicked();&lt;br /&gt;
         });&lt;br /&gt;
&lt;br /&gt;
The above example uses [http://jquery.com/ jQuery] to add a click listener to a button with id 'ok_button'. The event listener, on receiving click event in turn invokes 'buttonClicked'. This is a simple explicit delegation example.&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
# '''Properties/Variables''': these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
# '''Methods''': responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
# '''Slots''': Everything including a property/method is a slot. Slots are placeholders which respond to messages. For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value. Likewise, a method also responds to messages.&lt;br /&gt;
# '''Keep them separate''': Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
[[Image:traits.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, both ''print'' and ''println'' are methods in the trait and the objects ''ConsoleWriter'' and ''FileWriter'' make use of those methods. Only the property ''out'' changes.&lt;br /&gt;
&lt;br /&gt;
==Languages==&lt;br /&gt;
===JavaScript===&lt;br /&gt;
One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including [http://firefox.com Firefox], [http://www.google.com/chrome Google Chrome], [http://www.opera.com/ Opera]). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. [http://jquery.com/ jQuery], [http://www.prototypejs.org/ PrototypeJS], [http://mootools.net/ Moo-tools] are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
===Self===&lt;br /&gt;
It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Marcus Arnstrom, Mikael Christiansen, Daniel Sehlberg (May 2003),  ''Prototype-based programming''.&lt;br /&gt;
# Borning A (1986), ''Classes versus Prototypes in Object-Oriented Languages'', IEEE Computer Society Press, [http://portal.acm.org/citation.cfm?id=324538 In Proceedings of the IEEE/ACM Fall Joint Conference].&lt;br /&gt;
# Wikipedia's Prototype article[http://en.wikipedia.org/wiki/Prototype-based_programming]&lt;br /&gt;
# Wikipedia's Self Language article[http://en.wikipedia.org/wiki/Self_(programming_language)]&lt;br /&gt;
# Wikipedia's JavaScript article[http://en.wikipedia.org/wiki/JavaScript]&lt;br /&gt;
# IO Language[http://www.iolanguage.com/]&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43287</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43287"/>
		<updated>2010-12-02T04:32:12Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Simplicity */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
Throughout the rest of the text, we will see what a prototype based programming language is, its design goals, the broad list of features provided by such a language and information about some of the most successful 'prototype' based programming languages.&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&amp;lt;br/&amp;gt;&lt;br /&gt;
          &amp;lt;code&amp;gt;'Avoid classes. They are just too hard'&amp;lt;/code&amp;gt;&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
[[Image:clone.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, even though ''ClonedPerson'' was derived from ''OriginalPerson'', the OriginalPerson's name did not change even after we changed the 'Name' property of ClonedPerson to 'Clone1'. So they are not connected in anyway.&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
Delegation is one of the most important feature provided by a prototype-based programming language. It is a way of forwarding a request if the current object is not able to handle it. There are two types of delegations.&lt;br /&gt;
&lt;br /&gt;
# '''Implicit Delegation''': This is due to the inheritance mechanisms provided by a prototype-based programming language. When an object 'A' extends another object 'B', and if A has a method 'printHello()', B inherits it. In the sense, if B.printHello() is invoked, the interpreter, walks up the extension hierarchy and finds the correct printHello() method on object 'A' (which in this case is the parent of 'B')&lt;br /&gt;
&lt;br /&gt;
# '''Explicit Delegation''': A delegation is explicit if an object explicitly delegates a request to another object. Consider the example below:&lt;br /&gt;
      &lt;br /&gt;
        function buttonClicked(button){&lt;br /&gt;
            alert('you clicked ok button!');&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        $(':button#ok_button').click(function(){&lt;br /&gt;
             buttonClicked();&lt;br /&gt;
         });&lt;br /&gt;
&lt;br /&gt;
The above example uses [http://jquery.com/ jQuery] to add a click listener to a button with id 'ok_button'. The event listener, on receiving click event in turn invokes 'buttonClicked'. This is a simple explicit delegation example.&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
# '''Properties/Variables''': these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
# '''Methods''': responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
# '''Slots''': Everything including a property/method is a slot. Slots are placeholders which respond to messages. For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value. Likewise, a method also responds to messages.&lt;br /&gt;
# '''Keep them separate''': Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
[[Image:traits.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, both ''print'' and ''println'' are methods in the trait and the objects ''ConsoleWriter'' and ''FileWriter'' make use of those methods. Only the property ''out'' changes.&lt;br /&gt;
&lt;br /&gt;
==Languages==&lt;br /&gt;
===JavaScript===&lt;br /&gt;
One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including [http://firefox.com Firefox], [http://www.google.com/chrome Google Chrome], [http://www.opera.com/ Opera]). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. [http://jquery.com/ jQuery], [http://www.prototypejs.org/ PrototypeJS], [http://mootools.net/ Moo-tools] are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
===Self===&lt;br /&gt;
It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Marcus Arnstrom, Mikael Christiansen, Daniel Sehlberg (May 2003),  ''Prototype-based programming''.&lt;br /&gt;
# Borning A (1986), ''Classes versus Prototypes in Object-Oriented Languages'', IEEE Computer Society Press, [http://portal.acm.org/citation.cfm?id=324538 In Proceedings of the IEEE/ACM Fall Joint Conference].&lt;br /&gt;
# Wikipedia's Prototype article[http://en.wikipedia.org/wiki/Prototype-based_programming]&lt;br /&gt;
# Wikipedia's Self Language article[http://en.wikipedia.org/wiki/Self_(programming_language)]&lt;br /&gt;
# Wikipedia's JavaScript article[http://en.wikipedia.org/wiki/JavaScript]&lt;br /&gt;
# IO Language[http://www.iolanguage.com/]&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43284</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43284"/>
		<updated>2010-12-02T04:28:16Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* JavaScript */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
Throughout the rest of the text, we will see what a prototype based programming language is, its design goals, the broad list of features provided by such a language and information about some of the most successful 'prototype' based programming languages.&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
[[Image:clone.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, even though ''ClonedPerson'' was derived from ''OriginalPerson'', the OriginalPerson's name did not change even after we changed the 'Name' property of ClonedPerson to 'Clone1'. So they are not connected in anyway.&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
Delegation is one of the most important feature provided by a prototype-based programming language. It is a way of forwarding a request if the current object is not able to handle it. There are two types of delegations.&lt;br /&gt;
&lt;br /&gt;
# '''Implicit Delegation''': This is due to the inheritance mechanisms provided by a prototype-based programming language. When an object 'A' extends another object 'B', and if A has a method 'printHello()', B inherits it. In the sense, if B.printHello() is invoked, the interpreter, walks up the extension hierarchy and finds the correct printHello() method on object 'A' (which in this case is the parent of 'B')&lt;br /&gt;
&lt;br /&gt;
# '''Explicit Delegation''': A delegation is explicit if an object explicitly delegates a request to another object. Consider the example below:&lt;br /&gt;
      &lt;br /&gt;
        function buttonClicked(button){&lt;br /&gt;
            alert('you clicked ok button!');&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        $(':button#ok_button').click(function(){&lt;br /&gt;
             buttonClicked();&lt;br /&gt;
         });&lt;br /&gt;
&lt;br /&gt;
The above example uses [http://jquery.com/ jQuery] to add a click listener to a button with id 'ok_button'. The event listener, on receiving click event in turn invokes 'buttonClicked'. This is a simple explicit delegation example.&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
# '''Properties/Variables''': these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
# '''Methods''': responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
# '''Slots''': Everything including a property/method is a slot. Slots are placeholders which respond to messages. For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value. Likewise, a method also responds to messages.&lt;br /&gt;
# '''Keep them separate''': Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
[[Image:traits.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, both ''print'' and ''println'' are methods in the trait and the objects ''ConsoleWriter'' and ''FileWriter'' make use of those methods. Only the property ''out'' changes.&lt;br /&gt;
&lt;br /&gt;
==Languages==&lt;br /&gt;
===JavaScript===&lt;br /&gt;
One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including [http://firefox.com Firefox], [http://www.google.com/chrome Google Chrome], [http://www.opera.com/ Opera]). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. [http://jquery.com/ jQuery], [http://www.prototypejs.org/ PrototypeJS], [http://mootools.net/ Moo-tools] are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
===Self===&lt;br /&gt;
It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Marcus Arnstrom, Mikael Christiansen, Daniel Sehlberg (May 2003),  ''Prototype-based programming''.&lt;br /&gt;
# Borning A (1986), ''Classes versus Prototypes in Object-Oriented Languages'', IEEE Computer Society Press, [http://portal.acm.org/citation.cfm?id=324538 In Proceedings of the IEEE/ACM Fall Joint Conference].&lt;br /&gt;
# Wikipedia's Prototype article[http://en.wikipedia.org/wiki/Prototype-based_programming]&lt;br /&gt;
# Wikipedia's Self Language article[http://en.wikipedia.org/wiki/Self_(programming_language)]&lt;br /&gt;
# Wikipedia's JavaScript article[http://en.wikipedia.org/wiki/JavaScript]&lt;br /&gt;
# IO Language[http://www.iolanguage.com/]&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43282</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43282"/>
		<updated>2010-12-02T04:25:50Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Delegation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
Throughout the rest of the text, we will see what a prototype based programming language is, its design goals, the broad list of features provided by such a language and information about some of the most successful 'prototype' based programming languages.&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
[[Image:clone.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, even though ''ClonedPerson'' was derived from ''OriginalPerson'', the OriginalPerson's name did not change even after we changed the 'Name' property of ClonedPerson to 'Clone1'. So they are not connected in anyway.&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
Delegation is one of the most important feature provided by a prototype-based programming language. It is a way of forwarding a request if the current object is not able to handle it. There are two types of delegations.&lt;br /&gt;
&lt;br /&gt;
# '''Implicit Delegation''': This is due to the inheritance mechanisms provided by a prototype-based programming language. When an object 'A' extends another object 'B', and if A has a method 'printHello()', B inherits it. In the sense, if B.printHello() is invoked, the interpreter, walks up the extension hierarchy and finds the correct printHello() method on object 'A' (which in this case is the parent of 'B')&lt;br /&gt;
&lt;br /&gt;
# '''Explicit Delegation''': A delegation is explicit if an object explicitly delegates a request to another object. Consider the example below:&lt;br /&gt;
      &lt;br /&gt;
        function buttonClicked(button){&lt;br /&gt;
            alert('you clicked ok button!');&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        $(':button#ok_button').click(function(){&lt;br /&gt;
             buttonClicked();&lt;br /&gt;
         });&lt;br /&gt;
&lt;br /&gt;
The above example uses [http://jquery.com/ jQuery] to add a click listener to a button with id 'ok_button'. The event listener, on receiving click event in turn invokes 'buttonClicked'. This is a simple explicit delegation example.&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
# '''Properties/Variables''': these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
# '''Methods''': responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
# '''Slots''': Everything including a property/method is a slot. Slots are placeholders which respond to messages. For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value. Likewise, a method also responds to messages.&lt;br /&gt;
# '''Keep them separate''': Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
[[Image:traits.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, both ''print'' and ''println'' are methods in the trait and the objects ''ConsoleWriter'' and ''FileWriter'' make use of those methods. Only the property ''out'' changes.&lt;br /&gt;
&lt;br /&gt;
==Languages==&lt;br /&gt;
===JavaScript===&lt;br /&gt;
One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
===Self===&lt;br /&gt;
It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Marcus Arnstrom, Mikael Christiansen, Daniel Sehlberg (May 2003),  ''Prototype-based programming''.&lt;br /&gt;
# Borning A (1986), ''Classes versus Prototypes in Object-Oriented Languages'', IEEE Computer Society Press, [http://portal.acm.org/citation.cfm?id=324538 In Proceedings of the IEEE/ACM Fall Joint Conference].&lt;br /&gt;
# Wikipedia's Prototype article[http://en.wikipedia.org/wiki/Prototype-based_programming]&lt;br /&gt;
# Wikipedia's Self Language article[http://en.wikipedia.org/wiki/Self_(programming_language)]&lt;br /&gt;
# Wikipedia's JavaScript article[http://en.wikipedia.org/wiki/JavaScript]&lt;br /&gt;
# IO Language[http://www.iolanguage.com/]&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43279</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43279"/>
		<updated>2010-12-02T04:23:04Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Concepts of creation and representation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
Throughout the rest of the text, we will see what a prototype based programming language is, its design goals, the broad list of features provided by such a language and information about some of the most successful 'prototype' based programming languages.&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
[[Image:clone.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, even though ''ClonedPerson'' was derived from ''OriginalPerson'', the OriginalPerson's name did not change even after we changed the 'Name' property of ClonedPerson to 'Clone1'. So they are not connected in anyway.&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
===Delegation===&lt;br /&gt;
Delegation is one of the most important feature provided by a prototype-based programming language. It is a way of forwarding a request if the current object is not able to handle it. There are two types of delegations.&lt;br /&gt;
&lt;br /&gt;
# '''Implicit Delegation''': This is due to the inheritance mechanisms provided by a prototype-based programming language. When an object 'A' extends another object 'B', and if A has a method 'printHello()', B inherits it. In the sense, if B.printHello() is invoked, the interpreter, walks up the extension hierarchy and finds the correct printHello() method on object 'A' (which in this case is the parent of 'B')&lt;br /&gt;
&lt;br /&gt;
# '''Explicit Delegation''': A delegation is explicit if an object explicitly delegates a request to another object. Consider the example below:&lt;br /&gt;
      &lt;br /&gt;
        function buttonClicked(button){&lt;br /&gt;
            alert('you clicked ok button!');&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        $(':button#ok_button').click(function(){&lt;br /&gt;
             buttonClicked();&lt;br /&gt;
         });&lt;br /&gt;
&lt;br /&gt;
The above example uses jQuery to add a click listener to a button with id 'ok_button'. The event listener, on receiving click event in turn invokes 'buttonClicked'. This is a simple explicit delegation example.&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
# '''Properties/Variables''': these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
# '''Methods''': responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
# '''Slots''': Everything including a property/method is a slot. Slots are placeholders which respond to messages. For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value. Likewise, a method also responds to messages.&lt;br /&gt;
# '''Keep them separate''': Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
[[Image:traits.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, both ''print'' and ''println'' are methods in the trait and the objects ''ConsoleWriter'' and ''FileWriter'' make use of those methods. Only the property ''out'' changes.&lt;br /&gt;
&lt;br /&gt;
==Languages==&lt;br /&gt;
===JavaScript===&lt;br /&gt;
One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
===Self===&lt;br /&gt;
It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Marcus Arnstrom, Mikael Christiansen, Daniel Sehlberg (May 2003),  ''Prototype-based programming''.&lt;br /&gt;
# Borning A (1986), ''Classes versus Prototypes in Object-Oriented Languages'', IEEE Computer Society Press, [http://portal.acm.org/citation.cfm?id=324538 In Proceedings of the IEEE/ACM Fall Joint Conference].&lt;br /&gt;
# Wikipedia's Prototype article[http://en.wikipedia.org/wiki/Prototype-based_programming]&lt;br /&gt;
# Wikipedia's Self Language article[http://en.wikipedia.org/wiki/Self_(programming_language)]&lt;br /&gt;
# Wikipedia's JavaScript article[http://en.wikipedia.org/wiki/JavaScript]&lt;br /&gt;
# IO Language[http://www.iolanguage.com/]&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43270</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43270"/>
		<updated>2010-12-02T04:12:26Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
Throughout the rest of the text, we will see what a prototype based programming language is, its design goals, the broad list of features provided by such a language and information about some of the most successful 'prototype' based programming languages.&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
[[Image:clone.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, even though ''ClonedPerson'' was derived from ''OriginalPerson'', the OriginalPerson's name did not change even after we changed the 'Name' property of ClonedPerson to 'Clone1'. So they are not connected in anyway.&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
# '''Properties/Variables''': these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
# '''Methods''': responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
# '''Slots''': Everything including a property/method is a slot. Slots are placeholders which respond to messages. For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value. Likewise, a method also responds to messages.&lt;br /&gt;
# '''Keep them separate''': Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
[[Image:traits.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, both ''print'' and ''println'' are methods in the trait and the objects ''ConsoleWriter'' and ''FileWriter'' make use of those methods. Only the property ''out'' changes.&lt;br /&gt;
&lt;br /&gt;
==Languages==&lt;br /&gt;
===JavaScript===&lt;br /&gt;
One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
===Self===&lt;br /&gt;
It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Marcus Arnstrom, Mikael Christiansen, Daniel Sehlberg (May 2003),  ''Prototype-based programming''.&lt;br /&gt;
# Borning A (1986), ''Classes versus Prototypes in Object-Oriented Languages'', IEEE Computer Society Press, [http://portal.acm.org/citation.cfm?id=324538 In Proceedings of the IEEE/ACM Fall Joint Conference].&lt;br /&gt;
# Wikipedia's Prototype article[http://en.wikipedia.org/wiki/Prototype-based_programming]&lt;br /&gt;
# Wikipedia's Self Language article[http://en.wikipedia.org/wiki/Self_(programming_language)]&lt;br /&gt;
# Wikipedia's JavaScript article[http://en.wikipedia.org/wiki/JavaScript]&lt;br /&gt;
# IO Language[http://www.iolanguage.com/]&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43268</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43268"/>
		<updated>2010-12-02T04:11:00Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
Throughout the rest of the text, we will see what a prototype based programming language is, its design goals, the broad list of features provided by such a language and information about some of the most successful 'prototype' based programming languages.&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
[[Image:clone.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, even though ''ClonedPerson'' was derived from ''OriginalPerson'', the OriginalPerson's name did not change even after we changed the 'Name' property of ClonedPerson to 'Clone1'. So they are not connected in anyway.&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
# '''Properties/Variables''': these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
# '''Methods''': responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
# '''Slots''': Everything including a property/method is a slot. Slots are placeholders which respond to messages. For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value. Likewise, a method also responds to messages.&lt;br /&gt;
# '''Keep them separate''': Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
[[Image:traits.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, both ''print'' and ''println'' are methods in the trait and the objects ''ConsoleWriter'' and ''FileWriter'' make use of those methods. Only the property ''out'' changes.&lt;br /&gt;
&lt;br /&gt;
==Languages==&lt;br /&gt;
===JavaScript===&lt;br /&gt;
One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
===Self===&lt;br /&gt;
It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
# Marcus Arnstrom, Mikael Christiansen, Daniel Sehlberg (May 2003),  ''Prototype-based programming''.&lt;br /&gt;
# Borning A (1986), ''Classes versus Prototypes in Object-Oriented Languages'', IEEE Computer Society Press, [http://portal.acm.org/citation.cfm?id=324538 In Proceedings of the IEEE/ACM Fall Joint Conference].&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Prototype-based_programming]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/Self_(programming_language)]&lt;br /&gt;
# [http://en.wikipedia.org/wiki/JavaScript]&lt;br /&gt;
# [http://c2.com/cgi/wiki?PrototypeBasedProgramming]&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43263</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43263"/>
		<updated>2010-12-02T04:08:25Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Prototype-based programming - Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
Throughout the rest of the text, we will see what a prototype based programming language is, its design goals, the broad list of features provided by such a language and information about some of the most successful 'prototype' based programming languages.&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
[[Image:clone.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, even though ''ClonedPerson'' was derived from ''OriginalPerson'', the OriginalPerson's name did not change even after we changed the 'Name' property of ClonedPerson to 'Clone1'. So they are not connected in anyway.&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
# '''Properties/Variables''': these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
# '''Methods''': responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
# '''Slots''': Everything including a property/method is a slot. Slots are placeholders which respond to messages. For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value. Likewise, a method also responds to messages.&lt;br /&gt;
# '''Keep them separate''': Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
[[Image:traits.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, both ''print'' and ''println'' are methods in the trait and the objects ''ConsoleWriter'' and ''FileWriter'' make use of those methods. Only the property ''out'' changes.&lt;br /&gt;
&lt;br /&gt;
==Languages==&lt;br /&gt;
===JavaScript===&lt;br /&gt;
One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
===Self===&lt;br /&gt;
It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43261</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43261"/>
		<updated>2010-12-02T04:03:26Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Extending an already existing one */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
[[Image:clone.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, even though ''ClonedPerson'' was derived from ''OriginalPerson'', the OriginalPerson's name did not change even after we changed the 'Name' property of ClonedPerson to 'Clone1'. So they are not connected in anyway.&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
# '''Properties/Variables''': these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
# '''Methods''': responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
# '''Slots''': Everything including a property/method is a slot. Slots are placeholders which respond to messages. For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value. Likewise, a method also responds to messages.&lt;br /&gt;
# '''Keep them separate''': Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
[[Image:traits.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, both ''print'' and ''println'' are methods in the trait and the objects ''ConsoleWriter'' and ''FileWriter'' make use of those methods. Only the property ''out'' changes.&lt;br /&gt;
&lt;br /&gt;
==Languages==&lt;br /&gt;
===JavaScript===&lt;br /&gt;
One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
===Self===&lt;br /&gt;
It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Clone.png&amp;diff=43260</id>
		<title>File:Clone.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Clone.png&amp;diff=43260"/>
		<updated>2010-12-02T04:02:24Z</updated>

		<summary type="html">&lt;p&gt;Anadath: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43259</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43259"/>
		<updated>2010-12-02T04:01:59Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Extending an already existing one */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
[[Image:clone1.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, even though ''ClonedPerson'' was derived from ''OriginalPerson'', the OriginalPerson's name did not change even after we changed the 'Name' property of ClonedPerson to 'Clone1'. So they are not connected in anyway.&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
# '''Properties/Variables''': these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
# '''Methods''': responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
# '''Slots''': Everything including a property/method is a slot. Slots are placeholders which respond to messages. For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value. Likewise, a method also responds to messages.&lt;br /&gt;
# '''Keep them separate''': Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
[[Image:traits.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, both ''print'' and ''println'' are methods in the trait and the objects ''ConsoleWriter'' and ''FileWriter'' make use of those methods. Only the property ''out'' changes.&lt;br /&gt;
&lt;br /&gt;
==Languages==&lt;br /&gt;
===JavaScript===&lt;br /&gt;
One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
===Self===&lt;br /&gt;
It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43256</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43256"/>
		<updated>2010-12-02T04:01:31Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Extending an already existing one */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
[[Image:traits.png]]&lt;br /&gt;
In the above image, even though ''ClonedPerson'' was derived from ''OriginalPerson'', the OriginalPerson's name did not change even after we changed the 'Name' property of ClonedPerson to 'Clone1'. So they are not connected in anyway.&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
# '''Properties/Variables''': these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
# '''Methods''': responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
# '''Slots''': Everything including a property/method is a slot. Slots are placeholders which respond to messages. For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value. Likewise, a method also responds to messages.&lt;br /&gt;
# '''Keep them separate''': Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
[[Image:traits.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, both ''print'' and ''println'' are methods in the trait and the objects ''ConsoleWriter'' and ''FileWriter'' make use of those methods. Only the property ''out'' changes.&lt;br /&gt;
&lt;br /&gt;
==Languages==&lt;br /&gt;
===JavaScript===&lt;br /&gt;
One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
===Self===&lt;br /&gt;
It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43248</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43248"/>
		<updated>2010-12-02T03:51:01Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Concepts of representations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
# '''Properties/Variables''': these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
# '''Methods''': responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
# '''Slots''': Everything including a property/method is a slot. Slots are placeholders which respond to messages. For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value. Likewise, a method also responds to messages.&lt;br /&gt;
# '''Keep them separate''': Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
[[Image:traits.png]]&lt;br /&gt;
&lt;br /&gt;
In the above image, both ''print'' and ''println'' are methods in the trait and the objects ''ConsoleWriter'' and ''FileWriter'' make use of those methods. Only the property ''out'' changes.&lt;br /&gt;
&lt;br /&gt;
==Languages==&lt;br /&gt;
===JavaScript===&lt;br /&gt;
One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
===Self===&lt;br /&gt;
It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43245</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43245"/>
		<updated>2010-12-02T03:48:43Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Concepts of representations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
# '''Properties/Variables''': these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
# '''Methods''': responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
# '''Slots''': Everything including a property/method is a slot. Slots are placeholders which respond to messages. For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value. Likewise, a method also responds to messages.&lt;br /&gt;
# '''Keep them separate''': Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
[[Image:traits.png]]&lt;br /&gt;
&lt;br /&gt;
==Languages==&lt;br /&gt;
===JavaScript===&lt;br /&gt;
One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
===Self===&lt;br /&gt;
It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:Traits.png&amp;diff=43242</id>
		<title>File:Traits.png</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:Traits.png&amp;diff=43242"/>
		<updated>2010-12-02T03:45:58Z</updated>

		<summary type="html">&lt;p&gt;Anadath: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43241</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43241"/>
		<updated>2010-12-02T03:45:09Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Concepts of representations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
# '''Properties/Variables''': these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
# '''Methods''': responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
# '''Slots''': Everything including a property/method is a slot. Slots are placeholders which respond to messages. For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value. Likewise, a method also responds to messages.&lt;br /&gt;
# '''Keep them separate''': Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          [[Image:traits.jpg]]&lt;br /&gt;
&lt;br /&gt;
==Languages==&lt;br /&gt;
===JavaScript===&lt;br /&gt;
One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
===Self===&lt;br /&gt;
It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43156</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43156"/>
		<updated>2010-12-02T02:30:20Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Concepts of representations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
# '''Properties/Variables''': these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
# '''Methods''': responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
# '''Slots''': Everything including a property/method is a slot. Slots are placeholders which respond to messages. For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value. Likewise, a method also responds to messages.&lt;br /&gt;
# '''Keep them separate''': Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          //FIGURE HERE:&lt;br /&gt;
&lt;br /&gt;
==Languages==&lt;br /&gt;
===JavaScript===&lt;br /&gt;
One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
===Self===&lt;br /&gt;
It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43153</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43153"/>
		<updated>2010-12-02T02:29:39Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Concepts of representations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
# '''Properties/Variables''': these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
# '''Methods''': responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
# '''Slots''': Everything including a property/method is a slot. Slots are placeholders which respond to messages. For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value. Likewise, a method also responds to messages.&lt;br /&gt;
# '''Keep them separate''':&lt;br /&gt;
Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          //FIGURE HERE:&lt;br /&gt;
&lt;br /&gt;
==Languages==&lt;br /&gt;
===JavaScript===&lt;br /&gt;
One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
===Self===&lt;br /&gt;
It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
===Lua===&lt;br /&gt;
One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
==Drawbacks==&lt;br /&gt;
Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43151</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43151"/>
		<updated>2010-12-02T02:27:04Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Concepts of representations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
# '''Properties/Variables''': these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
# '''Methods''': responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
# '''Slots''': Everything including a property/method is a slot. Slots are placeholders which respond to messages. For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value.&lt;br /&gt;
Likewise, a method also responds to messages.&lt;br /&gt;
# '''Keep them separate''':&lt;br /&gt;
Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          //FIGURE HERE:&lt;br /&gt;
&lt;br /&gt;
4. Languages:&lt;br /&gt;
    JavaScript:&lt;br /&gt;
      One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
    Self:&lt;br /&gt;
      It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
   Lua:&lt;br /&gt;
      One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
5. Drawbacks:&lt;br /&gt;
    Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
6. Conclusion:&lt;br /&gt;
    Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
7. References:&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43150</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43150"/>
		<updated>2010-12-02T02:24:22Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Add/Remove methods to/from an object/prototype */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
For example, to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
===Concepts of representations===&lt;br /&gt;
Objects compose two types of primitives:&lt;br /&gt;
        1. Properties/Variables: these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
        2. Methods: responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
      The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
&lt;br /&gt;
        1. Slots: Everything including a property/method is a slot. Slots are placeholders which respond to messages.&lt;br /&gt;
          For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value.&lt;br /&gt;
&lt;br /&gt;
          Likewise, a method also responds to messages.&lt;br /&gt;
&lt;br /&gt;
        2. Keep them separate:&lt;br /&gt;
            Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          //FIGURE HERE:&lt;br /&gt;
&lt;br /&gt;
4. Languages:&lt;br /&gt;
    JavaScript:&lt;br /&gt;
      One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
    Self:&lt;br /&gt;
      It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
   Lua:&lt;br /&gt;
      One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
5. Drawbacks:&lt;br /&gt;
    Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
6. Conclusion:&lt;br /&gt;
    Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
7. References:&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43147</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43147"/>
		<updated>2010-12-02T02:20:36Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Extending an already existing one */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
:: For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
===Modification===&lt;br /&gt;
Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
====Add/Remove new attributes to an object/prototype====&lt;br /&gt;
For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
to add a new property 'age' to person,&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
and to delete the property age, its as simple as:&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
====Change the value of an attribute====&lt;br /&gt;
For example assume,&lt;br /&gt;
        Person = {name: 'Abcd', age: 99 }&lt;br /&gt;
To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
====Add/Remove methods to/from an object/prototype====&lt;br /&gt;
        For example,&lt;br /&gt;
          to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
&lt;br /&gt;
          to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
  The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.3 Concepts of representations:&lt;br /&gt;
      Objects compose two types of primitives:&lt;br /&gt;
        1. Properties/Variables: these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
        2. Methods: responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
      The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
&lt;br /&gt;
        1. Slots: Everything including a property/method is a slot. Slots are placeholders which respond to messages.&lt;br /&gt;
          For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value.&lt;br /&gt;
&lt;br /&gt;
          Likewise, a method also responds to messages.&lt;br /&gt;
&lt;br /&gt;
        2. Keep them separate:&lt;br /&gt;
            Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          //FIGURE HERE:&lt;br /&gt;
&lt;br /&gt;
4. Languages:&lt;br /&gt;
    JavaScript:&lt;br /&gt;
      One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
    Self:&lt;br /&gt;
      It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
   Lua:&lt;br /&gt;
      One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
5. Drawbacks:&lt;br /&gt;
    Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
6. Conclusion:&lt;br /&gt;
    Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
7. References:&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43146</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43146"/>
		<updated>2010-12-02T02:15:59Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Creation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# Ex nihilo&lt;br /&gt;
# Extending an already existing prototype&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
&amp;lt;br/&amp;gt;&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
&lt;br /&gt;
        For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
      But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.2 Modification:&lt;br /&gt;
    Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
&lt;br /&gt;
    1. Add/Remove new attributes to an object/prototype.&lt;br /&gt;
      For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
&lt;br /&gt;
      to add a new property 'age' to person,&lt;br /&gt;
&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
      and to delete the property age, its as simple as:&lt;br /&gt;
&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
    2. Change the value of an attribute&lt;br /&gt;
      For example,&lt;br /&gt;
        assume Person = {name: 'Abcd', age: 99 }.&lt;br /&gt;
&lt;br /&gt;
      To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
    3. Add/Remove methods to/from an object/prototype&lt;br /&gt;
        For example,&lt;br /&gt;
          to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
&lt;br /&gt;
          to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
  The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.3 Concepts of representations:&lt;br /&gt;
      Objects compose two types of primitives:&lt;br /&gt;
        1. Properties/Variables: these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
        2. Methods: responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
      The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
&lt;br /&gt;
        1. Slots: Everything including a property/method is a slot. Slots are placeholders which respond to messages.&lt;br /&gt;
          For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value.&lt;br /&gt;
&lt;br /&gt;
          Likewise, a method also responds to messages.&lt;br /&gt;
&lt;br /&gt;
        2. Keep them separate:&lt;br /&gt;
            Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          //FIGURE HERE:&lt;br /&gt;
&lt;br /&gt;
4. Languages:&lt;br /&gt;
    JavaScript:&lt;br /&gt;
      One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
    Self:&lt;br /&gt;
      It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
   Lua:&lt;br /&gt;
      One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
5. Drawbacks:&lt;br /&gt;
    Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
6. Conclusion:&lt;br /&gt;
    Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
7. References:&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43145</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43145"/>
		<updated>2010-12-02T02:14:36Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Creation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# '''Ex nihilo'''&lt;br /&gt;
# '''Extending an already existing prototype'''&lt;br /&gt;
&lt;br /&gt;
====Ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
====Extending an already existing one====&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
# Cloning&lt;br /&gt;
# Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
&lt;br /&gt;
        For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
      But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.2 Modification:&lt;br /&gt;
    Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
&lt;br /&gt;
    1. Add/Remove new attributes to an object/prototype.&lt;br /&gt;
      For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
&lt;br /&gt;
      to add a new property 'age' to person,&lt;br /&gt;
&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
      and to delete the property age, its as simple as:&lt;br /&gt;
&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
    2. Change the value of an attribute&lt;br /&gt;
      For example,&lt;br /&gt;
        assume Person = {name: 'Abcd', age: 99 }.&lt;br /&gt;
&lt;br /&gt;
      To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
    3. Add/Remove methods to/from an object/prototype&lt;br /&gt;
        For example,&lt;br /&gt;
          to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
&lt;br /&gt;
          to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
  The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.3 Concepts of representations:&lt;br /&gt;
      Objects compose two types of primitives:&lt;br /&gt;
        1. Properties/Variables: these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
        2. Methods: responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
      The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
&lt;br /&gt;
        1. Slots: Everything including a property/method is a slot. Slots are placeholders which respond to messages.&lt;br /&gt;
          For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value.&lt;br /&gt;
&lt;br /&gt;
          Likewise, a method also responds to messages.&lt;br /&gt;
&lt;br /&gt;
        2. Keep them separate:&lt;br /&gt;
            Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          //FIGURE HERE:&lt;br /&gt;
&lt;br /&gt;
4. Languages:&lt;br /&gt;
    JavaScript:&lt;br /&gt;
      One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
    Self:&lt;br /&gt;
      It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
   Lua:&lt;br /&gt;
      One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
5. Drawbacks:&lt;br /&gt;
    Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
6. Conclusion:&lt;br /&gt;
    Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
7. References:&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43143</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43143"/>
		<updated>2010-12-02T02:12:58Z</updated>

		<summary type="html">&lt;p&gt;Anadath: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# '''ex nihilo'''&amp;lt;br/&amp;gt;&amp;lt;p&amp;gt;This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&amp;lt;/p&amp;gt;&lt;br /&gt;
# '''Extending an already existing one'''&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
## Cloning&lt;br /&gt;
## Extending&lt;br /&gt;
Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
&lt;br /&gt;
        For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
      But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.2 Modification:&lt;br /&gt;
    Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
&lt;br /&gt;
    1. Add/Remove new attributes to an object/prototype.&lt;br /&gt;
      For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
&lt;br /&gt;
      to add a new property 'age' to person,&lt;br /&gt;
&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
      and to delete the property age, its as simple as:&lt;br /&gt;
&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
    2. Change the value of an attribute&lt;br /&gt;
      For example,&lt;br /&gt;
        assume Person = {name: 'Abcd', age: 99 }.&lt;br /&gt;
&lt;br /&gt;
      To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
    3. Add/Remove methods to/from an object/prototype&lt;br /&gt;
        For example,&lt;br /&gt;
          to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
&lt;br /&gt;
          to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
  The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.3 Concepts of representations:&lt;br /&gt;
      Objects compose two types of primitives:&lt;br /&gt;
        1. Properties/Variables: these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
        2. Methods: responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
      The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
&lt;br /&gt;
        1. Slots: Everything including a property/method is a slot. Slots are placeholders which respond to messages.&lt;br /&gt;
          For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value.&lt;br /&gt;
&lt;br /&gt;
          Likewise, a method also responds to messages.&lt;br /&gt;
&lt;br /&gt;
        2. Keep them separate:&lt;br /&gt;
            Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          //FIGURE HERE:&lt;br /&gt;
&lt;br /&gt;
4. Languages:&lt;br /&gt;
    JavaScript:&lt;br /&gt;
      One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
    Self:&lt;br /&gt;
      It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
   Lua:&lt;br /&gt;
      One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
5. Drawbacks:&lt;br /&gt;
    Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
6. Conclusion:&lt;br /&gt;
    Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
7. References:&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43141</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43141"/>
		<updated>2010-12-02T02:10:24Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Creation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# '''ex nihilo'''&amp;lt;br/&amp;gt;&amp;lt;p&amp;gt;This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&amp;lt;/p&amp;gt;&lt;br /&gt;
# '''Extending an already existing one'''&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
        1. Cloning&lt;br /&gt;
        2. Extending&lt;br /&gt;
&lt;br /&gt;
      Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
&lt;br /&gt;
        For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
      But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.2 Modification:&lt;br /&gt;
    Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
&lt;br /&gt;
    1. Add/Remove new attributes to an object/prototype.&lt;br /&gt;
      For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
&lt;br /&gt;
      to add a new property 'age' to person,&lt;br /&gt;
&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
      and to delete the property age, its as simple as:&lt;br /&gt;
&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
    2. Change the value of an attribute&lt;br /&gt;
      For example,&lt;br /&gt;
        assume Person = {name: 'Abcd', age: 99 }.&lt;br /&gt;
&lt;br /&gt;
      To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
    3. Add/Remove methods to/from an object/prototype&lt;br /&gt;
        For example,&lt;br /&gt;
          to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
&lt;br /&gt;
          to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
  The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.3 Concepts of representations:&lt;br /&gt;
      Objects compose two types of primitives:&lt;br /&gt;
        1. Properties/Variables: these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
        2. Methods: responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
      The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
&lt;br /&gt;
        1. Slots: Everything including a property/method is a slot. Slots are placeholders which respond to messages.&lt;br /&gt;
          For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value.&lt;br /&gt;
&lt;br /&gt;
          Likewise, a method also responds to messages.&lt;br /&gt;
&lt;br /&gt;
        2. Keep them separate:&lt;br /&gt;
            Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          //FIGURE HERE:&lt;br /&gt;
&lt;br /&gt;
4. Languages:&lt;br /&gt;
    JavaScript:&lt;br /&gt;
      One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
    Self:&lt;br /&gt;
      It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
   Lua:&lt;br /&gt;
      One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
5. Drawbacks:&lt;br /&gt;
    Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
6. Conclusion:&lt;br /&gt;
    Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
7. References:&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43139</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43139"/>
		<updated>2010-12-02T02:09:34Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Concepts of creation and representation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
# '''ex nihilo'''&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
&lt;br /&gt;
Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
# '''Extending an already existing one'''&lt;br /&gt;
Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
&lt;br /&gt;
Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
        1. Cloning&lt;br /&gt;
        2. Extending&lt;br /&gt;
&lt;br /&gt;
      Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
&lt;br /&gt;
        For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
      But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.2 Modification:&lt;br /&gt;
    Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
&lt;br /&gt;
    1. Add/Remove new attributes to an object/prototype.&lt;br /&gt;
      For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
&lt;br /&gt;
      to add a new property 'age' to person,&lt;br /&gt;
&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
      and to delete the property age, its as simple as:&lt;br /&gt;
&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
    2. Change the value of an attribute&lt;br /&gt;
      For example,&lt;br /&gt;
        assume Person = {name: 'Abcd', age: 99 }.&lt;br /&gt;
&lt;br /&gt;
      To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
    3. Add/Remove methods to/from an object/prototype&lt;br /&gt;
        For example,&lt;br /&gt;
          to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
&lt;br /&gt;
          to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
  The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.3 Concepts of representations:&lt;br /&gt;
      Objects compose two types of primitives:&lt;br /&gt;
        1. Properties/Variables: these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
        2. Methods: responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
      The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
&lt;br /&gt;
        1. Slots: Everything including a property/method is a slot. Slots are placeholders which respond to messages.&lt;br /&gt;
          For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value.&lt;br /&gt;
&lt;br /&gt;
          Likewise, a method also responds to messages.&lt;br /&gt;
&lt;br /&gt;
        2. Keep them separate:&lt;br /&gt;
            Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          //FIGURE HERE:&lt;br /&gt;
&lt;br /&gt;
4. Languages:&lt;br /&gt;
    JavaScript:&lt;br /&gt;
      One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
    Self:&lt;br /&gt;
      It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
   Lua:&lt;br /&gt;
      One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
5. Drawbacks:&lt;br /&gt;
    Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
6. Conclusion:&lt;br /&gt;
    Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
7. References:&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43133</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43133"/>
		<updated>2010-12-02T01:58:14Z</updated>

		<summary type="html">&lt;p&gt;Anadath: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
Creation of objects can be classified into two types.&lt;br /&gt;
====ex nihilo====&lt;br /&gt;
This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
&lt;br /&gt;
        Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
        ii) Extending an already existing one:&lt;br /&gt;
            Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
&lt;br /&gt;
      Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
        1. Cloning&lt;br /&gt;
        2. Extending&lt;br /&gt;
&lt;br /&gt;
      Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
&lt;br /&gt;
        For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
      But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.2 Modification:&lt;br /&gt;
    Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
&lt;br /&gt;
    1. Add/Remove new attributes to an object/prototype.&lt;br /&gt;
      For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
&lt;br /&gt;
      to add a new property 'age' to person,&lt;br /&gt;
&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
      and to delete the property age, its as simple as:&lt;br /&gt;
&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
    2. Change the value of an attribute&lt;br /&gt;
      For example,&lt;br /&gt;
        assume Person = {name: 'Abcd', age: 99 }.&lt;br /&gt;
&lt;br /&gt;
      To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
    3. Add/Remove methods to/from an object/prototype&lt;br /&gt;
        For example,&lt;br /&gt;
          to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
&lt;br /&gt;
          to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
  The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.3 Concepts of representations:&lt;br /&gt;
      Objects compose two types of primitives:&lt;br /&gt;
        1. Properties/Variables: these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
        2. Methods: responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
      The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
&lt;br /&gt;
        1. Slots: Everything including a property/method is a slot. Slots are placeholders which respond to messages.&lt;br /&gt;
          For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value.&lt;br /&gt;
&lt;br /&gt;
          Likewise, a method also responds to messages.&lt;br /&gt;
&lt;br /&gt;
        2. Keep them separate:&lt;br /&gt;
            Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          //FIGURE HERE:&lt;br /&gt;
&lt;br /&gt;
4. Languages:&lt;br /&gt;
    JavaScript:&lt;br /&gt;
      One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
    Self:&lt;br /&gt;
      It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
   Lua:&lt;br /&gt;
      One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
5. Drawbacks:&lt;br /&gt;
    Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
6. Conclusion:&lt;br /&gt;
    Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
7. References:&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43128</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43128"/>
		<updated>2010-12-02T01:48:38Z</updated>

		<summary type="html">&lt;p&gt;Anadath: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming - Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
===Simplicity===&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
===Reduce concepts/provide lesser primitives===&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
===Extensibility/Dynamic behavior===&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
==Concepts of creation and representation==&lt;br /&gt;
    As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
===Creation===&lt;br /&gt;
&lt;br /&gt;
      Creation of objects can be classified into two types.&lt;br /&gt;
        i) ex nihilo:&lt;br /&gt;
          This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
        in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
&lt;br /&gt;
        Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
        ii) Extending an already existing one:&lt;br /&gt;
            Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
&lt;br /&gt;
      Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
        1. Cloning&lt;br /&gt;
        2. Extending&lt;br /&gt;
&lt;br /&gt;
      Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
&lt;br /&gt;
        For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
      But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.2 Modification:&lt;br /&gt;
    Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
&lt;br /&gt;
    1. Add/Remove new attributes to an object/prototype.&lt;br /&gt;
      For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
&lt;br /&gt;
      to add a new property 'age' to person,&lt;br /&gt;
&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
      and to delete the property age, its as simple as:&lt;br /&gt;
&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
    2. Change the value of an attribute&lt;br /&gt;
      For example,&lt;br /&gt;
        assume Person = {name: 'Abcd', age: 99 }.&lt;br /&gt;
&lt;br /&gt;
      To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
    3. Add/Remove methods to/from an object/prototype&lt;br /&gt;
        For example,&lt;br /&gt;
          to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
&lt;br /&gt;
          to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
  The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.3 Concepts of representations:&lt;br /&gt;
      Objects compose two types of primitives:&lt;br /&gt;
        1. Properties/Variables: these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
        2. Methods: responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
      The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
&lt;br /&gt;
        1. Slots: Everything including a property/method is a slot. Slots are placeholders which respond to messages.&lt;br /&gt;
          For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value.&lt;br /&gt;
&lt;br /&gt;
          Likewise, a method also responds to messages.&lt;br /&gt;
&lt;br /&gt;
        2. Keep them separate:&lt;br /&gt;
            Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          //FIGURE HERE:&lt;br /&gt;
&lt;br /&gt;
4. Languages:&lt;br /&gt;
    JavaScript:&lt;br /&gt;
      One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
    Self:&lt;br /&gt;
      It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
   Lua:&lt;br /&gt;
      One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
5. Drawbacks:&lt;br /&gt;
    Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
6. Conclusion:&lt;br /&gt;
    Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
7. References:&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43126</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43126"/>
		<updated>2010-12-02T01:45:59Z</updated>

		<summary type="html">&lt;p&gt;Anadath: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming==&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
==Goals==&lt;br /&gt;
# Simplicity&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
====Reduce concepts/provide lesser primitives====&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
====Extensibility/Dynamic behavior====&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
===Concepts of creation and representation===&lt;br /&gt;
    As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
====Creation====&lt;br /&gt;
&lt;br /&gt;
      Creation of objects can be classified into two types.&lt;br /&gt;
        i) ex nihilo:&lt;br /&gt;
          This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
        in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
&lt;br /&gt;
        Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
        ii) Extending an already existing one:&lt;br /&gt;
            Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
&lt;br /&gt;
      Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
        1. Cloning&lt;br /&gt;
        2. Extending&lt;br /&gt;
&lt;br /&gt;
      Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
&lt;br /&gt;
        For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
      But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.2 Modification:&lt;br /&gt;
    Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
&lt;br /&gt;
    1. Add/Remove new attributes to an object/prototype.&lt;br /&gt;
      For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
&lt;br /&gt;
      to add a new property 'age' to person,&lt;br /&gt;
&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
      and to delete the property age, its as simple as:&lt;br /&gt;
&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
    2. Change the value of an attribute&lt;br /&gt;
      For example,&lt;br /&gt;
        assume Person = {name: 'Abcd', age: 99 }.&lt;br /&gt;
&lt;br /&gt;
      To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
    3. Add/Remove methods to/from an object/prototype&lt;br /&gt;
        For example,&lt;br /&gt;
          to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
&lt;br /&gt;
          to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
  The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.3 Concepts of representations:&lt;br /&gt;
      Objects compose two types of primitives:&lt;br /&gt;
        1. Properties/Variables: these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
        2. Methods: responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
      The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
&lt;br /&gt;
        1. Slots: Everything including a property/method is a slot. Slots are placeholders which respond to messages.&lt;br /&gt;
          For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value.&lt;br /&gt;
&lt;br /&gt;
          Likewise, a method also responds to messages.&lt;br /&gt;
&lt;br /&gt;
        2. Keep them separate:&lt;br /&gt;
            Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          //FIGURE HERE:&lt;br /&gt;
&lt;br /&gt;
4. Languages:&lt;br /&gt;
    JavaScript:&lt;br /&gt;
      One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
    Self:&lt;br /&gt;
      It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
   Lua:&lt;br /&gt;
      One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
5. Drawbacks:&lt;br /&gt;
    Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
6. Conclusion:&lt;br /&gt;
    Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
7. References:&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43124</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43124"/>
		<updated>2010-12-02T01:41:55Z</updated>

		<summary type="html">&lt;p&gt;Anadath: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming==&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
===Goals===&lt;br /&gt;
# Simplicity&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
====Reduce concepts/provide lesser primitives====&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
====Extensibility/Dynamic behavior====&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
===Concepts of creation and representation===&lt;br /&gt;
    As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
====Creation====&lt;br /&gt;
&lt;br /&gt;
      Creation of objects can be classified into two types.&lt;br /&gt;
        i) ex nihilo:&lt;br /&gt;
          This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
        in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
&lt;br /&gt;
        Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
        ii) Extending an already existing one:&lt;br /&gt;
            Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
&lt;br /&gt;
      Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
        1. Cloning&lt;br /&gt;
        2. Extending&lt;br /&gt;
&lt;br /&gt;
      Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
&lt;br /&gt;
        For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
      But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.2 Modification:&lt;br /&gt;
    Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
&lt;br /&gt;
    1. Add/Remove new attributes to an object/prototype.&lt;br /&gt;
      For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
&lt;br /&gt;
      to add a new property 'age' to person,&lt;br /&gt;
&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
      and to delete the property age, its as simple as:&lt;br /&gt;
&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
    2. Change the value of an attribute&lt;br /&gt;
      For example,&lt;br /&gt;
        assume Person = {name: 'Abcd', age: 99 }.&lt;br /&gt;
&lt;br /&gt;
      To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
    3. Add/Remove methods to/from an object/prototype&lt;br /&gt;
        For example,&lt;br /&gt;
          to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
&lt;br /&gt;
          to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
  The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.3 Concepts of representations:&lt;br /&gt;
      Objects compose two types of primitives:&lt;br /&gt;
        1. Properties/Variables: these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
        2. Methods: responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
      The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
&lt;br /&gt;
        1. Slots: Everything including a property/method is a slot. Slots are placeholders which respond to messages.&lt;br /&gt;
          For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value.&lt;br /&gt;
&lt;br /&gt;
          Likewise, a method also responds to messages.&lt;br /&gt;
&lt;br /&gt;
        2. Keep them separate:&lt;br /&gt;
            Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          //FIGURE HERE:&lt;br /&gt;
&lt;br /&gt;
4. Languages:&lt;br /&gt;
    JavaScript:&lt;br /&gt;
      One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
    Self:&lt;br /&gt;
      It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
   Lua:&lt;br /&gt;
      One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
5. Drawbacks:&lt;br /&gt;
    Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
6. Conclusion:&lt;br /&gt;
    Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
7. References:&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43122</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43122"/>
		<updated>2010-12-02T01:40:34Z</updated>

		<summary type="html">&lt;p&gt;Anadath: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming==&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
===Goals===&lt;br /&gt;
====Simplicity====&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
====Reduce concepts/provide lesser primitives====&lt;br /&gt;
If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
====Extensibility/Dynamic behavior====&lt;br /&gt;
Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
===Concepts of creation and representation===&lt;br /&gt;
    As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
====Creation====&lt;br /&gt;
&lt;br /&gt;
      Creation of objects can be classified into two types.&lt;br /&gt;
        i) ex nihilo:&lt;br /&gt;
          This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
        in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
&lt;br /&gt;
        Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
        ii) Extending an already existing one:&lt;br /&gt;
            Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
&lt;br /&gt;
      Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
        1. Cloning&lt;br /&gt;
        2. Extending&lt;br /&gt;
&lt;br /&gt;
      Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
&lt;br /&gt;
        For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
      But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.2 Modification:&lt;br /&gt;
    Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
&lt;br /&gt;
    1. Add/Remove new attributes to an object/prototype.&lt;br /&gt;
      For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
&lt;br /&gt;
      to add a new property 'age' to person,&lt;br /&gt;
&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
      and to delete the property age, its as simple as:&lt;br /&gt;
&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
    2. Change the value of an attribute&lt;br /&gt;
      For example,&lt;br /&gt;
        assume Person = {name: 'Abcd', age: 99 }.&lt;br /&gt;
&lt;br /&gt;
      To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
    3. Add/Remove methods to/from an object/prototype&lt;br /&gt;
        For example,&lt;br /&gt;
          to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
&lt;br /&gt;
          to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
  The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.3 Concepts of representations:&lt;br /&gt;
      Objects compose two types of primitives:&lt;br /&gt;
        1. Properties/Variables: these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
        2. Methods: responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
      The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
&lt;br /&gt;
        1. Slots: Everything including a property/method is a slot. Slots are placeholders which respond to messages.&lt;br /&gt;
          For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value.&lt;br /&gt;
&lt;br /&gt;
          Likewise, a method also responds to messages.&lt;br /&gt;
&lt;br /&gt;
        2. Keep them separate:&lt;br /&gt;
            Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          //FIGURE HERE:&lt;br /&gt;
&lt;br /&gt;
4. Languages:&lt;br /&gt;
    JavaScript:&lt;br /&gt;
      One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
    Self:&lt;br /&gt;
      It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
   Lua:&lt;br /&gt;
      One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
5. Drawbacks:&lt;br /&gt;
    Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
6. Conclusion:&lt;br /&gt;
    Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
7. References:&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43120</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43120"/>
		<updated>2010-12-02T01:39:27Z</updated>

		<summary type="html">&lt;p&gt;Anadath: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming==&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt;WHAT WILL BE COVERED?&lt;br /&gt;
&lt;br /&gt;
===Goals===&lt;br /&gt;
* Simplicity&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&amp;lt;/p&amp;gt;&lt;br /&gt;
* Reduce concepts/provide lesser primitives&amp;lt;br/&amp;gt;&lt;br /&gt;
        If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
* Extensibility/Dynamic behavior:&lt;br /&gt;
        Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
  Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
===Concepts of creation and representation===&lt;br /&gt;
    As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
====Creation====&lt;br /&gt;
&lt;br /&gt;
      Creation of objects can be classified into two types.&lt;br /&gt;
        i) ex nihilo:&lt;br /&gt;
          This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
        in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
&lt;br /&gt;
        Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
        ii) Extending an already existing one:&lt;br /&gt;
            Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
&lt;br /&gt;
      Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
        1. Cloning&lt;br /&gt;
        2. Extending&lt;br /&gt;
&lt;br /&gt;
      Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
&lt;br /&gt;
        For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
      But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.2 Modification:&lt;br /&gt;
    Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
&lt;br /&gt;
    1. Add/Remove new attributes to an object/prototype.&lt;br /&gt;
      For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
&lt;br /&gt;
      to add a new property 'age' to person,&lt;br /&gt;
&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
      and to delete the property age, its as simple as:&lt;br /&gt;
&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
    2. Change the value of an attribute&lt;br /&gt;
      For example,&lt;br /&gt;
        assume Person = {name: 'Abcd', age: 99 }.&lt;br /&gt;
&lt;br /&gt;
      To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
    3. Add/Remove methods to/from an object/prototype&lt;br /&gt;
        For example,&lt;br /&gt;
          to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
&lt;br /&gt;
          to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
  The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.3 Concepts of representations:&lt;br /&gt;
      Objects compose two types of primitives:&lt;br /&gt;
        1. Properties/Variables: these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
        2. Methods: responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
      The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
&lt;br /&gt;
        1. Slots: Everything including a property/method is a slot. Slots are placeholders which respond to messages.&lt;br /&gt;
          For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value.&lt;br /&gt;
&lt;br /&gt;
          Likewise, a method also responds to messages.&lt;br /&gt;
&lt;br /&gt;
        2. Keep them separate:&lt;br /&gt;
            Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          //FIGURE HERE:&lt;br /&gt;
&lt;br /&gt;
4. Languages:&lt;br /&gt;
    JavaScript:&lt;br /&gt;
      One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
    Self:&lt;br /&gt;
      It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
   Lua:&lt;br /&gt;
      One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
5. Drawbacks:&lt;br /&gt;
    Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
6. Conclusion:&lt;br /&gt;
    Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
7. References:&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43118</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43118"/>
		<updated>2010-12-02T01:36:03Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Prototype-based programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming==&lt;br /&gt;
&lt;br /&gt;
===Introduction===&lt;br /&gt;
===What is prototype based programming===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
===Flashback===&lt;br /&gt;
It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  WHAT WILL BE COVERED? == say here.&lt;br /&gt;
&lt;br /&gt;
===Goals===&lt;br /&gt;
* Simplicity&lt;br /&gt;
One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&lt;br /&gt;
* Reduce concepts/provide lesser primitives:&lt;br /&gt;
        If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
* Extensibility/Dynamic behavior:&lt;br /&gt;
        Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
  Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
===Concepts of creation and representation===&lt;br /&gt;
    As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
====Creation====&lt;br /&gt;
&lt;br /&gt;
      Creation of objects can be classified into two types.&lt;br /&gt;
        i) ex nihilo:&lt;br /&gt;
          This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
        in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
&lt;br /&gt;
        Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
        ii) Extending an already existing one:&lt;br /&gt;
            Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
&lt;br /&gt;
      Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
        1. Cloning&lt;br /&gt;
        2. Extending&lt;br /&gt;
&lt;br /&gt;
      Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
&lt;br /&gt;
        For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
      But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.2 Modification:&lt;br /&gt;
    Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
&lt;br /&gt;
    1. Add/Remove new attributes to an object/prototype.&lt;br /&gt;
      For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
&lt;br /&gt;
      to add a new property 'age' to person,&lt;br /&gt;
&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
      and to delete the property age, its as simple as:&lt;br /&gt;
&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
    2. Change the value of an attribute&lt;br /&gt;
      For example,&lt;br /&gt;
        assume Person = {name: 'Abcd', age: 99 }.&lt;br /&gt;
&lt;br /&gt;
      To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
    3. Add/Remove methods to/from an object/prototype&lt;br /&gt;
        For example,&lt;br /&gt;
          to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
&lt;br /&gt;
          to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
  The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.3 Concepts of representations:&lt;br /&gt;
      Objects compose two types of primitives:&lt;br /&gt;
        1. Properties/Variables: these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
        2. Methods: responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
      The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
&lt;br /&gt;
        1. Slots: Everything including a property/method is a slot. Slots are placeholders which respond to messages.&lt;br /&gt;
          For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value.&lt;br /&gt;
&lt;br /&gt;
          Likewise, a method also responds to messages.&lt;br /&gt;
&lt;br /&gt;
        2. Keep them separate:&lt;br /&gt;
            Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          //FIGURE HERE:&lt;br /&gt;
&lt;br /&gt;
4. Languages:&lt;br /&gt;
    JavaScript:&lt;br /&gt;
      One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
    Self:&lt;br /&gt;
      It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
   Lua:&lt;br /&gt;
      One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
5. Drawbacks:&lt;br /&gt;
    Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
6. Conclusion:&lt;br /&gt;
    Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
7. References:&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43117</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43117"/>
		<updated>2010-12-02T01:33:00Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Prototype-based programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming==&lt;br /&gt;
&lt;br /&gt;
===Introduction: What is prototype based programming?===&lt;br /&gt;
===What is it?===&lt;br /&gt;
Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
  ===Flashback===&lt;br /&gt;
      It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  WHAT WILL BE COVERED? == say here.&lt;br /&gt;
&lt;br /&gt;
===Goals===&lt;br /&gt;
     # Simplicity:&lt;br /&gt;
        One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
        Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
        Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&lt;br /&gt;
    ii) Reduce concepts/provide lesser primitives:&lt;br /&gt;
        If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
    iii) Extensibility/Dynamic behavior:&lt;br /&gt;
        Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
  Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
3. Concepts of creation and representation:&lt;br /&gt;
    As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
  3.1 Creation:&lt;br /&gt;
&lt;br /&gt;
      Creation of objects can be classified into two types.&lt;br /&gt;
        i) ex nihilo:&lt;br /&gt;
          This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
        in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
&lt;br /&gt;
        Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
        ii) Extending an already existing one:&lt;br /&gt;
            Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
&lt;br /&gt;
      Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
        1. Cloning&lt;br /&gt;
        2. Extending&lt;br /&gt;
&lt;br /&gt;
      Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
&lt;br /&gt;
        For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
      But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.2 Modification:&lt;br /&gt;
    Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
&lt;br /&gt;
    1. Add/Remove new attributes to an object/prototype.&lt;br /&gt;
      For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
&lt;br /&gt;
      to add a new property 'age' to person,&lt;br /&gt;
&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
      and to delete the property age, its as simple as:&lt;br /&gt;
&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
    2. Change the value of an attribute&lt;br /&gt;
      For example,&lt;br /&gt;
        assume Person = {name: 'Abcd', age: 99 }.&lt;br /&gt;
&lt;br /&gt;
      To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
    3. Add/Remove methods to/from an object/prototype&lt;br /&gt;
        For example,&lt;br /&gt;
          to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
&lt;br /&gt;
          to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
  The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.3 Concepts of representations:&lt;br /&gt;
      Objects compose two types of primitives:&lt;br /&gt;
        1. Properties/Variables: these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
        2. Methods: responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
      The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
&lt;br /&gt;
        1. Slots: Everything including a property/method is a slot. Slots are placeholders which respond to messages.&lt;br /&gt;
          For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value.&lt;br /&gt;
&lt;br /&gt;
          Likewise, a method also responds to messages.&lt;br /&gt;
&lt;br /&gt;
        2. Keep them separate:&lt;br /&gt;
            Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          //FIGURE HERE:&lt;br /&gt;
&lt;br /&gt;
4. Languages:&lt;br /&gt;
    JavaScript:&lt;br /&gt;
      One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
    Self:&lt;br /&gt;
      It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
   Lua:&lt;br /&gt;
      One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
5. Drawbacks:&lt;br /&gt;
    Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
6. Conclusion:&lt;br /&gt;
    Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
7. References:&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43114</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43114"/>
		<updated>2010-12-02T01:32:37Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Prototype-based programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming==&lt;br /&gt;
&lt;br /&gt;
===Introduction: What is prototype based programming?===&lt;br /&gt;
  ===What is it?===&lt;br /&gt;
      Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
  ===Flashback===&lt;br /&gt;
      It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  WHAT WILL BE COVERED? == say here.&lt;br /&gt;
&lt;br /&gt;
===Goals===&lt;br /&gt;
     # Simplicity:&lt;br /&gt;
        One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
        Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
        Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&lt;br /&gt;
    ii) Reduce concepts/provide lesser primitives:&lt;br /&gt;
        If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
    iii) Extensibility/Dynamic behavior:&lt;br /&gt;
        Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
  Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
3. Concepts of creation and representation:&lt;br /&gt;
    As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
  3.1 Creation:&lt;br /&gt;
&lt;br /&gt;
      Creation of objects can be classified into two types.&lt;br /&gt;
        i) ex nihilo:&lt;br /&gt;
          This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
        in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
&lt;br /&gt;
        Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
        ii) Extending an already existing one:&lt;br /&gt;
            Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
&lt;br /&gt;
      Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
        1. Cloning&lt;br /&gt;
        2. Extending&lt;br /&gt;
&lt;br /&gt;
      Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
&lt;br /&gt;
        For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
      But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.2 Modification:&lt;br /&gt;
    Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
&lt;br /&gt;
    1. Add/Remove new attributes to an object/prototype.&lt;br /&gt;
      For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
&lt;br /&gt;
      to add a new property 'age' to person,&lt;br /&gt;
&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
      and to delete the property age, its as simple as:&lt;br /&gt;
&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
    2. Change the value of an attribute&lt;br /&gt;
      For example,&lt;br /&gt;
        assume Person = {name: 'Abcd', age: 99 }.&lt;br /&gt;
&lt;br /&gt;
      To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
    3. Add/Remove methods to/from an object/prototype&lt;br /&gt;
        For example,&lt;br /&gt;
          to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
&lt;br /&gt;
          to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
  The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.3 Concepts of representations:&lt;br /&gt;
      Objects compose two types of primitives:&lt;br /&gt;
        1. Properties/Variables: these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
        2. Methods: responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
      The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
&lt;br /&gt;
        1. Slots: Everything including a property/method is a slot. Slots are placeholders which respond to messages.&lt;br /&gt;
          For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value.&lt;br /&gt;
&lt;br /&gt;
          Likewise, a method also responds to messages.&lt;br /&gt;
&lt;br /&gt;
        2. Keep them separate:&lt;br /&gt;
            Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          //FIGURE HERE:&lt;br /&gt;
&lt;br /&gt;
4. Languages:&lt;br /&gt;
    JavaScript:&lt;br /&gt;
      One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
    Self:&lt;br /&gt;
      It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
   Lua:&lt;br /&gt;
      One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
5. Drawbacks:&lt;br /&gt;
    Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
6. Conclusion:&lt;br /&gt;
    Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
7. References:&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43113</id>
		<title>CSC/ECE 517 Fall 2010/ch7 4e ak</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch7_4e_ak&amp;diff=43113"/>
		<updated>2010-12-02T01:32:06Z</updated>

		<summary type="html">&lt;p&gt;Anadath: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Prototype-based programming==&lt;br /&gt;
&lt;br /&gt;
===Introduction: What is prototype based programming?===&lt;br /&gt;
  # What is it?&lt;br /&gt;
      Prototype based programming is a family of languages supporting object oriented development based on the concept of 'prototype', rather than a 'class' in languages such as Java, C++ and C#. In otherwords, these languages propose a new world in which there are no classes and behavior reuse is done via cloning/extending objects which are already present. Also known as 'class-less' or 'instance-based programming', some of well-known languages belonging to this category include Self, JavaScript/ECMAScript, NewtonScript, Lua.&lt;br /&gt;
&lt;br /&gt;
  # Flashback:&lt;br /&gt;
      It all started with frame theory and frame-based languages such as KRL and FRL. These languages allow us to capture knowledge such as default values or exceptions. A frame is a set of attributes each mapped to a set of facets(usually a single value). We will see how this influenced prototype based languages through the course of this article. One of the most popular prototype-language 'Self' was developed by David Ungar and Randall Smith when working at Sun microsystems throughout 1980s and 90s. The self project aimed at providing near 'C' performance via JIT compilation techniques and runtime optimizations. Infact, some of these optimizations are implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  WHAT WILL BE COVERED? == say here.&lt;br /&gt;
&lt;br /&gt;
===Goals===&lt;br /&gt;
     # Simplicity:&lt;br /&gt;
        One of the basic tenants of prototype based programming languages is that:&lt;br /&gt;
          'Avoid classes. They are just too hard'&lt;br /&gt;
        Why so? Well, we humans tend to grasp new concepts only by working with concrete examples rather than with abstract descriptions. For example, when learning math, we tend to work out a large number of examples before deriving relationships and equations.&lt;br /&gt;
&lt;br /&gt;
        Classes are similar to equations. They are abstractions of the concrete objects we are trying to model. They force us to think in the opposite direction and hence tend to be counter productive in a number of situations. No wonder its so hard to design classes and their responsibilities. We never get it right the first time. ;)&lt;br /&gt;
&lt;br /&gt;
    ii) Reduce concepts/provide lesser primitives:&lt;br /&gt;
        If you have done GUI programming using Java's Swing or GTK+, then you know classes don't make it any easier to program. Sometimes, these classes tend to play a very large number of roles and it is hard to reduce complexity in such scenarios. Prototype-based languages tend to move away from classes and provide protoypes which can be very easily customized/extended.&lt;br /&gt;
&lt;br /&gt;
    iii) Extensibility/Dynamic behavior:&lt;br /&gt;
        Protoype-based languages are inherently dynamic. Languages in this family allow new methods/attributes to be added/removed in the prototype at runtime.&lt;br /&gt;
&lt;br /&gt;
  Some of the prototype-based languages have evolved and include quasi concepts from other language families. This could be attributed to design constraints, improve performance etc.. Likewise, some of the concepts of prototype-based languages have been adopted by a number of languages.&lt;br /&gt;
&lt;br /&gt;
3. Concepts of creation and representation:&lt;br /&gt;
    As mentioned earlier, prototype based programming allow us to create new objects, modify their behavior in any possible manner.&lt;br /&gt;
&lt;br /&gt;
  3.1 Creation:&lt;br /&gt;
&lt;br /&gt;
      Creation of objects can be classified into two types.&lt;br /&gt;
        i) ex nihilo:&lt;br /&gt;
          This is a type of object creation in which the object is created ex nihilo. In otherwords, it is derived from no object. For example,&lt;br /&gt;
        in JavaScript, to create a new object Person with attributes name, age:&lt;br /&gt;
&lt;br /&gt;
          var Person = { name: 'Kovalan', age: 23 }&lt;br /&gt;
&lt;br /&gt;
        Note that, the above object was created from nothing, simply by using '{}' braces.&lt;br /&gt;
&lt;br /&gt;
        ii) Extending an already existing one:&lt;br /&gt;
            Here, we create a new object by extending/cloning an already existing object/prototype. For example in Javascript,&lt;br /&gt;
&lt;br /&gt;
              //source: StackOverflow.&lt;br /&gt;
              function extend(o) {&lt;br /&gt;
                  function F() {}&lt;br /&gt;
                  F.prototype = o;&lt;br /&gt;
                  return new F();&lt;br /&gt;
              }&lt;br /&gt;
&lt;br /&gt;
              var newObject = extend(Person);&lt;br /&gt;
&lt;br /&gt;
      Actually there are two concrete types of creating objects based on a prototype.&lt;br /&gt;
        1. Cloning&lt;br /&gt;
        2. Extending&lt;br /&gt;
&lt;br /&gt;
      Note that both features may not be supported by all prototype-based languages. One notable example is 'Kevo' which supports only cloning and adding new properties to clones. The main difference between the two lies in the way objects share their properties. This is because cloning employs 'shallow cloning/copying'(see wikipedia). In other words, the values of the properties are copied and they don't share any references with each other. As a result, cloned objects tend to evolve in a direction independent of their source.&lt;br /&gt;
&lt;br /&gt;
        For example:&lt;br /&gt;
          //PICTURES HERE&lt;br /&gt;
&lt;br /&gt;
      But in the case of objects created by 'extension', they tend to share the same references. So, whenever the value referred to is changed via either of the objects, the change is visible to all the instances. Also when extending, properties are not copied. Rather they are delegated. Delegation is a very important feature of prototype based programming.&lt;br /&gt;
&lt;br /&gt;
        //SHOULD WE DESCRIBE DELEGATION HERE?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.2 Modification:&lt;br /&gt;
    Prototype based programming allows us to modify objects in a number of ways including the following:&lt;br /&gt;
&lt;br /&gt;
    1. Add/Remove new attributes to an object/prototype.&lt;br /&gt;
      For example, in JavaScript&lt;br /&gt;
        var Person = {name: 'John' }&lt;br /&gt;
&lt;br /&gt;
      to add a new property 'age' to person,&lt;br /&gt;
&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
      and to delete the property age, its as simple as:&lt;br /&gt;
&lt;br /&gt;
        delete Person.age&lt;br /&gt;
&lt;br /&gt;
    2. Change the value of an attribute&lt;br /&gt;
      For example,&lt;br /&gt;
        assume Person = {name: 'Abcd', age: 99 }.&lt;br /&gt;
&lt;br /&gt;
      To change age to 23, we write.&lt;br /&gt;
        Person.age = 23&lt;br /&gt;
&lt;br /&gt;
    3. Add/Remove methods to/from an object/prototype&lt;br /&gt;
        For example,&lt;br /&gt;
          to add a new method to Array prototype in js, we write:&lt;br /&gt;
&lt;br /&gt;
          Array.prototype.isEmpty = function(){&lt;br /&gt;
            return this.size() == 0;&lt;br /&gt;
          }&lt;br /&gt;
&lt;br /&gt;
          to delete the method 'useless' from Array prototype(assuming we added it earlier),&lt;br /&gt;
&lt;br /&gt;
            delete Array.prototype.useless&lt;br /&gt;
&lt;br /&gt;
  The whole world of objects is open for modification and extensibility. This is very similar to how other dynamic programming languages like python, ruby operate. However, deleting/modifying standard methods are frowned upon.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  3.3 Concepts of representations:&lt;br /&gt;
      Objects compose two types of primitives:&lt;br /&gt;
        1. Properties/Variables: these are simple variable types(such as 'string', 'array' etc..)&lt;br /&gt;
        2. Methods: responsible for behavior/characteristic of a prototype. methods/functions operate on properties and respond to requests/invocations.&lt;br /&gt;
&lt;br /&gt;
      The above concept is the same in almost all the languages such as C++, Java, Ruby, Python etc... However, in prototype based languages there are two broad concept of representations.&lt;br /&gt;
&lt;br /&gt;
        1. Slots: Everything including a property/method is a slot. Slots are placeholders which respond to messages.&lt;br /&gt;
          For example, a property is a slot which when invoked with nothing yields its value. If invoked with a value, the property stores this value.&lt;br /&gt;
&lt;br /&gt;
          Likewise, a method also responds to messages.&lt;br /&gt;
&lt;br /&gt;
        2. Keep them separate:&lt;br /&gt;
            Properties and methods are treated separately(as in languages like Java, C++). Self introduced the concept of a 'trait'. Trait objects are repositories of methods/functions. These apply to a whole family of objects and so its logical to keep the properties and methods separate. So, when viewing objects inheriting from a parent object, all the values(concrete objects) tend to be stored in the leaf nodes.&lt;br /&gt;
&lt;br /&gt;
          //FIGURE HERE:&lt;br /&gt;
&lt;br /&gt;
4. Languages:&lt;br /&gt;
    JavaScript:&lt;br /&gt;
      One of the most popular prototype languages, it was developed by Brendan Eich when working for Netscape. It is the standard programming language supported by all the web browsers(including Firefox, Google Chrome, Opera). There are a number of JavaScript frameworks out there to simplify the development of prototypes in js. jQuery, PrototypeJS, Moo-tools are some of the popular ones.&lt;br /&gt;
&lt;br /&gt;
    Self:&lt;br /&gt;
      It was designed by David Ungar and Randall Smith in 1986 while working at Xerox. Later they moved to Sun Microsystems where, it is still under continuous development. It supports garbage collection(like Java) and several JIT techniques were pioneered in Self research. Some of these techniques are currently implemented in JVM.&lt;br /&gt;
&lt;br /&gt;
   Lua:&lt;br /&gt;
      One of the relatively new programming languages emerging out of Brazil, it was created in 1993 and is now very popularly used as a scripting language in game engines used on ipad/iphone/etc.. However, it is not a pure prototype-based language; rather a number of programming paradigms are incorporated.&lt;br /&gt;
&lt;br /&gt;
5. Drawbacks:&lt;br /&gt;
    Even though prototype based languages are highly flexible and dynamic, they do not provide type safety or predictability. With class-based programming languages, it is easier to predict the outcome of programs given the static nature of the classes and hence prove correctness. Another concern is that of efficiency of prototype-based languages. Classes being static in nature allow the compiler to perform a large number of optimizations which are not currently possible with prototype-based languages. Having said that, currently there is a very healthy competition among different browser vendors such as Google, Microsoft, Firefox/Mozilla, Opera to provide the fastest javascript execution engine. In otherwords, a number of new techniques are still being pioneered to improve the performance of prototype based languages.&lt;br /&gt;
&lt;br /&gt;
6. Conclusion:&lt;br /&gt;
    Thus all the prototype based languages incorporate the concept of 'prototype' and creating/deriving new objects from this prototype. This line of thinking has inspired a number of beautiful languages including JavaScript, Lua, Self and much more.&lt;br /&gt;
&lt;br /&gt;
7. References:&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_az&amp;diff=34561</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1e az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_az&amp;diff=34561"/>
		<updated>2010-09-09T02:44:22Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Mixed paradigm */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Programming Paradigms==&lt;br /&gt;
 &lt;br /&gt;
Every computer program needs a style of writing which specifies how to solve a software engineering problem. This style is represented by the paradigm. Each computer program follows one or more paradigm which differs in representing the elements of a program(such as variables and objects) and the steps needed to compute a task.&lt;br /&gt;
&lt;br /&gt;
Diferent paradigms are:&lt;br /&gt;
&lt;br /&gt;
# Procedural/imperative paradigms: Assembly, C, C++, Java, C#&lt;br /&gt;
# Object Oriented paradigm : C++, Java, Python, Ruby, Scala, C#&lt;br /&gt;
# Functional Paradigm : Lisp, Haskell, Clojure, Scala, OCaml, Ruby&lt;br /&gt;
# Logic Paradigm: Prolog&lt;br /&gt;
&lt;br /&gt;
==Multi-Paradigm Programming==&lt;br /&gt;
&lt;br /&gt;
Multiparadigm refers to use of a combination of programming paradigms for solving a computer problem. Some languages subscribe strictly to a single paradigm like Assembly and C. Others like Java, C++, Scala and C# employ more than one paradigm. Every paradigm comes with its own strength and weakness and this quite motivates us to take advantage of each paradigm and use it in a manner that best fits the problem at hand.&lt;br /&gt;
&lt;br /&gt;
==Overview of Functional Programming==&lt;br /&gt;
&lt;br /&gt;
Functional programming is derived from lambda calculus which models computations as the evaluation of functions and recursions. More emphasis is laid on application of functions rather than changing the state of variables in a program. Functional programming relies heavily on recursions and it is the only way to iterate instead of loops.&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;How it differs from imperative programming&amp;lt;/b&amp;gt;===&lt;br /&gt;
    &lt;br /&gt;
Imperative programming follows the Von-Neumann architecture and mainly consists of loops and usage of globa states to perform a computation. For example consider the task of computing the sum of numbers from 1 to n. &lt;br /&gt;
&lt;br /&gt;
In imperative style,&lt;br /&gt;
        &lt;br /&gt;
        sum := 0         // global state&lt;br /&gt;
        for i &amp;lt;- 1 to n do&lt;br /&gt;
            sum := sum + i&lt;br /&gt;
            &lt;br /&gt;
In Functional style,&lt;br /&gt;
    &lt;br /&gt;
         func sum(n)        // note that sum is a function and is recursive&lt;br /&gt;
           if  n = 1 then&lt;br /&gt;
               return 1&lt;br /&gt;
           else&lt;br /&gt;
               return n + sum(n-1)&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Pure and impure functional programming&amp;lt;/b&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
Purely functional programming exhibit referential transparency [http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)] which does not involve any global state or I/O changes. If the same functional expression results in the same output value for the same argument x at different stages of execution , then the function is said to be pure which is devoid of any global state change. For example Haskell [http://www.haskell.org] is purely functional.&lt;br /&gt;
&lt;br /&gt;
==Overview of object oriented programming==&lt;br /&gt;
&lt;br /&gt;
Typially programs were written with a procedural view where the program's logic was given utmost importance and it follows a sequential structure. But object oriented techniques really cares about the data or objects we want to mainpulate with rather than the logic of  a program. Every object contains its own state(in the form of variables) and and a number of methods to manipulate the variables.For example consider the class Add:&lt;br /&gt;
           &lt;br /&gt;
           class Add{&lt;br /&gt;
           /* Object variables */&lt;br /&gt;
                  private int sum = 0;&lt;br /&gt;
          /* Object Methods */&lt;br /&gt;
                  public void calculate_sum(int n){&lt;br /&gt;
                      int i=0;&lt;br /&gt;
                      while(i &amp;lt;= n){&lt;br /&gt;
                          sum = sum + i;&lt;br /&gt;
                          i++;&lt;br /&gt;
                      }&lt;br /&gt;
                  }&lt;br /&gt;
            }&lt;br /&gt;
       &lt;br /&gt;
            Add instance1 = new Add();     // instance1 is an object of type Add&lt;br /&gt;
            instance1.calculate_sum(100);&lt;br /&gt;
           &lt;br /&gt;
===&amp;lt;b&amp;gt;Principles of object oriented design&amp;lt;/b&amp;gt;===&lt;br /&gt;
           &lt;br /&gt;
1. &amp;lt;b&amp;gt;Encapsulation:&amp;lt;/b&amp;gt; Encapsulation refers to hiding the internal representation of the object from the outside view. For example algorithm to compute the calculate_sum() method may be hidden, but it will present the user with the expected results.&lt;br /&gt;
&lt;br /&gt;
2. &amp;lt;b&amp;gt;Polymorphism:&amp;lt;/b&amp;gt; Polymorphism means ability to take multiple forms. For example an operation may exhibit different behaviour at different instances. Consider the operator '+', for numbers it will generate the sum, but for strings it will produce a third string which concatenates the input strings.&lt;br /&gt;
&lt;br /&gt;
3. &amp;lt;b&amp;gt;Inheritance:&amp;lt;/b&amp;gt; Inheritance involves base and derived classes with derived class inheriting all the methods and states frm the base class. Inheritance basically forms a hirerachy. For example if shape is an object, then objects square, rectangle, circle all derive the same characteristic as shape does.&lt;br /&gt;
&lt;br /&gt;
==Functional + OOP code==&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Functions as Objects&amp;lt;/b&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
One of the cornerstone of functional and object oriented code is treating functions as objects.It means that we can pass functions as arguments, store it and return them from other functions. This is highly useful in an user interface code where it can be used as call back functions which get called when an event occurs( in event driven programming)&lt;br /&gt;
&lt;br /&gt;
An example in Scala [http://www.scala-lang.org]:&lt;br /&gt;
&lt;br /&gt;
  Object Timer{&lt;br /&gt;
    def  action(callback() : () =&amp;gt; unit){              // callback() is the function passed as an argument&lt;br /&gt;
         while( some condition)                                                            &lt;br /&gt;
         { &lt;br /&gt;
            callback();&lt;br /&gt;
            Thread.sleep(3000)&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
     def event(){                                      //  event is the method which tells what to do &lt;br /&gt;
         /* process the event here&lt;br /&gt;
         &lt;br /&gt;
         */      &lt;br /&gt;
     }&lt;br /&gt;
        &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Another example of functional concept in object oriented design is the concept of blocks especially in Ruby. Blocks are basically nameless functions which can be passed to a function and then that function can invoke the passed in nameless function. This is a common style called higher order function style among languages that handle functions as first class objects. Basically blocks can be designed for loop abstraction and lets the user to design their own way of iterating the values, thereby hiding the implementation details. &lt;br /&gt;
For example if we want to iterate backwards from the end to the beginning without revealing its internal implementations, we could implement the loop logic inside the block and pass it to a method or function.&lt;br /&gt;
&lt;br /&gt;
Here is a Ruby implementation of block&lt;br /&gt;
 &lt;br /&gt;
 def printlist(array,&amp;amp;reverse)&lt;br /&gt;
    array.each do |element|&lt;br /&gt;
       puts &amp;quot;array element: #{element}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    reverse.call(array)   /* Here it prints the reverse implementation of list*/&lt;br /&gt;
    end&lt;br /&gt;
  &lt;br /&gt;
 printlist([1,2,3,4,5,6]) { |array| &lt;br /&gt;
   array.reverse.each do |element| &lt;br /&gt;
     puts &amp;quot;array element: #{element}&amp;quot; &lt;br /&gt;
   end &lt;br /&gt;
 }&lt;br /&gt;
Next we will take a look at some of the features of Scala which supports a blend of functional and object oriented concepts in a concise manner.&lt;br /&gt;
&lt;br /&gt;
= Scala =&lt;br /&gt;
Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages&lt;br /&gt;
&lt;br /&gt;
Scala can be considered a very good blend of the best of functional and object oriented world. It was conceived(in 2001) and implemented by Martin Odersky who was also the author of Sun javac compiler and played a major role in the design and retrofitting of generics into Java. The latest version of Scala is 2.8 and includes a number of performance improvements over previous versions. We shall see more about the features of Scala in the following pages, but for fun, some facts!&lt;br /&gt;
&lt;br /&gt;
=== Features ===&lt;br /&gt;
==== Statically Typed ====&lt;br /&gt;
Scala belongs to the family of languages which do not allow the type of variables once they have been defined. In other words, the type information of each and every variable is encoded in the generated code and the compiler makes sure, you are not doing something funny. For example, lets consider the following example,&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
   irb(main):001:0&amp;gt; str=&amp;quot;Hello World&amp;quot;&lt;br /&gt;
    =&amp;gt; &amp;quot;Hello World&amp;quot;&lt;br /&gt;
    irb(main):002:0&amp;gt; puts str.class&lt;br /&gt;
    String&lt;br /&gt;
    irb(main):003:0&amp;gt; str=123&lt;br /&gt;
    =&amp;gt; 123&lt;br /&gt;
    irb(main):004:0&amp;gt; puts str.class&lt;br /&gt;
    Fixnum&lt;br /&gt;
&lt;br /&gt;
In the above example, first we define 'str' variable to be of type String and then as a number. So basically, no type information is associated with the variable at any point in the program. Hence we can easily reassociate that variable with an object/instance&lt;br /&gt;
of a different type. This is a feature of all dynamically typed languages(like Ruby, Python, Clojure). However most of the statically typed languages such as Scala, Java, C/C++ do not allow you do so. In Scala,&lt;br /&gt;
&lt;br /&gt;
  scala&amp;gt; var str=&amp;quot;Hello World&amp;quot;&lt;br /&gt;
  str: java.lang.String = Hello World&lt;br /&gt;
  scala&amp;gt; str=123&lt;br /&gt;
  &amp;lt;console&amp;gt;:6: error: type mismatch;&lt;br /&gt;
   found   : Int(123)&lt;br /&gt;
   required: java.lang.String&lt;br /&gt;
         str=123&lt;br /&gt;
==== Mixed paradigm ====&lt;br /&gt;
Scala is a strange specimen. It is a complete blend of object oriented and functional programming. How can this happen? Functional world advocates immutability but object oriented world is all about state and how do you mutate it to fit your needs. Scala allows you create both mutable and immutable objects. Consider the following example,&lt;br /&gt;
 val str: String = &amp;quot;Hello World&amp;quot;&lt;br /&gt;
In the first line, I am creating a immutable variable(a misnomer indeed). Roughly translating the above line to Java is below:&lt;br /&gt;
&lt;br /&gt;
 final String str = &amp;quot;Hello World&amp;quot;; //note the extra semi-colon&lt;br /&gt;
Consider the next example below,&lt;br /&gt;
 val list: List[String] = List(&amp;quot;Scala&amp;quot;, &amp;quot;is&amp;quot;, &amp;quot;the coolest&amp;quot;, &amp;quot;language&amp;quot;, &amp;quot;in the world!&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The same code can be written in Java as below&lt;br /&gt;
&lt;br /&gt;
  List&amp;lt;String&amp;gt; mutableList = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
  list.add(&amp;quot;Scala&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;is&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;the coolest&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;language&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;in the world!&amp;quot;);&lt;br /&gt;
  List&amp;lt;String&amp;gt; immutableList = Collections.immutableList(mutableList);&lt;br /&gt;
&lt;br /&gt;
Compared to Java, Scala reduces the total number of lines you need to type and at the same time looks so much simpler and less verbose. Even though the above example shows immutable collection example, scala also provides mutable collection and object types under the package &amp;lt;code&amp;gt;scala.collection.mutable.*&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As it can be observed, even though scala advocates immutability, it does not restrict you from creating mutable objects. So its the best of both worlds. Another part of functional world is the concept of functions as ''''first class members'''' of the language. Some of the common languages like Ruby, Python and JavaScript all incorporate this feature. In the sense you can pass around functions to methods as a parameter. Enough talking;&lt;br /&gt;
&lt;br /&gt;
Let us have a class say &amp;lt;code&amp;gt;'Person'&amp;lt;/code&amp;gt; which has properties, &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; and another variable &amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt;.&lt;br /&gt;
In Java:&lt;br /&gt;
&lt;br /&gt;
 class Person{&lt;br /&gt;
    private String name;&lt;br /&gt;
    private int age;&lt;br /&gt;
    public String getName(){&lt;br /&gt;
        return name;&lt;br /&gt;
    }&lt;br /&gt;
    public void setName(String s){&lt;br /&gt;
        this.name = s;&lt;br /&gt;
    }&lt;br /&gt;
    public int getAge(){&lt;br /&gt;
        return age;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAge(int age){&lt;br /&gt;
        this.age = age;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
 case class Person(name: String, age: Int)&lt;br /&gt;
&lt;br /&gt;
This single line is equivalent to the above java code! So much cooler.. I would not be doing justice if I don't mention the fact that getters for &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt; are generated. Also &amp;lt;code&amp;gt;equals()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;hashCode()&amp;lt;/code&amp;gt; method are automatically generated for you by the compiler. Isn't this an awesome feature? No wonder Scala is considered a beautiful and high level language. Now assume we have a list of persons and we want to filter out all the persons who are of age 23. How would you do that in Java?&lt;br /&gt;
&lt;br /&gt;
  List&amp;lt;Person&amp;gt; persons; //has a bunch of person objects in it.&lt;br /&gt;
  List&amp;lt;Person&amp;gt; twentyThree = new ArrayList&amp;lt;Person&amp;gt;();&lt;br /&gt;
  for(Person p: persons){&lt;br /&gt;
      if(p.getAge() == 23){&lt;br /&gt;
          twentyThree.add(p);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
&lt;br /&gt;
 val twentyThree: List[Person] = persons.filter( _.age == 23)&lt;br /&gt;
&lt;br /&gt;
That's it! In case you are wondering whats going on, filter is a method present on scala.List class and the code block  &amp;lt;code&amp;gt;_.age == 23&amp;lt;/code&amp;gt; is a function created on the fly and passed onto the filter method. More precisely its called a closure - a functional concept which can be emulated in java only using anonymous inner classes. In case you are wondering how scala does this, scala's compiler transforms our one line function into some anonymous inner classes when generating java byte code but allows developer to define functions in a simple manner.&lt;br /&gt;
&lt;br /&gt;
Infact every function is an object in scala. Scala's standard library has a number of functions which are object themselves.&lt;br /&gt;
&lt;br /&gt;
==== Sophisticated Syntax/Features ====&lt;br /&gt;
When we say sophisticated, its really an awesome feature which is present in other functional languages like Haskell, (O)Caml and Erlang. Consider the following example,&lt;br /&gt;
&lt;br /&gt;
   val name = &amp;quot;Jackie Chan&amp;quot;&lt;br /&gt;
   println(name.getClass) //prints out String.&lt;br /&gt;
&lt;br /&gt;
Previously we saw that scala is a statically typed language and yet in the above example we don't specify what type is the variable name. In Java, you would type in the following:&lt;br /&gt;
&lt;br /&gt;
 String name = &amp;quot;Jackie Chan&amp;quot;&lt;br /&gt;
&lt;br /&gt;
So basically in Java you specify what type the name variable is. Scala compiler is very smart in the manner that it can automatically detect the type of the variable even if you don't specify it. Consider the following example,&lt;br /&gt;
&lt;br /&gt;
 case class Person(name: String, age: Int)&lt;br /&gt;
 val persons = List(&lt;br /&gt;
                        Person(&amp;quot;Jackie Chan&amp;quot;, 51), Person(&amp;quot;Jet Li&amp;quot;, 41),&lt;br /&gt;
                        Person(&amp;quot;RajniKanth&amp;quot;, 51), Person(&amp;quot;Kamal Hasan&amp;quot;, 44),&lt;br /&gt;
                        Person(&amp;quot;Martin Odersky&amp;quot;, 18), Person(&amp;quot;Steve Yegge&amp;quot;, 23))&lt;br /&gt;
 //notice that you don't have to use 'new' keyword to create new instances. This is taken care of by the companion objects.&lt;br /&gt;
&lt;br /&gt;
The above lines of code create a list with 6 different persons(An immutable list)&lt;br /&gt;
&lt;br /&gt;
Now, as in ruby scala supports the concepts of closure. Let's see what it is.&lt;br /&gt;
&lt;br /&gt;
 val twentyThree: List[Person] = persons.filter( { p: Person =&amp;gt; p.age == 3})&lt;br /&gt;
&lt;br /&gt;
In the above set of lines, the code within the '{ }' brackets is a function's body. Basically what the compiler did was to create a function which takes a single argument and returns either true or false. Now that's called a high level language. Ready for more?&lt;br /&gt;
&lt;br /&gt;
==== A Scalable Language ====&lt;br /&gt;
In almost all programming languages, there is a necessity to profile the running time of the procedures and tune its performance. The old way of doing things in Java would be:&lt;br /&gt;
&lt;br /&gt;
 public class Main{&lt;br /&gt;
 public static int factorial(int x){&lt;br /&gt;
   if(x == 0 || x == 1) return 1&lt;br /&gt;
   else return x * factorial(x-1)&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String [] args){&lt;br /&gt;
    long beforeTime = System.currentTimeMillis();&lt;br /&gt;
    int result = factorial(5);&lt;br /&gt;
    System.out.println(&amp;quot;Total time taken to execute factorial(5) is &amp;quot; + (System.currentTimeMillis() - result)+&amp;quot;ms&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;factorial(5) = &amp;quot; + result);&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
However, if you want to find out the execution time of some other method, you would have to type in all the above lines again. So much for code re-use! Lets see how its done in Scala. Hold on to your seats...&lt;br /&gt;
&lt;br /&gt;
 object Main{&lt;br /&gt;
  def main(args: Array[String]){&lt;br /&gt;
    def factorial(x: Int):Int = x match{&lt;br /&gt;
      case 0 | 1 =&amp;gt; 1&lt;br /&gt;
      case _ =&amp;gt; x * factorial(x-1)&lt;br /&gt;
  }&lt;br /&gt;
  import System.currentTimeMillis&lt;br /&gt;
  def timeIt(msg: String = &amp;quot;Time to execute is: &amp;quot;)(func: =&amp;gt; Unit){&lt;br /&gt;
    val before = currentTimeMillis&lt;br /&gt;
    func&lt;br /&gt;
    println(msg + (currentTimeMillis - before) + &amp;quot;ms&amp;quot;)&lt;br /&gt;
  }&lt;br /&gt;
  timeIt(&amp;quot;Total time taken to execute factorial(5) is &amp;quot;){&lt;br /&gt;
      val result = factorial(5)&lt;br /&gt;
      println(&amp;quot;factorial(5) = &amp;quot; + result)&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 //would print: factorial(5) = 120&lt;br /&gt;
 // Total time taken to execute factorial(5) is 1ms&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That's not right! The function &amp;lt;code&amp;gt;timeIt&amp;lt;/code&amp;gt; looks like a feature inbuilt into the language. Turn your disbelief into amazement;save the above code into a separate file and run it. Infact using this feature, it is possible to build powerful DSL languages which can run at full speed on JVM.&lt;br /&gt;
&lt;br /&gt;
Ruby allows you to add new methods to objects. This feature is often called as '''Monkey Patching''' because you are splitting open the class and adding/overriding methods (in)|(to) the class. Often this leads to unexpected behavior at runtime because, other libraries including into build path may depend on some methods which you changed. Scala provides yet another feature called implicits.&lt;br /&gt;
&lt;br /&gt;
In Java, &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; class is &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; so there is no way you can add new methods to it. But with implicits you can do more! Suppose you have some text in memory and you know it represents a number. But in Java the usual way to convert string to int is to do a &amp;lt;code&amp;gt;Integer.parseInt(string)&amp;lt;/code&amp;gt; which seems so unnecessary. Let us make our lives easier...&lt;br /&gt;
&lt;br /&gt;
  implicit def strToInt(str: String) = new {&lt;br /&gt;
    def asInt = Integer.parseInt(str)&lt;br /&gt;
  }&lt;br /&gt;
  val str = &amp;quot;1234&amp;quot;&lt;br /&gt;
  println(str.asInt * 3) //prints out 3702&lt;br /&gt;
&lt;br /&gt;
The above code gives us an impression as if String class has gained new &amp;lt;code&amp;gt;asInt&amp;lt;/code&amp;gt; method. Can static object oriented languages do this? No! Infact scala cheats by calling our implicits to convert it into a different object as and when needed.&lt;br /&gt;
&lt;br /&gt;
The above functionalities are purely object oriented, yet they are safe. They don't pollute the global namespace unlike Ruby or Python&lt;br /&gt;
&lt;br /&gt;
==== Broad Classification of Functions ====&lt;br /&gt;
===== Predicate =====&lt;br /&gt;
It is a function which receives an object as argument and either returns true or false. They are useful in a number of situations. Suppose you had a list of integers and you wanted to filter out all odd numbers you could apply the predicate on every element in the list and&lt;br /&gt;
* If the predicate returned true, add it to the new list&lt;br /&gt;
* else ignore the current entry and try next.&lt;br /&gt;
&lt;br /&gt;
 scala&amp;gt; val numbers = 0 to 20&lt;br /&gt;
 numbers: scala.collection.immutable.Range.Inclusive with  scala.collection.immutable.Range.ByOne = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)&lt;br /&gt;
 scala&amp;gt; val evenNumbers = numbers.filter( _ % 2 == 0)&lt;br /&gt;
 evenNumbers: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20)&lt;br /&gt;
&lt;br /&gt;
===== Closure =====&lt;br /&gt;
A function which does some operation on an object but does not return any result. In otherwords, the return type of closure is void. Say we have a list of strings and we need to print them all to the console; we can do it the following way:&lt;br /&gt;
&lt;br /&gt;
 scala&amp;gt; val strs = List(&amp;quot;print&amp;quot;, &amp;quot;all&amp;quot;, &amp;quot;to&amp;quot;, &amp;quot;console&amp;quot;)&lt;br /&gt;
 strs: List[java.lang.String] = List(print, all, to, console)&lt;br /&gt;
 scala&amp;gt; strs.foreach(println) //println == System.out.println&lt;br /&gt;
 print&lt;br /&gt;
 all&lt;br /&gt;
 to&lt;br /&gt;
 console&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; is a method which is executed for every element present in the list.&lt;br /&gt;
* &amp;lt;code&amp;gt; str.foreach(println)&amp;lt;/code&amp;gt; - Now, this is getting little confusing. Whats &amp;lt;code&amp;gt;println&amp;lt;/code&amp;gt; doing without any arguments? Scala's compiler is intelligent enough to determine that println takes in a single argument and so allows us to skip specifying it.&lt;br /&gt;
===== Transformer =====&lt;br /&gt;
A function which recieves an entity/object and returns an another object(possibly of different type). Infact both predicate and a closure are transformers. To understand more what a transformer is, let us change the case of all the strings in a list.&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
   val list = List(&amp;quot;Scala&amp;quot;, &amp;quot;is&amp;quot;, &amp;quot;the coolest&amp;quot;, &amp;quot;language&amp;quot;, &amp;quot;in the world!&amp;quot;)&lt;br /&gt;
 scala&amp;gt; list.map(_.toUpperCase)&lt;br /&gt;
 res5: List[java.lang.String] = List(SCALA, IS, THE COOLEST, LANGUAGE, IN THE WORLD!)&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;scala.List's&amp;lt;/code&amp;gt; method which takes in a function as a parameter and for every element in the list invokes the function. The result returned by the function is added to a new list.&lt;br /&gt;
* &amp;lt;code&amp;gt;_.toUpperCase&amp;lt;/code&amp;gt; is the transformer function upcasing all the strings.&lt;br /&gt;
&lt;br /&gt;
==== Currying ====&lt;br /&gt;
Excellent tutorial here: [http://bit.ly/16Qs6l]&lt;br /&gt;
Named after its inventor 'Haskell Curry', it is a way of converting a function which takes multiple arguments, into a function with lesser number of arguments. Theory is always boring or we feel so. Let's jump right in and work out an example. Consider a simple function which adds two numbers.&lt;br /&gt;
&lt;br /&gt;
 def add(x:Int)(y:Int) = x + y&lt;br /&gt;
&lt;br /&gt;
Now assume you want to create a function which will add '5' to all its input and return the sum. Can you re-use the code above? Currying works great in these scenarios..&lt;br /&gt;
 val add5ToInput = add(5) _  // the '_' is to tell the compiler to create a curried version of the above function.&lt;br /&gt;
 println(add5ToInput(10))  //surprise surprise - prints 15!&lt;br /&gt;
&lt;br /&gt;
So basically what happens is that compiler creates an intermediate function which always has &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; as the first parameter. So when we say &amp;lt;code&amp;gt;add5ToInput(10)&amp;lt;/code&amp;gt; in reality, it inturn calls &amp;lt;code&amp;gt;add(5)(10)&amp;lt;/code&amp;gt;. So currying basically helps us reduce the total number of parameters which needs to passed to functions and in turn helps re-use code in a functional way!&lt;br /&gt;
&lt;br /&gt;
==== Pattern Matching ====&lt;br /&gt;
also see [http://bit.ly/3o5JA1]&lt;br /&gt;
Yet another unique feature of functional programming languages is the concept of pattern matching. Let us see what pattern matching is all about. Continuing our previous example of Person objects, let us write some code to guess the profession of a person based on his name.(I know it sounds lame..)&lt;br /&gt;
&lt;br /&gt;
 def guessProfession(p: Person) = p.name match {&lt;br /&gt;
        case &amp;quot;Jackie Chan&amp;quot; | &amp;quot;Jet Li&amp;quot; =&amp;gt; &amp;quot;Martial Artist and Actor&amp;quot;&lt;br /&gt;
        case &amp;quot;RajniKanth&amp;quot; | &amp;quot;Kamal Hasan&amp;quot; =&amp;gt; &amp;quot;South Indian Actor&amp;quot;&lt;br /&gt;
        case &amp;quot;Martin Odersky&amp;quot; | &amp;quot;Steve Yegge&amp;quot; =&amp;gt; &amp;quot;Famous Programmer&amp;quot;&lt;br /&gt;
        case _ =&amp;gt; &amp;quot;I don't know %s&amp;quot;.format(p.name)&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 val p = Person(&amp;quot;Kovalan Venkatesan&amp;quot;, 24)&lt;br /&gt;
 val p1 = Person(&amp;quot;Martin Odersky&amp;quot;, 31)&lt;br /&gt;
 println(guessProfession(p)) //prints I don't know Kovalan Venkatesan.&lt;br /&gt;
 println(guessProfession(p1)) //prints &amp;quot;Famous Programmer&amp;quot;&lt;br /&gt;
&lt;br /&gt;
As you may have noticed, the above example is very similar to switch statements in imperative languages such as C, Java. However, it is more powerful and extensible. Why?&lt;br /&gt;
&lt;br /&gt;
# In both C and Java only numeric types can be used in switch statements.(including enums which are simply names mapped onto integers). In Scala you can use almost use anything you want. Infact scala supports custom extractor patterns, whose scope is beyond this text. Please consider visiting scala resources to learn more.&lt;br /&gt;
# The cases do not allow fall through. In Java and C, you have to manually type &amp;lt;code&amp;gt;break&amp;lt;/code&amp;gt; after the end of every case. Scala does not have any &amp;lt;code&amp;gt;break&amp;lt;/code&amp;gt; nor &amp;lt;code&amp;gt;continue&amp;lt;/code&amp;gt; keywords(shocking?)&lt;br /&gt;
&lt;br /&gt;
A purely object oriented person may argue that we could use Visitor Pattern to do the same. That is true, but how many lines of code would you have to write to accomplish the same functionality? Wanna guess?[http://www.artima.com/weblogs/viewpost.jsp?thread=166742]&lt;br /&gt;
&lt;br /&gt;
More explanation of case statements:&lt;br /&gt;
* &amp;lt;code&amp;gt;match&amp;lt;/code&amp;gt; is equivalent to switch keyword in C, C++ and Java.&lt;br /&gt;
* &amp;lt;code&amp;gt;case&amp;lt;/code&amp;gt; keyword signifies the beginning of the particular case.&lt;br /&gt;
* The &amp;lt;code&amp;gt;_&amp;lt;/code&amp;gt;(underscore) signifies many things in scala. When we say &amp;lt;code&amp;gt;case _ =&amp;gt;&amp;lt;/code&amp;gt;, it is equivalent to the &amp;lt;code&amp;gt;default&amp;lt;/code&amp;gt; keyword in C/Java.&lt;br /&gt;
&lt;br /&gt;
===== Handling exceptions - Scala way! =====&lt;br /&gt;
In Java, exception matching is considered verbose and ugly. To open and write a file,&lt;br /&gt;
 try {&lt;br /&gt;
    File file = new File(&amp;quot;/tmp/sample.txt&amp;quot;);&lt;br /&gt;
    PrintStream printStream = new PrintStream(file);&lt;br /&gt;
    printStream.write(&amp;quot;something&amp;quot;.getBytes());&lt;br /&gt;
 } catch (FileNotFoundException e) {&lt;br /&gt;
    System.out.println(&amp;quot;Unable to find the file /tmp/sample.txt&amp;quot;);&lt;br /&gt;
    e.printStackTrace();&lt;br /&gt;
 } catch (IOException e) {&lt;br /&gt;
   System.out.println(&amp;quot;Unknown IO error&amp;quot;);&lt;br /&gt;
    e.printStackTrace();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
  try{&lt;br /&gt;
    val file = new File(&amp;quot;/tmp/sample.txt&amp;quot;)&lt;br /&gt;
    val ps = new PrintStream(file)&lt;br /&gt;
    ps.write(&amp;quot;something&amp;quot;.getBytes())&lt;br /&gt;
  }catch{&lt;br /&gt;
    case e: FileNotFoundException =&amp;gt; println(&amp;quot;Unable to find file /tmp/sample.txt&amp;quot;)&lt;br /&gt;
    case IOException =&amp;gt; println(&amp;quot;Unknown IO error&amp;quot;)&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
We have just scratched the surface of Scala, but it has innumerable features which show a beautiful blend of functional and object oriented features. Scala proves we can use them both together.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Functional programming integrated with object oriented style leads to:&lt;br /&gt;
&lt;br /&gt;
#Better understanding of program behavior as a result of immutability features&lt;br /&gt;
#Use of efficient recursive techniques such as tail call optimization&lt;br /&gt;
#Reduction in number of lines of code&lt;br /&gt;
&lt;br /&gt;
By using these techniques we utilize the best of both functional and object oriented techniques.&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
*[1]: http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)&lt;br /&gt;
*[2]: http://www.haskell.org&lt;br /&gt;
*[3]: http://www.scala-lang.org&lt;br /&gt;
*[4]: http://bit.ly/16Qs6l&lt;br /&gt;
*[5]: http://bit.ly/3o5JA1&lt;br /&gt;
*[6]: http://www.artima.com/weblogs/viewpost.jsp?thread=166742&lt;br /&gt;
*[7]: http://lamp.epfl.ch/~odersky/&lt;br /&gt;
*[8]: http://www.scala-lang.org/docu/files/collections-api/collections.html#&lt;br /&gt;
*[9]: http://www.codecommit.com/blog/java/interop-between-java-and-scala&lt;br /&gt;
*[10]: http://www.codecommit.com/blog/scala/what-is-hindley-milner-and-why-is-it-cool&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_az&amp;diff=34558</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1e az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_az&amp;diff=34558"/>
		<updated>2010-09-09T02:43:32Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* A Scalable Language */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Programming Paradigms==&lt;br /&gt;
 &lt;br /&gt;
Every computer program needs a style of writing which specifies how to solve a software engineering problem. This style is represented by the paradigm. Each computer program follows one or more paradigm which differs in representing the elements of a program(such as variables and objects) and the steps needed to compute a task.&lt;br /&gt;
&lt;br /&gt;
Diferent paradigms are:&lt;br /&gt;
&lt;br /&gt;
# Procedural/imperative paradigms: Assembly, C, C++, Java, C#&lt;br /&gt;
# Object Oriented paradigm : C++, Java, Python, Ruby, Scala, C#&lt;br /&gt;
# Functional Paradigm : Lisp, Haskell, Clojure, Scala, OCaml, Ruby&lt;br /&gt;
# Logic Paradigm: Prolog&lt;br /&gt;
&lt;br /&gt;
==Multi-Paradigm Programming==&lt;br /&gt;
&lt;br /&gt;
Multiparadigm refers to use of a combination of programming paradigms for solving a computer problem. Some languages subscribe strictly to a single paradigm like Assembly and C. Others like Java, C++, Scala and C# employ more than one paradigm. Every paradigm comes with its own strength and weakness and this quite motivates us to take advantage of each paradigm and use it in a manner that best fits the problem at hand.&lt;br /&gt;
&lt;br /&gt;
==Overview of Functional Programming==&lt;br /&gt;
&lt;br /&gt;
Functional programming is derived from lambda calculus which models computations as the evaluation of functions and recursions. More emphasis is laid on application of functions rather than changing the state of variables in a program. Functional programming relies heavily on recursions and it is the only way to iterate instead of loops.&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;How it differs from imperative programming&amp;lt;/b&amp;gt;===&lt;br /&gt;
    &lt;br /&gt;
Imperative programming follows the Von-Neumann architecture and mainly consists of loops and usage of globa states to perform a computation. For example consider the task of computing the sum of numbers from 1 to n. &lt;br /&gt;
&lt;br /&gt;
In imperative style,&lt;br /&gt;
        &lt;br /&gt;
        sum := 0         // global state&lt;br /&gt;
        for i &amp;lt;- 1 to n do&lt;br /&gt;
            sum := sum + i&lt;br /&gt;
            &lt;br /&gt;
In Functional style,&lt;br /&gt;
    &lt;br /&gt;
         func sum(n)        // note that sum is a function and is recursive&lt;br /&gt;
           if  n = 1 then&lt;br /&gt;
               return 1&lt;br /&gt;
           else&lt;br /&gt;
               return n + sum(n-1)&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Pure and impure functional programming&amp;lt;/b&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
Purely functional programming exhibit referential transparency [http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)] which does not involve any global state or I/O changes. If the same functional expression results in the same output value for the same argument x at different stages of execution , then the function is said to be pure which is devoid of any global state change. For example Haskell [http://www.haskell.org] is purely functional.&lt;br /&gt;
&lt;br /&gt;
==Overview of object oriented programming==&lt;br /&gt;
&lt;br /&gt;
Typially programs were written with a procedural view where the program's logic was given utmost importance and it follows a sequential structure. But object oriented techniques really cares about the data or objects we want to mainpulate with rather than the logic of  a program. Every object contains its own state(in the form of variables) and and a number of methods to manipulate the variables.For example consider the class Add:&lt;br /&gt;
           &lt;br /&gt;
           class Add{&lt;br /&gt;
           /* Object variables */&lt;br /&gt;
                  private int sum = 0;&lt;br /&gt;
          /* Object Methods */&lt;br /&gt;
                  public void calculate_sum(int n){&lt;br /&gt;
                      int i=0;&lt;br /&gt;
                      while(i &amp;lt;= n){&lt;br /&gt;
                          sum = sum + i;&lt;br /&gt;
                          i++;&lt;br /&gt;
                      }&lt;br /&gt;
                  }&lt;br /&gt;
            }&lt;br /&gt;
       &lt;br /&gt;
            Add instance1 = new Add();     // instance1 is an object of type Add&lt;br /&gt;
            instance1.calculate_sum(100);&lt;br /&gt;
           &lt;br /&gt;
===&amp;lt;b&amp;gt;Principles of object oriented design&amp;lt;/b&amp;gt;===&lt;br /&gt;
           &lt;br /&gt;
1. &amp;lt;b&amp;gt;Encapsulation:&amp;lt;/b&amp;gt; Encapsulation refers to hiding the internal representation of the object from the outside view. For example algorithm to compute the calculate_sum() method may be hidden, but it will present the user with the expected results.&lt;br /&gt;
&lt;br /&gt;
2. &amp;lt;b&amp;gt;Polymorphism:&amp;lt;/b&amp;gt; Polymorphism means ability to take multiple forms. For example an operation may exhibit different behaviour at different instances. Consider the operator '+', for numbers it will generate the sum, but for strings it will produce a third string which concatenates the input strings.&lt;br /&gt;
&lt;br /&gt;
3. &amp;lt;b&amp;gt;Inheritance:&amp;lt;/b&amp;gt; Inheritance involves base and derived classes with derived class inheriting all the methods and states frm the base class. Inheritance basically forms a hirerachy. For example if shape is an object, then objects square, rectangle, circle all derive the same characteristic as shape does.&lt;br /&gt;
&lt;br /&gt;
==Functional + OOP code==&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Functions as Objects&amp;lt;/b&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
One of the cornerstone of functional and object oriented code is treating functions as objects.It means that we can pass functions as arguments, store it and return them from other functions. This is highly useful in an user interface code where it can be used as call back functions which get called when an event occurs( in event driven programming)&lt;br /&gt;
&lt;br /&gt;
An example in Scala [http://www.scala-lang.org]:&lt;br /&gt;
&lt;br /&gt;
  Object Timer{&lt;br /&gt;
    def  action(callback() : () =&amp;gt; unit){              // callback() is the function passed as an argument&lt;br /&gt;
         while( some condition)                                                            &lt;br /&gt;
         { &lt;br /&gt;
            callback();&lt;br /&gt;
            Thread.sleep(3000)&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
     def event(){                                      //  event is the method which tells what to do &lt;br /&gt;
         /* process the event here&lt;br /&gt;
         &lt;br /&gt;
         */      &lt;br /&gt;
     }&lt;br /&gt;
        &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Another example of functional concept in object oriented design is the concept of blocks especially in Ruby. Blocks are basically nameless functions which can be passed to a function and then that function can invoke the passed in nameless function. This is a common style called higher order function style among languages that handle functions as first class objects. Basically blocks can be designed for loop abstraction and lets the user to design their own way of iterating the values, thereby hiding the implementation details. &lt;br /&gt;
For example if we want to iterate backwards from the end to the beginning without revealing its internal implementations, we could implement the loop logic inside the block and pass it to a method or function.&lt;br /&gt;
&lt;br /&gt;
Here is a Ruby implementation of block&lt;br /&gt;
 &lt;br /&gt;
 def printlist(array,&amp;amp;reverse)&lt;br /&gt;
    array.each do |element|&lt;br /&gt;
       puts &amp;quot;array element: #{element}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    reverse.call(array)   /* Here it prints the reverse implementation of list*/&lt;br /&gt;
    end&lt;br /&gt;
  &lt;br /&gt;
 printlist([1,2,3,4,5,6]) { |array| &lt;br /&gt;
   array.reverse.each do |element| &lt;br /&gt;
     puts &amp;quot;array element: #{element}&amp;quot; &lt;br /&gt;
   end &lt;br /&gt;
 }&lt;br /&gt;
Next we will take a look at some of the features of Scala which supports a blend of functional and object oriented concepts in a concise manner.&lt;br /&gt;
&lt;br /&gt;
= Scala =&lt;br /&gt;
Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages&lt;br /&gt;
&lt;br /&gt;
Scala can be considered a very good blend of the best of functional and object oriented world. It was conceived(in 2001) and implemented by Martin Odersky who was also the author of Sun javac compiler and played a major role in the design and retrofitting of generics into Java. The latest version of Scala is 2.8 and includes a number of performance improvements over previous versions. We shall see more about the features of Scala in the following pages, but for fun, some facts!&lt;br /&gt;
&lt;br /&gt;
=== Features ===&lt;br /&gt;
==== Statically Typed ====&lt;br /&gt;
Scala belongs to the family of languages which do not allow the type of variables once they have been defined. In other words, the type information of each and every variable is encoded in the generated code and the compiler makes sure, you are not doing something funny. For example, lets consider the following example,&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
   irb(main):001:0&amp;gt; str=&amp;quot;Hello World&amp;quot;&lt;br /&gt;
    =&amp;gt; &amp;quot;Hello World&amp;quot;&lt;br /&gt;
    irb(main):002:0&amp;gt; puts str.class&lt;br /&gt;
    String&lt;br /&gt;
    irb(main):003:0&amp;gt; str=123&lt;br /&gt;
    =&amp;gt; 123&lt;br /&gt;
    irb(main):004:0&amp;gt; puts str.class&lt;br /&gt;
    Fixnum&lt;br /&gt;
&lt;br /&gt;
In the above example, first we define 'str' variable to be of type String and then as a number. So basically, no type information is associated with the variable at any point in the program. Hence we can easily reassociate that variable with an object/instance&lt;br /&gt;
of a different type. This is a feature of all dynamically typed languages(like Ruby, Python, Clojure). However most of the statically typed languages such as Scala, Java, C/C++ do not allow you do so. In Scala,&lt;br /&gt;
&lt;br /&gt;
  scala&amp;gt; var str=&amp;quot;Hello World&amp;quot;&lt;br /&gt;
  str: java.lang.String = Hello World&lt;br /&gt;
  scala&amp;gt; str=123&lt;br /&gt;
  &amp;lt;console&amp;gt;:6: error: type mismatch;&lt;br /&gt;
   found   : Int(123)&lt;br /&gt;
   required: java.lang.String&lt;br /&gt;
         str=123&lt;br /&gt;
==== Mixed paradigm ====&lt;br /&gt;
Scala is a strange specimen. It is a complete blend of object oriented and functional programming. How can this happen? Functional world advocates immutability but object oriented world is all about state and how do you mutate it to fit your needs. Scala allows you create both mutable and immutable objects. Consider the following example,&lt;br /&gt;
 val str: String = &amp;quot;Hello World&amp;quot;&lt;br /&gt;
In the first line, I am creating a immutable variable(a misnomer indeed). Roughly translating the above line to Java is below:&lt;br /&gt;
&lt;br /&gt;
 final String str = &amp;quot;Hello World&amp;quot;; //note the extra semi-colon&lt;br /&gt;
Consider the next example below,&lt;br /&gt;
 val list: List[String] = List(&amp;quot;Scala&amp;quot;, &amp;quot;is&amp;quot;, &amp;quot;the coolest&amp;quot;, &amp;quot;language&amp;quot;, &amp;quot;in the world!&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The same code can be written in Java as below&lt;br /&gt;
&lt;br /&gt;
  List&amp;lt;String&amp;gt; mutableList = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
  list.add(&amp;quot;Scala&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;is&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;the coolest&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;language&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;in the world!&amp;quot;);&lt;br /&gt;
  List&amp;lt;String&amp;gt; immutableList = Collections.immutableList(mutableList);&lt;br /&gt;
&lt;br /&gt;
Compared to Java, Scala reduces the total number of lines you need to type and at the same time looks so much simpler and less verbose. Even though the above example shows immutable collection example, scala also provides mutable collection and object types under the package &amp;lt;code&amp;gt;scala.collection.mutable.*&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As it can be observed, even though scala advocates immutability, it does not restrict you from creating mutable objects. So its the best of both worlds. Another part of functional world is the concept of functions as ''''first class members'''' of the language. Some of the common languages like Ruby, Python and JavaScript all incorporate this feature. In the sense you can pass around functions to methods as a parameter. Enough talking;&lt;br /&gt;
&lt;br /&gt;
Let us have a class say &amp;lt;code&amp;gt;'Person'&amp;lt;/code&amp;gt; which has properties, &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; and another variable &amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt;.&lt;br /&gt;
In Java:&lt;br /&gt;
&lt;br /&gt;
 class Person{&lt;br /&gt;
    private String name;&lt;br /&gt;
    private int age;&lt;br /&gt;
    public String getName(){&lt;br /&gt;
        return name;&lt;br /&gt;
    }&lt;br /&gt;
    public void setName(String s){&lt;br /&gt;
        this.name = s;&lt;br /&gt;
    }&lt;br /&gt;
    public int getAge(){&lt;br /&gt;
        return age;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAge(int age){&lt;br /&gt;
        this.age = age;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
 case class Person(name: String, age: Int)&lt;br /&gt;
&lt;br /&gt;
This single line is equivalent to the above java code! So much cooler.. I would not be doing justice if I don't mention the fact that getters for &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt; are generated. Also equals() and hashCode() method are automatically generated for you by the compiler. Isn't this an awesome feature? No wonder Scala is considered a beautiful and high level language. Now assume we have a list of persons and we want to filter out all the persons who are of age 23. How would you do that in Java?&lt;br /&gt;
&lt;br /&gt;
  List&amp;lt;Person&amp;gt; persons; //has a bunch of person objects in it.&lt;br /&gt;
  List&amp;lt;Person&amp;gt; twentyThree = new ArrayList&amp;lt;Person&amp;gt;();&lt;br /&gt;
  for(Person p: persons){&lt;br /&gt;
      if(p.getAge() == 23){&lt;br /&gt;
          twentyThree.add(p);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
&lt;br /&gt;
 val twentyThree: List[Person] = persons.filter( _.age == 23)&lt;br /&gt;
&lt;br /&gt;
That's it! In case you are wondering whats going on, filter is a method present on scala.List class and the code block  _.age == 23 is a function created on the fly and passed onto the filter method. More precisely its called a closure - a functional concept which can be emulated in java only using anonymous inner classes. In case you are wondering how scala does this, scala's compiler transforms our one line function into some anonymous inner classes when generating java byte code but allows developer to define functions in a simple manner.&lt;br /&gt;
&lt;br /&gt;
Infact every function is an object in scala. Scala's standard library has a number of functions which are object themselves.&lt;br /&gt;
&lt;br /&gt;
==== Sophisticated Syntax/Features ====&lt;br /&gt;
When we say sophisticated, its really an awesome feature which is present in other functional languages like Haskell, (O)Caml and Erlang. Consider the following example,&lt;br /&gt;
&lt;br /&gt;
   val name = &amp;quot;Jackie Chan&amp;quot;&lt;br /&gt;
   println(name.getClass) //prints out String.&lt;br /&gt;
&lt;br /&gt;
Previously we saw that scala is a statically typed language and yet in the above example we don't specify what type is the variable name. In Java, you would type in the following:&lt;br /&gt;
&lt;br /&gt;
 String name = &amp;quot;Jackie Chan&amp;quot;&lt;br /&gt;
&lt;br /&gt;
So basically in Java you specify what type the name variable is. Scala compiler is very smart in the manner that it can automatically detect the type of the variable even if you don't specify it. Consider the following example,&lt;br /&gt;
&lt;br /&gt;
 case class Person(name: String, age: Int)&lt;br /&gt;
 val persons = List(&lt;br /&gt;
                        Person(&amp;quot;Jackie Chan&amp;quot;, 51), Person(&amp;quot;Jet Li&amp;quot;, 41),&lt;br /&gt;
                        Person(&amp;quot;RajniKanth&amp;quot;, 51), Person(&amp;quot;Kamal Hasan&amp;quot;, 44),&lt;br /&gt;
                        Person(&amp;quot;Martin Odersky&amp;quot;, 18), Person(&amp;quot;Steve Yegge&amp;quot;, 23))&lt;br /&gt;
 //notice that you don't have to use 'new' keyword to create new instances. This is taken care of by the companion objects.&lt;br /&gt;
&lt;br /&gt;
The above lines of code create a list with 6 different persons(An immutable list)&lt;br /&gt;
&lt;br /&gt;
Now, as in ruby scala supports the concepts of closure. Let's see what it is.&lt;br /&gt;
&lt;br /&gt;
 val twentyThree: List[Person] = persons.filter( { p: Person =&amp;gt; p.age == 3})&lt;br /&gt;
&lt;br /&gt;
In the above set of lines, the code within the '{ }' brackets is a function's body. Basically what the compiler did was to create a function which takes a single argument and returns either true or false. Now that's called a high level language. Ready for more?&lt;br /&gt;
&lt;br /&gt;
==== A Scalable Language ====&lt;br /&gt;
In almost all programming languages, there is a necessity to profile the running time of the procedures and tune its performance. The old way of doing things in Java would be:&lt;br /&gt;
&lt;br /&gt;
 public class Main{&lt;br /&gt;
 public static int factorial(int x){&lt;br /&gt;
   if(x == 0 || x == 1) return 1&lt;br /&gt;
   else return x * factorial(x-1)&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String [] args){&lt;br /&gt;
    long beforeTime = System.currentTimeMillis();&lt;br /&gt;
    int result = factorial(5);&lt;br /&gt;
    System.out.println(&amp;quot;Total time taken to execute factorial(5) is &amp;quot; + (System.currentTimeMillis() - result)+&amp;quot;ms&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;factorial(5) = &amp;quot; + result);&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
However, if you want to find out the execution time of some other method, you would have to type in all the above lines again. So much for code re-use! Lets see how its done in Scala. Hold on to your seats...&lt;br /&gt;
&lt;br /&gt;
 object Main{&lt;br /&gt;
  def main(args: Array[String]){&lt;br /&gt;
    def factorial(x: Int):Int = x match{&lt;br /&gt;
      case 0 | 1 =&amp;gt; 1&lt;br /&gt;
      case _ =&amp;gt; x * factorial(x-1)&lt;br /&gt;
  }&lt;br /&gt;
  import System.currentTimeMillis&lt;br /&gt;
  def timeIt(msg: String = &amp;quot;Time to execute is: &amp;quot;)(func: =&amp;gt; Unit){&lt;br /&gt;
    val before = currentTimeMillis&lt;br /&gt;
    func&lt;br /&gt;
    println(msg + (currentTimeMillis - before) + &amp;quot;ms&amp;quot;)&lt;br /&gt;
  }&lt;br /&gt;
  timeIt(&amp;quot;Total time taken to execute factorial(5) is &amp;quot;){&lt;br /&gt;
      val result = factorial(5)&lt;br /&gt;
      println(&amp;quot;factorial(5) = &amp;quot; + result)&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 //would print: factorial(5) = 120&lt;br /&gt;
 // Total time taken to execute factorial(5) is 1ms&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That's not right! The function &amp;lt;code&amp;gt;timeIt&amp;lt;/code&amp;gt; looks like a feature inbuilt into the language. Turn your disbelief into amazement;save the above code into a separate file and run it. Infact using this feature, it is possible to build powerful DSL languages which can run at full speed on JVM.&lt;br /&gt;
&lt;br /&gt;
Ruby allows you to add new methods to objects. This feature is often called as '''Monkey Patching''' because you are splitting open the class and adding/overriding methods (in)|(to) the class. Often this leads to unexpected behavior at runtime because, other libraries including into build path may depend on some methods which you changed. Scala provides yet another feature called implicits.&lt;br /&gt;
&lt;br /&gt;
In Java, &amp;lt;code&amp;gt;String&amp;lt;/code&amp;gt; class is &amp;lt;code&amp;gt;final&amp;lt;/code&amp;gt; so there is no way you can add new methods to it. But with implicits you can do more! Suppose you have some text in memory and you know it represents a number. But in Java the usual way to convert string to int is to do a &amp;lt;code&amp;gt;Integer.parseInt(string)&amp;lt;/code&amp;gt; which seems so unnecessary. Let us make our lives easier...&lt;br /&gt;
&lt;br /&gt;
  implicit def strToInt(str: String) = new {&lt;br /&gt;
    def asInt = Integer.parseInt(str)&lt;br /&gt;
  }&lt;br /&gt;
  val str = &amp;quot;1234&amp;quot;&lt;br /&gt;
  println(str.asInt * 3) //prints out 3702&lt;br /&gt;
&lt;br /&gt;
The above code gives us an impression as if String class has gained new &amp;lt;code&amp;gt;asInt&amp;lt;/code&amp;gt; method. Can static object oriented languages do this? No! Infact scala cheats by calling our implicits to convert it into a different object as and when needed.&lt;br /&gt;
&lt;br /&gt;
The above functionalities are purely object oriented, yet they are safe. They don't pollute the global namespace unlike Ruby or Python&lt;br /&gt;
&lt;br /&gt;
==== Broad Classification of Functions ====&lt;br /&gt;
===== Predicate =====&lt;br /&gt;
It is a function which receives an object as argument and either returns true or false. They are useful in a number of situations. Suppose you had a list of integers and you wanted to filter out all odd numbers you could apply the predicate on every element in the list and&lt;br /&gt;
* If the predicate returned true, add it to the new list&lt;br /&gt;
* else ignore the current entry and try next.&lt;br /&gt;
&lt;br /&gt;
 scala&amp;gt; val numbers = 0 to 20&lt;br /&gt;
 numbers: scala.collection.immutable.Range.Inclusive with  scala.collection.immutable.Range.ByOne = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)&lt;br /&gt;
 scala&amp;gt; val evenNumbers = numbers.filter( _ % 2 == 0)&lt;br /&gt;
 evenNumbers: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20)&lt;br /&gt;
&lt;br /&gt;
===== Closure =====&lt;br /&gt;
A function which does some operation on an object but does not return any result. In otherwords, the return type of closure is void. Say we have a list of strings and we need to print them all to the console; we can do it the following way:&lt;br /&gt;
&lt;br /&gt;
 scala&amp;gt; val strs = List(&amp;quot;print&amp;quot;, &amp;quot;all&amp;quot;, &amp;quot;to&amp;quot;, &amp;quot;console&amp;quot;)&lt;br /&gt;
 strs: List[java.lang.String] = List(print, all, to, console)&lt;br /&gt;
 scala&amp;gt; strs.foreach(println) //println == System.out.println&lt;br /&gt;
 print&lt;br /&gt;
 all&lt;br /&gt;
 to&lt;br /&gt;
 console&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; is a method which is executed for every element present in the list.&lt;br /&gt;
* &amp;lt;code&amp;gt; str.foreach(println)&amp;lt;/code&amp;gt; - Now, this is getting little confusing. Whats &amp;lt;code&amp;gt;println&amp;lt;/code&amp;gt; doing without any arguments? Scala's compiler is intelligent enough to determine that println takes in a single argument and so allows us to skip specifying it.&lt;br /&gt;
===== Transformer =====&lt;br /&gt;
A function which recieves an entity/object and returns an another object(possibly of different type). Infact both predicate and a closure are transformers. To understand more what a transformer is, let us change the case of all the strings in a list.&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
   val list = List(&amp;quot;Scala&amp;quot;, &amp;quot;is&amp;quot;, &amp;quot;the coolest&amp;quot;, &amp;quot;language&amp;quot;, &amp;quot;in the world!&amp;quot;)&lt;br /&gt;
 scala&amp;gt; list.map(_.toUpperCase)&lt;br /&gt;
 res5: List[java.lang.String] = List(SCALA, IS, THE COOLEST, LANGUAGE, IN THE WORLD!)&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;scala.List's&amp;lt;/code&amp;gt; method which takes in a function as a parameter and for every element in the list invokes the function. The result returned by the function is added to a new list.&lt;br /&gt;
* &amp;lt;code&amp;gt;_.toUpperCase&amp;lt;/code&amp;gt; is the transformer function upcasing all the strings.&lt;br /&gt;
&lt;br /&gt;
==== Currying ====&lt;br /&gt;
Excellent tutorial here: [http://bit.ly/16Qs6l]&lt;br /&gt;
Named after its inventor 'Haskell Curry', it is a way of converting a function which takes multiple arguments, into a function with lesser number of arguments. Theory is always boring or we feel so. Let's jump right in and work out an example. Consider a simple function which adds two numbers.&lt;br /&gt;
&lt;br /&gt;
 def add(x:Int)(y:Int) = x + y&lt;br /&gt;
&lt;br /&gt;
Now assume you want to create a function which will add '5' to all its input and return the sum. Can you re-use the code above? Currying works great in these scenarios..&lt;br /&gt;
 val add5ToInput = add(5) _  // the '_' is to tell the compiler to create a curried version of the above function.&lt;br /&gt;
 println(add5ToInput(10))  //surprise surprise - prints 15!&lt;br /&gt;
&lt;br /&gt;
So basically what happens is that compiler creates an intermediate function which always has &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; as the first parameter. So when we say &amp;lt;code&amp;gt;add5ToInput(10)&amp;lt;/code&amp;gt; in reality, it inturn calls &amp;lt;code&amp;gt;add(5)(10)&amp;lt;/code&amp;gt;. So currying basically helps us reduce the total number of parameters which needs to passed to functions and in turn helps re-use code in a functional way!&lt;br /&gt;
&lt;br /&gt;
==== Pattern Matching ====&lt;br /&gt;
also see [http://bit.ly/3o5JA1]&lt;br /&gt;
Yet another unique feature of functional programming languages is the concept of pattern matching. Let us see what pattern matching is all about. Continuing our previous example of Person objects, let us write some code to guess the profession of a person based on his name.(I know it sounds lame..)&lt;br /&gt;
&lt;br /&gt;
 def guessProfession(p: Person) = p.name match {&lt;br /&gt;
        case &amp;quot;Jackie Chan&amp;quot; | &amp;quot;Jet Li&amp;quot; =&amp;gt; &amp;quot;Martial Artist and Actor&amp;quot;&lt;br /&gt;
        case &amp;quot;RajniKanth&amp;quot; | &amp;quot;Kamal Hasan&amp;quot; =&amp;gt; &amp;quot;South Indian Actor&amp;quot;&lt;br /&gt;
        case &amp;quot;Martin Odersky&amp;quot; | &amp;quot;Steve Yegge&amp;quot; =&amp;gt; &amp;quot;Famous Programmer&amp;quot;&lt;br /&gt;
        case _ =&amp;gt; &amp;quot;I don't know %s&amp;quot;.format(p.name)&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 val p = Person(&amp;quot;Kovalan Venkatesan&amp;quot;, 24)&lt;br /&gt;
 val p1 = Person(&amp;quot;Martin Odersky&amp;quot;, 31)&lt;br /&gt;
 println(guessProfession(p)) //prints I don't know Kovalan Venkatesan.&lt;br /&gt;
 println(guessProfession(p1)) //prints &amp;quot;Famous Programmer&amp;quot;&lt;br /&gt;
&lt;br /&gt;
As you may have noticed, the above example is very similar to switch statements in imperative languages such as C, Java. However, it is more powerful and extensible. Why?&lt;br /&gt;
&lt;br /&gt;
# In both C and Java only numeric types can be used in switch statements.(including enums which are simply names mapped onto integers). In Scala you can use almost use anything you want. Infact scala supports custom extractor patterns, whose scope is beyond this text. Please consider visiting scala resources to learn more.&lt;br /&gt;
# The cases do not allow fall through. In Java and C, you have to manually type &amp;lt;code&amp;gt;break&amp;lt;/code&amp;gt; after the end of every case. Scala does not have any &amp;lt;code&amp;gt;break&amp;lt;/code&amp;gt; nor &amp;lt;code&amp;gt;continue&amp;lt;/code&amp;gt; keywords(shocking?)&lt;br /&gt;
&lt;br /&gt;
A purely object oriented person may argue that we could use Visitor Pattern to do the same. That is true, but how many lines of code would you have to write to accomplish the same functionality? Wanna guess?[http://www.artima.com/weblogs/viewpost.jsp?thread=166742]&lt;br /&gt;
&lt;br /&gt;
More explanation of case statements:&lt;br /&gt;
* &amp;lt;code&amp;gt;match&amp;lt;/code&amp;gt; is equivalent to switch keyword in C, C++ and Java.&lt;br /&gt;
* &amp;lt;code&amp;gt;case&amp;lt;/code&amp;gt; keyword signifies the beginning of the particular case.&lt;br /&gt;
* The &amp;lt;code&amp;gt;_&amp;lt;/code&amp;gt;(underscore) signifies many things in scala. When we say &amp;lt;code&amp;gt;case _ =&amp;gt;&amp;lt;/code&amp;gt;, it is equivalent to the &amp;lt;code&amp;gt;default&amp;lt;/code&amp;gt; keyword in C/Java.&lt;br /&gt;
&lt;br /&gt;
===== Handling exceptions - Scala way! =====&lt;br /&gt;
In Java, exception matching is considered verbose and ugly. To open and write a file,&lt;br /&gt;
 try {&lt;br /&gt;
    File file = new File(&amp;quot;/tmp/sample.txt&amp;quot;);&lt;br /&gt;
    PrintStream printStream = new PrintStream(file);&lt;br /&gt;
    printStream.write(&amp;quot;something&amp;quot;.getBytes());&lt;br /&gt;
 } catch (FileNotFoundException e) {&lt;br /&gt;
    System.out.println(&amp;quot;Unable to find the file /tmp/sample.txt&amp;quot;);&lt;br /&gt;
    e.printStackTrace();&lt;br /&gt;
 } catch (IOException e) {&lt;br /&gt;
   System.out.println(&amp;quot;Unknown IO error&amp;quot;);&lt;br /&gt;
    e.printStackTrace();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
  try{&lt;br /&gt;
    val file = new File(&amp;quot;/tmp/sample.txt&amp;quot;)&lt;br /&gt;
    val ps = new PrintStream(file)&lt;br /&gt;
    ps.write(&amp;quot;something&amp;quot;.getBytes())&lt;br /&gt;
  }catch{&lt;br /&gt;
    case e: FileNotFoundException =&amp;gt; println(&amp;quot;Unable to find file /tmp/sample.txt&amp;quot;)&lt;br /&gt;
    case IOException =&amp;gt; println(&amp;quot;Unknown IO error&amp;quot;)&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
We have just scratched the surface of Scala, but it has innumerable features which show a beautiful blend of functional and object oriented features. Scala proves we can use them both together.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Functional programming integrated with object oriented style leads to:&lt;br /&gt;
&lt;br /&gt;
#Better understanding of program behavior as a result of immutability features&lt;br /&gt;
#Use of efficient recursive techniques such as tail call optimization&lt;br /&gt;
#Reduction in number of lines of code&lt;br /&gt;
&lt;br /&gt;
By using these techniques we utilize the best of both functional and object oriented techniques.&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
*[1]: http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)&lt;br /&gt;
*[2]: http://www.haskell.org&lt;br /&gt;
*[3]: http://www.scala-lang.org&lt;br /&gt;
*[4]: http://bit.ly/16Qs6l&lt;br /&gt;
*[5]: http://bit.ly/3o5JA1&lt;br /&gt;
*[6]: http://www.artima.com/weblogs/viewpost.jsp?thread=166742&lt;br /&gt;
*[7]: http://lamp.epfl.ch/~odersky/&lt;br /&gt;
*[8]: http://www.scala-lang.org/docu/files/collections-api/collections.html#&lt;br /&gt;
*[9]: http://www.codecommit.com/blog/java/interop-between-java-and-scala&lt;br /&gt;
*[10]: http://www.codecommit.com/blog/scala/what-is-hindley-milner-and-why-is-it-cool&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_az&amp;diff=34552</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1e az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_az&amp;diff=34552"/>
		<updated>2010-09-09T02:42:17Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Pattern Matching */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Programming Paradigms==&lt;br /&gt;
 &lt;br /&gt;
Every computer program needs a style of writing which specifies how to solve a software engineering problem. This style is represented by the paradigm. Each computer program follows one or more paradigm which differs in representing the elements of a program(such as variables and objects) and the steps needed to compute a task.&lt;br /&gt;
&lt;br /&gt;
Diferent paradigms are:&lt;br /&gt;
&lt;br /&gt;
# Procedural/imperative paradigms: Assembly, C, C++, Java, C#&lt;br /&gt;
# Object Oriented paradigm : C++, Java, Python, Ruby, Scala, C#&lt;br /&gt;
# Functional Paradigm : Lisp, Haskell, Clojure, Scala, OCaml, Ruby&lt;br /&gt;
# Logic Paradigm: Prolog&lt;br /&gt;
&lt;br /&gt;
==Multi-Paradigm Programming==&lt;br /&gt;
&lt;br /&gt;
Multiparadigm refers to use of a combination of programming paradigms for solving a computer problem. Some languages subscribe strictly to a single paradigm like Assembly and C. Others like Java, C++, Scala and C# employ more than one paradigm. Every paradigm comes with its own strength and weakness and this quite motivates us to take advantage of each paradigm and use it in a manner that best fits the problem at hand.&lt;br /&gt;
&lt;br /&gt;
==Overview of Functional Programming==&lt;br /&gt;
&lt;br /&gt;
Functional programming is derived from lambda calculus which models computations as the evaluation of functions and recursions. More emphasis is laid on application of functions rather than changing the state of variables in a program. Functional programming relies heavily on recursions and it is the only way to iterate instead of loops.&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;How it differs from imperative programming&amp;lt;/b&amp;gt;===&lt;br /&gt;
    &lt;br /&gt;
Imperative programming follows the Von-Neumann architecture and mainly consists of loops and usage of globa states to perform a computation. For example consider the task of computing the sum of numbers from 1 to n. &lt;br /&gt;
&lt;br /&gt;
In imperative style,&lt;br /&gt;
        &lt;br /&gt;
        sum := 0         // global state&lt;br /&gt;
        for i &amp;lt;- 1 to n do&lt;br /&gt;
            sum := sum + i&lt;br /&gt;
            &lt;br /&gt;
In Functional style,&lt;br /&gt;
    &lt;br /&gt;
         func sum(n)        // note that sum is a function and is recursive&lt;br /&gt;
           if  n = 1 then&lt;br /&gt;
               return 1&lt;br /&gt;
           else&lt;br /&gt;
               return n + sum(n-1)&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Pure and impure functional programming&amp;lt;/b&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
Purely functional programming exhibit referential transparency [http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)] which does not involve any global state or I/O changes. If the same functional expression results in the same output value for the same argument x at different stages of execution , then the function is said to be pure which is devoid of any global state change. For example Haskell [http://www.haskell.org] is purely functional.&lt;br /&gt;
&lt;br /&gt;
==Overview of object oriented programming==&lt;br /&gt;
&lt;br /&gt;
Typially programs were written with a procedural view where the program's logic was given utmost importance and it follows a sequential structure. But object oriented techniques really cares about the data or objects we want to mainpulate with rather than the logic of  a program. Every object contains its own state(in the form of variables) and and a number of methods to manipulate the variables.For example consider the class Add:&lt;br /&gt;
           &lt;br /&gt;
           class Add{&lt;br /&gt;
           /* Object variables */&lt;br /&gt;
                  private int sum = 0;&lt;br /&gt;
          /* Object Methods */&lt;br /&gt;
                  public void calculate_sum(int n){&lt;br /&gt;
                      int i=0;&lt;br /&gt;
                      while(i &amp;lt;= n){&lt;br /&gt;
                          sum = sum + i;&lt;br /&gt;
                          i++;&lt;br /&gt;
                      }&lt;br /&gt;
                  }&lt;br /&gt;
            }&lt;br /&gt;
       &lt;br /&gt;
            Add instance1 = new Add();     // instance1 is an object of type Add&lt;br /&gt;
            instance1.calculate_sum(100);&lt;br /&gt;
           &lt;br /&gt;
===&amp;lt;b&amp;gt;Principles of object oriented design&amp;lt;/b&amp;gt;===&lt;br /&gt;
           &lt;br /&gt;
1. &amp;lt;b&amp;gt;Encapsulation:&amp;lt;/b&amp;gt; Encapsulation refers to hiding the internal representation of the object from the outside view. For example algorithm to compute the calculate_sum() method may be hidden, but it will present the user with the expected results.&lt;br /&gt;
&lt;br /&gt;
2. &amp;lt;b&amp;gt;Polymorphism:&amp;lt;/b&amp;gt; Polymorphism means ability to take multiple forms. For example an operation may exhibit different behaviour at different instances. Consider the operator '+', for numbers it will generate the sum, but for strings it will produce a third string which concatenates the input strings.&lt;br /&gt;
&lt;br /&gt;
3. &amp;lt;b&amp;gt;Inheritance:&amp;lt;/b&amp;gt; Inheritance involves base and derived classes with derived class inheriting all the methods and states frm the base class. Inheritance basically forms a hirerachy. For example if shape is an object, then objects square, rectangle, circle all derive the same characteristic as shape does.&lt;br /&gt;
&lt;br /&gt;
==Functional + OOP code==&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Functions as Objects&amp;lt;/b&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
One of the cornerstone of functional and object oriented code is treating functions as objects.It means that we can pass functions as arguments, store it and return them from other functions. This is highly useful in an user interface code where it can be used as call back functions which get called when an event occurs( in event driven programming)&lt;br /&gt;
&lt;br /&gt;
An example in Scala [http://www.scala-lang.org]:&lt;br /&gt;
&lt;br /&gt;
  Object Timer{&lt;br /&gt;
    def  action(callback() : () =&amp;gt; unit){              // callback() is the function passed as an argument&lt;br /&gt;
         while( some condition)                                                            &lt;br /&gt;
         { &lt;br /&gt;
            callback();&lt;br /&gt;
            Thread.sleep(3000)&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
     def event(){                                      //  event is the method which tells what to do &lt;br /&gt;
         /* process the event here&lt;br /&gt;
         &lt;br /&gt;
         */      &lt;br /&gt;
     }&lt;br /&gt;
        &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Another example of functional concept in object oriented design is the concept of blocks especially in Ruby. Blocks are basically nameless functions which can be passed to a function and then that function can invoke the passed in nameless function. This is a common style called higher order function style among languages that handle functions as first class objects. Basically blocks can be designed for loop abstraction and lets the user to design their own way of iterating the values, thereby hiding the implementation details. &lt;br /&gt;
For example if we want to iterate backwards from the end to the beginning without revealing its internal implementations, we could implement the loop logic inside the block and pass it to a method or function.&lt;br /&gt;
&lt;br /&gt;
Here is a Ruby implementation of block&lt;br /&gt;
 &lt;br /&gt;
 def printlist(array,&amp;amp;reverse)&lt;br /&gt;
    array.each do |element|&lt;br /&gt;
       puts &amp;quot;array element: #{element}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    reverse.call(array)   /* Here it prints the reverse implementation of list*/&lt;br /&gt;
    end&lt;br /&gt;
  &lt;br /&gt;
 printlist([1,2,3,4,5,6]) { |array| &lt;br /&gt;
   array.reverse.each do |element| &lt;br /&gt;
     puts &amp;quot;array element: #{element}&amp;quot; &lt;br /&gt;
   end &lt;br /&gt;
 }&lt;br /&gt;
Next we will take a look at some of the features of Scala which supports a blend of functional and object oriented concepts in a concise manner.&lt;br /&gt;
&lt;br /&gt;
= Scala =&lt;br /&gt;
Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages&lt;br /&gt;
&lt;br /&gt;
Scala can be considered a very good blend of the best of functional and object oriented world. It was conceived(in 2001) and implemented by Martin Odersky who was also the author of Sun javac compiler and played a major role in the design and retrofitting of generics into Java. The latest version of Scala is 2.8 and includes a number of performance improvements over previous versions. We shall see more about the features of Scala in the following pages, but for fun, some facts!&lt;br /&gt;
&lt;br /&gt;
=== Features ===&lt;br /&gt;
==== Statically Typed ====&lt;br /&gt;
Scala belongs to the family of languages which do not allow the type of variables once they have been defined. In other words, the type information of each and every variable is encoded in the generated code and the compiler makes sure, you are not doing something funny. For example, lets consider the following example,&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
   irb(main):001:0&amp;gt; str=&amp;quot;Hello World&amp;quot;&lt;br /&gt;
    =&amp;gt; &amp;quot;Hello World&amp;quot;&lt;br /&gt;
    irb(main):002:0&amp;gt; puts str.class&lt;br /&gt;
    String&lt;br /&gt;
    irb(main):003:0&amp;gt; str=123&lt;br /&gt;
    =&amp;gt; 123&lt;br /&gt;
    irb(main):004:0&amp;gt; puts str.class&lt;br /&gt;
    Fixnum&lt;br /&gt;
&lt;br /&gt;
In the above example, first we define 'str' variable to be of type String and then as a number. So basically, no type information is associated with the variable at any point in the program. Hence we can easily reassociate that variable with an object/instance&lt;br /&gt;
of a different type. This is a feature of all dynamically typed languages(like Ruby, Python, Clojure). However most of the statically typed languages such as Scala, Java, C/C++ do not allow you do so. In Scala,&lt;br /&gt;
&lt;br /&gt;
  scala&amp;gt; var str=&amp;quot;Hello World&amp;quot;&lt;br /&gt;
  str: java.lang.String = Hello World&lt;br /&gt;
  scala&amp;gt; str=123&lt;br /&gt;
  &amp;lt;console&amp;gt;:6: error: type mismatch;&lt;br /&gt;
   found   : Int(123)&lt;br /&gt;
   required: java.lang.String&lt;br /&gt;
         str=123&lt;br /&gt;
==== Mixed paradigm ====&lt;br /&gt;
Scala is a strange specimen. It is a complete blend of object oriented and functional programming. How can this happen? Functional world advocates immutability but object oriented world is all about state and how do you mutate it to fit your needs. Scala allows you create both mutable and immutable objects. Consider the following example,&lt;br /&gt;
 val str: String = &amp;quot;Hello World&amp;quot;&lt;br /&gt;
In the first line, I am creating a immutable variable(a misnomer indeed). Roughly translating the above line to Java is below:&lt;br /&gt;
&lt;br /&gt;
 final String str = &amp;quot;Hello World&amp;quot;; //note the extra semi-colon&lt;br /&gt;
Consider the next example below,&lt;br /&gt;
 val list: List[String] = List(&amp;quot;Scala&amp;quot;, &amp;quot;is&amp;quot;, &amp;quot;the coolest&amp;quot;, &amp;quot;language&amp;quot;, &amp;quot;in the world!&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The same code can be written in Java as below&lt;br /&gt;
&lt;br /&gt;
  List&amp;lt;String&amp;gt; mutableList = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
  list.add(&amp;quot;Scala&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;is&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;the coolest&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;language&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;in the world!&amp;quot;);&lt;br /&gt;
  List&amp;lt;String&amp;gt; immutableList = Collections.immutableList(mutableList);&lt;br /&gt;
&lt;br /&gt;
Compared to Java, Scala reduces the total number of lines you need to type and at the same time looks so much simpler and less verbose. Even though the above example shows immutable collection example, scala also provides mutable collection and object types under the package &amp;lt;code&amp;gt;scala.collection.mutable.*&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As it can be observed, even though scala advocates immutability, it does not restrict you from creating mutable objects. So its the best of both worlds. Another part of functional world is the concept of functions as ''''first class members'''' of the language. Some of the common languages like Ruby, Python and JavaScript all incorporate this feature. In the sense you can pass around functions to methods as a parameter. Enough talking;&lt;br /&gt;
&lt;br /&gt;
Let us have a class say &amp;lt;code&amp;gt;'Person'&amp;lt;/code&amp;gt; which has properties, &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; and another variable &amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt;.&lt;br /&gt;
In Java:&lt;br /&gt;
&lt;br /&gt;
 class Person{&lt;br /&gt;
    private String name;&lt;br /&gt;
    private int age;&lt;br /&gt;
    public String getName(){&lt;br /&gt;
        return name;&lt;br /&gt;
    }&lt;br /&gt;
    public void setName(String s){&lt;br /&gt;
        this.name = s;&lt;br /&gt;
    }&lt;br /&gt;
    public int getAge(){&lt;br /&gt;
        return age;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAge(int age){&lt;br /&gt;
        this.age = age;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
 case class Person(name: String, age: Int)&lt;br /&gt;
&lt;br /&gt;
This single line is equivalent to the above java code! So much cooler.. I would not be doing justice if I don't mention the fact that getters for &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt; are generated. Also equals() and hashCode() method are automatically generated for you by the compiler. Isn't this an awesome feature? No wonder Scala is considered a beautiful and high level language. Now assume we have a list of persons and we want to filter out all the persons who are of age 23. How would you do that in Java?&lt;br /&gt;
&lt;br /&gt;
  List&amp;lt;Person&amp;gt; persons; //has a bunch of person objects in it.&lt;br /&gt;
  List&amp;lt;Person&amp;gt; twentyThree = new ArrayList&amp;lt;Person&amp;gt;();&lt;br /&gt;
  for(Person p: persons){&lt;br /&gt;
      if(p.getAge() == 23){&lt;br /&gt;
          twentyThree.add(p);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
&lt;br /&gt;
 val twentyThree: List[Person] = persons.filter( _.age == 23)&lt;br /&gt;
&lt;br /&gt;
That's it! In case you are wondering whats going on, filter is a method present on scala.List class and the code block  _.age == 23 is a function created on the fly and passed onto the filter method. More precisely its called a closure - a functional concept which can be emulated in java only using anonymous inner classes. In case you are wondering how scala does this, scala's compiler transforms our one line function into some anonymous inner classes when generating java byte code but allows developer to define functions in a simple manner.&lt;br /&gt;
&lt;br /&gt;
Infact every function is an object in scala. Scala's standard library has a number of functions which are object themselves.&lt;br /&gt;
&lt;br /&gt;
==== Sophisticated Syntax/Features ====&lt;br /&gt;
When we say sophisticated, its really an awesome feature which is present in other functional languages like Haskell, (O)Caml and Erlang. Consider the following example,&lt;br /&gt;
&lt;br /&gt;
   val name = &amp;quot;Jackie Chan&amp;quot;&lt;br /&gt;
   println(name.getClass) //prints out String.&lt;br /&gt;
&lt;br /&gt;
Previously we saw that scala is a statically typed language and yet in the above example we don't specify what type is the variable name. In Java, you would type in the following:&lt;br /&gt;
&lt;br /&gt;
 String name = &amp;quot;Jackie Chan&amp;quot;&lt;br /&gt;
&lt;br /&gt;
So basically in Java you specify what type the name variable is. Scala compiler is very smart in the manner that it can automatically detect the type of the variable even if you don't specify it. Consider the following example,&lt;br /&gt;
&lt;br /&gt;
 case class Person(name: String, age: Int)&lt;br /&gt;
 val persons = List(&lt;br /&gt;
                        Person(&amp;quot;Jackie Chan&amp;quot;, 51), Person(&amp;quot;Jet Li&amp;quot;, 41),&lt;br /&gt;
                        Person(&amp;quot;RajniKanth&amp;quot;, 51), Person(&amp;quot;Kamal Hasan&amp;quot;, 44),&lt;br /&gt;
                        Person(&amp;quot;Martin Odersky&amp;quot;, 18), Person(&amp;quot;Steve Yegge&amp;quot;, 23))&lt;br /&gt;
 //notice that you don't have to use 'new' keyword to create new instances. This is taken care of by the companion objects.&lt;br /&gt;
&lt;br /&gt;
The above lines of code create a list with 6 different persons(An immutable list)&lt;br /&gt;
&lt;br /&gt;
Now, as in ruby scala supports the concepts of closure. Let's see what it is.&lt;br /&gt;
&lt;br /&gt;
 val twentyThree: List[Person] = persons.filter( { p: Person =&amp;gt; p.age == 3})&lt;br /&gt;
&lt;br /&gt;
In the above set of lines, the code within the '{ }' brackets is a function's body. Basically what the compiler did was to create a function which takes a single argument and returns either true or false. Now that's called a high level language. Ready for more?&lt;br /&gt;
&lt;br /&gt;
==== A Scalable Language ====&lt;br /&gt;
In almost all programming languages, there is a necessity to profile the running time of the procedures and tune its performance. The old way of doing things in Java would be:&lt;br /&gt;
&lt;br /&gt;
 public class Main{&lt;br /&gt;
 public static int factorial(int x){&lt;br /&gt;
   if(x == 0 || x == 1) return 1&lt;br /&gt;
   else return x * factorial(x-1)&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String [] args){&lt;br /&gt;
    long beforeTime = System.currentTimeMillis();&lt;br /&gt;
    int result = factorial(5);&lt;br /&gt;
    System.out.println(&amp;quot;Total time taken to execute factorial(5) is &amp;quot; + (System.currentTimeMillis() - result)+&amp;quot;ms&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;factorial(5) = &amp;quot; + result);&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
However, if you want to find out the execution time of some other method, you would have to type in all the above lines again. So much for code re-use! Lets see how its done in Scala. Hold on to your seats...&lt;br /&gt;
&lt;br /&gt;
 object Main{&lt;br /&gt;
  def main(args: Array[String]){&lt;br /&gt;
    def factorial(x: Int):Int = x match{&lt;br /&gt;
      case 0 | 1 =&amp;gt; 1&lt;br /&gt;
      case _ =&amp;gt; x * factorial(x-1)&lt;br /&gt;
  }&lt;br /&gt;
  import System.currentTimeMillis&lt;br /&gt;
  def timeIt(msg: String = &amp;quot;Time to execute is: &amp;quot;)(func: =&amp;gt; Unit){&lt;br /&gt;
    val before = currentTimeMillis&lt;br /&gt;
    func&lt;br /&gt;
    println(msg + (currentTimeMillis - before) + &amp;quot;ms&amp;quot;)&lt;br /&gt;
  }&lt;br /&gt;
  timeIt(&amp;quot;Total time taken to execute factorial(5) is &amp;quot;){&lt;br /&gt;
      val result = factorial(5)&lt;br /&gt;
      println(&amp;quot;factorial(5) = &amp;quot; + result)&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 //would print: factorial(5) = 120&lt;br /&gt;
 // Total time taken to execute factorial(5) is 1ms&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That's not right! The function &amp;lt;code&amp;gt;timeIt&amp;lt;/code&amp;gt; looks like a feature inbuilt into the language. Turn your disbelief into amazement;save the above code into a separate file and run it. Infact using this feature, it is possible to build powerful DSL languages which can run at full speed on JVM.&lt;br /&gt;
&lt;br /&gt;
Ruby allows you to add new methods to objects. This feature is often called as '''Monkey Patching''' because you are splitting open the class and adding/overriding methods (in)|(to) the class. Often this leads to unexpected behavior at runtime because, other libraries including into build path may depend on some methods which you changed. Scala provides yet another feature called implicits.&lt;br /&gt;
&lt;br /&gt;
In Java, String class is final so there is no way you can add new methods to it. But with implicits you can do more! Suppose you have some text in memory and you know it represents a number. But in Java the usual way to convert string to int is to do a Integer.parseInt(string) which seems so unnecessary. Let us make our lives easier...&lt;br /&gt;
&lt;br /&gt;
  implicit def strToInt(str: String) = new {&lt;br /&gt;
    def asInt = Integer.parseInt(str)&lt;br /&gt;
  }&lt;br /&gt;
  val str = &amp;quot;1234&amp;quot;&lt;br /&gt;
  println(str.asInt * 3) //prints out 3702&lt;br /&gt;
&lt;br /&gt;
The above code gives us an impression as if String class has gained new 'asInt' method. Can static object oriented languages do this? No! Infact scala cheats by calling our implicits to convert it into a different object as and when needed.&lt;br /&gt;
&lt;br /&gt;
The above functionalities are purely object oriented, yet they are safe. They don't pollute the global namespace unlike Ruby or Python&lt;br /&gt;
&lt;br /&gt;
==== Broad Classification of Functions ====&lt;br /&gt;
===== Predicate =====&lt;br /&gt;
It is a function which receives an object as argument and either returns true or false. They are useful in a number of situations. Suppose you had a list of integers and you wanted to filter out all odd numbers you could apply the predicate on every element in the list and&lt;br /&gt;
* If the predicate returned true, add it to the new list&lt;br /&gt;
* else ignore the current entry and try next.&lt;br /&gt;
&lt;br /&gt;
 scala&amp;gt; val numbers = 0 to 20&lt;br /&gt;
 numbers: scala.collection.immutable.Range.Inclusive with  scala.collection.immutable.Range.ByOne = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)&lt;br /&gt;
 scala&amp;gt; val evenNumbers = numbers.filter( _ % 2 == 0)&lt;br /&gt;
 evenNumbers: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20)&lt;br /&gt;
&lt;br /&gt;
===== Closure =====&lt;br /&gt;
A function which does some operation on an object but does not return any result. In otherwords, the return type of closure is void. Say we have a list of strings and we need to print them all to the console; we can do it the following way:&lt;br /&gt;
&lt;br /&gt;
 scala&amp;gt; val strs = List(&amp;quot;print&amp;quot;, &amp;quot;all&amp;quot;, &amp;quot;to&amp;quot;, &amp;quot;console&amp;quot;)&lt;br /&gt;
 strs: List[java.lang.String] = List(print, all, to, console)&lt;br /&gt;
 scala&amp;gt; strs.foreach(println) //println == System.out.println&lt;br /&gt;
 print&lt;br /&gt;
 all&lt;br /&gt;
 to&lt;br /&gt;
 console&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; is a method which is executed for every element present in the list.&lt;br /&gt;
* &amp;lt;code&amp;gt; str.foreach(println)&amp;lt;/code&amp;gt; - Now, this is getting little confusing. Whats &amp;lt;code&amp;gt;println&amp;lt;/code&amp;gt; doing without any arguments? Scala's compiler is intelligent enough to determine that println takes in a single argument and so allows us to skip specifying it.&lt;br /&gt;
===== Transformer =====&lt;br /&gt;
A function which recieves an entity/object and returns an another object(possibly of different type). Infact both predicate and a closure are transformers. To understand more what a transformer is, let us change the case of all the strings in a list.&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
   val list = List(&amp;quot;Scala&amp;quot;, &amp;quot;is&amp;quot;, &amp;quot;the coolest&amp;quot;, &amp;quot;language&amp;quot;, &amp;quot;in the world!&amp;quot;)&lt;br /&gt;
 scala&amp;gt; list.map(_.toUpperCase)&lt;br /&gt;
 res5: List[java.lang.String] = List(SCALA, IS, THE COOLEST, LANGUAGE, IN THE WORLD!)&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;scala.List's&amp;lt;/code&amp;gt; method which takes in a function as a parameter and for every element in the list invokes the function. The result returned by the function is added to a new list.&lt;br /&gt;
* &amp;lt;code&amp;gt;_.toUpperCase&amp;lt;/code&amp;gt; is the transformer function upcasing all the strings.&lt;br /&gt;
&lt;br /&gt;
==== Currying ====&lt;br /&gt;
Excellent tutorial here: [http://bit.ly/16Qs6l]&lt;br /&gt;
Named after its inventor 'Haskell Curry', it is a way of converting a function which takes multiple arguments, into a function with lesser number of arguments. Theory is always boring or we feel so. Let's jump right in and work out an example. Consider a simple function which adds two numbers.&lt;br /&gt;
&lt;br /&gt;
 def add(x:Int)(y:Int) = x + y&lt;br /&gt;
&lt;br /&gt;
Now assume you want to create a function which will add '5' to all its input and return the sum. Can you re-use the code above? Currying works great in these scenarios..&lt;br /&gt;
 val add5ToInput = add(5) _  // the '_' is to tell the compiler to create a curried version of the above function.&lt;br /&gt;
 println(add5ToInput(10))  //surprise surprise - prints 15!&lt;br /&gt;
&lt;br /&gt;
So basically what happens is that compiler creates an intermediate function which always has &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; as the first parameter. So when we say &amp;lt;code&amp;gt;add5ToInput(10)&amp;lt;/code&amp;gt; in reality, it inturn calls &amp;lt;code&amp;gt;add(5)(10)&amp;lt;/code&amp;gt;. So currying basically helps us reduce the total number of parameters which needs to passed to functions and in turn helps re-use code in a functional way!&lt;br /&gt;
&lt;br /&gt;
==== Pattern Matching ====&lt;br /&gt;
also see [http://bit.ly/3o5JA1]&lt;br /&gt;
Yet another unique feature of functional programming languages is the concept of pattern matching. Let us see what pattern matching is all about. Continuing our previous example of Person objects, let us write some code to guess the profession of a person based on his name.(I know it sounds lame..)&lt;br /&gt;
&lt;br /&gt;
 def guessProfession(p: Person) = p.name match {&lt;br /&gt;
        case &amp;quot;Jackie Chan&amp;quot; | &amp;quot;Jet Li&amp;quot; =&amp;gt; &amp;quot;Martial Artist and Actor&amp;quot;&lt;br /&gt;
        case &amp;quot;RajniKanth&amp;quot; | &amp;quot;Kamal Hasan&amp;quot; =&amp;gt; &amp;quot;South Indian Actor&amp;quot;&lt;br /&gt;
        case &amp;quot;Martin Odersky&amp;quot; | &amp;quot;Steve Yegge&amp;quot; =&amp;gt; &amp;quot;Famous Programmer&amp;quot;&lt;br /&gt;
        case _ =&amp;gt; &amp;quot;I don't know %s&amp;quot;.format(p.name)&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 val p = Person(&amp;quot;Kovalan Venkatesan&amp;quot;, 24)&lt;br /&gt;
 val p1 = Person(&amp;quot;Martin Odersky&amp;quot;, 31)&lt;br /&gt;
 println(guessProfession(p)) //prints I don't know Kovalan Venkatesan.&lt;br /&gt;
 println(guessProfession(p1)) //prints &amp;quot;Famous Programmer&amp;quot;&lt;br /&gt;
&lt;br /&gt;
As you may have noticed, the above example is very similar to switch statements in imperative languages such as C, Java. However, it is more powerful and extensible. Why?&lt;br /&gt;
&lt;br /&gt;
# In both C and Java only numeric types can be used in switch statements.(including enums which are simply names mapped onto integers). In Scala you can use almost use anything you want. Infact scala supports custom extractor patterns, whose scope is beyond this text. Please consider visiting scala resources to learn more.&lt;br /&gt;
# The cases do not allow fall through. In Java and C, you have to manually type &amp;lt;code&amp;gt;break&amp;lt;/code&amp;gt; after the end of every case. Scala does not have any &amp;lt;code&amp;gt;break&amp;lt;/code&amp;gt; nor &amp;lt;code&amp;gt;continue&amp;lt;/code&amp;gt; keywords(shocking?)&lt;br /&gt;
&lt;br /&gt;
A purely object oriented person may argue that we could use Visitor Pattern to do the same. That is true, but how many lines of code would you have to write to accomplish the same functionality? Wanna guess?[http://www.artima.com/weblogs/viewpost.jsp?thread=166742]&lt;br /&gt;
&lt;br /&gt;
More explanation of case statements:&lt;br /&gt;
* &amp;lt;code&amp;gt;match&amp;lt;/code&amp;gt; is equivalent to switch keyword in C, C++ and Java.&lt;br /&gt;
* &amp;lt;code&amp;gt;case&amp;lt;/code&amp;gt; keyword signifies the beginning of the particular case.&lt;br /&gt;
* The &amp;lt;code&amp;gt;_&amp;lt;/code&amp;gt;(underscore) signifies many things in scala. When we say &amp;lt;code&amp;gt;case _ =&amp;gt;&amp;lt;/code&amp;gt;, it is equivalent to the &amp;lt;code&amp;gt;default&amp;lt;/code&amp;gt; keyword in C/Java.&lt;br /&gt;
&lt;br /&gt;
===== Handling exceptions - Scala way! =====&lt;br /&gt;
In Java, exception matching is considered verbose and ugly. To open and write a file,&lt;br /&gt;
 try {&lt;br /&gt;
    File file = new File(&amp;quot;/tmp/sample.txt&amp;quot;);&lt;br /&gt;
    PrintStream printStream = new PrintStream(file);&lt;br /&gt;
    printStream.write(&amp;quot;something&amp;quot;.getBytes());&lt;br /&gt;
 } catch (FileNotFoundException e) {&lt;br /&gt;
    System.out.println(&amp;quot;Unable to find the file /tmp/sample.txt&amp;quot;);&lt;br /&gt;
    e.printStackTrace();&lt;br /&gt;
 } catch (IOException e) {&lt;br /&gt;
   System.out.println(&amp;quot;Unknown IO error&amp;quot;);&lt;br /&gt;
    e.printStackTrace();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
  try{&lt;br /&gt;
    val file = new File(&amp;quot;/tmp/sample.txt&amp;quot;)&lt;br /&gt;
    val ps = new PrintStream(file)&lt;br /&gt;
    ps.write(&amp;quot;something&amp;quot;.getBytes())&lt;br /&gt;
  }catch{&lt;br /&gt;
    case e: FileNotFoundException =&amp;gt; println(&amp;quot;Unable to find file /tmp/sample.txt&amp;quot;)&lt;br /&gt;
    case IOException =&amp;gt; println(&amp;quot;Unknown IO error&amp;quot;)&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
We have just scratched the surface of Scala, but it has innumerable features which show a beautiful blend of functional and object oriented features. Scala proves we can use them both together.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Functional programming integrated with object oriented style leads to:&lt;br /&gt;
&lt;br /&gt;
#better understanding of program behavior as a result of immutability features&lt;br /&gt;
#use of efficient recursive techniques such as tail call optimization&lt;br /&gt;
#Reduction in number of lines of code&lt;br /&gt;
&lt;br /&gt;
By using these techniques we utilize the best of both functional and object oriented techniques.&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
*[1]: http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)&lt;br /&gt;
*[2]: http://www.haskell.org&lt;br /&gt;
*[3]: http://www.scala-lang.org&lt;br /&gt;
*[4]: http://bit.ly/16Qs6l&lt;br /&gt;
*[5]: http://bit.ly/3o5JA1&lt;br /&gt;
*[6]: http://www.artima.com/weblogs/viewpost.jsp?thread=166742&lt;br /&gt;
*[7]: http://lamp.epfl.ch/~odersky/&lt;br /&gt;
*[8]: http://www.scala-lang.org/docu/files/collections-api/collections.html#&lt;br /&gt;
*[9]: http://www.codecommit.com/blog/java/interop-between-java-and-scala&lt;br /&gt;
*[10]: http://www.codecommit.com/blog/scala/what-is-hindley-milner-and-why-is-it-cool&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_az&amp;diff=34544</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1e az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_az&amp;diff=34544"/>
		<updated>2010-09-09T02:39:54Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Programming Paradigms */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Programming Paradigms==&lt;br /&gt;
 &lt;br /&gt;
Every computer program needs a style of writing which specifies how to solve a software engineering problem. This style is represented by the paradigm. Each computer program follows one or more paradigm which differs in representing the elements of a program(such as variables and objects) and the steps needed to compute a task.&lt;br /&gt;
&lt;br /&gt;
Diferent paradigms are:&lt;br /&gt;
&lt;br /&gt;
# Procedural/imperative paradigms: Assembly, C, C++, Java, C#&lt;br /&gt;
# Object Oriented paradigm : C++, Java, Python, Ruby, Scala, C#&lt;br /&gt;
# Functional Paradigm : Lisp, Haskell, Clojure, Scala, OCaml, Ruby&lt;br /&gt;
# Logic Paradigm: Prolog&lt;br /&gt;
&lt;br /&gt;
==Multi-Paradigm Programming==&lt;br /&gt;
&lt;br /&gt;
Multiparadigm refers to use of a combination of programming paradigms for solving a computer problem. Some languages subscribe strictly to a single paradigm like Assembly and C. Others like Java, C++, Scala and C# employ more than one paradigm. Every paradigm comes with its own strength and weakness and this quite motivates us to take advantage of each paradigm and use it in a manner that best fits the problem at hand.&lt;br /&gt;
&lt;br /&gt;
==Overview of Functional Programming==&lt;br /&gt;
&lt;br /&gt;
Functional programming is derived from lambda calculus which models computations as the evaluation of functions and recursions. More emphasis is laid on application of functions rather than changing the state of variables in a program. Functional programming relies heavily on recursions and it is the only way to iterate instead of loops.&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;How it differs from imperative programming&amp;lt;/b&amp;gt;===&lt;br /&gt;
    &lt;br /&gt;
Imperative programming follows the Von-Neumann architecture and mainly consists of loops and usage of globa states to perform a computation. For example consider the task of computing the sum of numbers from 1 to n. &lt;br /&gt;
&lt;br /&gt;
In imperative style,&lt;br /&gt;
        &lt;br /&gt;
        sum := 0         // global state&lt;br /&gt;
        for i &amp;lt;- 1 to n do&lt;br /&gt;
            sum := sum + i&lt;br /&gt;
            &lt;br /&gt;
In Functional style,&lt;br /&gt;
    &lt;br /&gt;
         func sum(n)        // note that sum is a function and is recursive&lt;br /&gt;
           if  n = 1 then&lt;br /&gt;
               return 1&lt;br /&gt;
           else&lt;br /&gt;
               return n + sum(n-1)&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Pure and impure functional programming&amp;lt;/b&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
Purely functional programming exhibit referential transparency [http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)] which does not involve any global state or I/O changes. If the same functional expression results in the same output value for the same argument x at different stages of execution , then the function is said to be pure which is devoid of any global state change. For example Haskell [http://www.haskell.org] is purely functional.&lt;br /&gt;
&lt;br /&gt;
==Overview of object oriented programming==&lt;br /&gt;
&lt;br /&gt;
Typially programs were written with a procedural view where the program's logic was given utmost importance and it follows a sequential structure. But object oriented techniques really cares about the data or objects we want to mainpulate with rather than the logic of  a program. Every object contains its own state(in the form of variables) and and a number of methods to manipulate the variables.For example consider the class Add:&lt;br /&gt;
           &lt;br /&gt;
           class Add{&lt;br /&gt;
           /* Object variables */&lt;br /&gt;
                  private int sum = 0;&lt;br /&gt;
          /* Object Methods */&lt;br /&gt;
                  public void calculate_sum(int n){&lt;br /&gt;
                      int i=0;&lt;br /&gt;
                      while(i &amp;lt;= n){&lt;br /&gt;
                          sum = sum + i;&lt;br /&gt;
                          i++;&lt;br /&gt;
                      }&lt;br /&gt;
                  }&lt;br /&gt;
            }&lt;br /&gt;
       &lt;br /&gt;
            Add instance1 = new Add();     // instance1 is an object of type Add&lt;br /&gt;
            instance1.calculate_sum(100);&lt;br /&gt;
           &lt;br /&gt;
===&amp;lt;b&amp;gt;Principles of object oriented design&amp;lt;/b&amp;gt;===&lt;br /&gt;
           &lt;br /&gt;
1. &amp;lt;b&amp;gt;Encapsulation:&amp;lt;/b&amp;gt; Encapsulation refers to hiding the internal representation of the object from the outside view. For example algorithm to compute the calculate_sum() method may be hidden, but it will present the user with the expected results.&lt;br /&gt;
&lt;br /&gt;
2. &amp;lt;b&amp;gt;Polymorphism:&amp;lt;/b&amp;gt; Polymorphism means ability to take multiple forms. For example an operation may exhibit different behaviour at different instances. Consider the operator '+', for numbers it will generate the sum, but for strings it will produce a third string which concatenates the input strings.&lt;br /&gt;
&lt;br /&gt;
3. &amp;lt;b&amp;gt;Inheritance:&amp;lt;/b&amp;gt; Inheritance involves base and derived classes with derived class inheriting all the methods and states frm the base class. Inheritance basically forms a hirerachy. For example if shape is an object, then objects square, rectangle, circle all derive the same characteristic as shape does.&lt;br /&gt;
&lt;br /&gt;
==Functional + OOP code==&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Functions as Objects&amp;lt;/b&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
One of the cornerstone of functional and object oriented code is treating functions as objects.It means that we can pass functions as arguments, store it and return them from other functions. This is highly useful in an user interface code where it can be used as call back functions which get called when an event occurs( in event driven programming)&lt;br /&gt;
&lt;br /&gt;
An example in Scala [http://www.scala-lang.org]:&lt;br /&gt;
&lt;br /&gt;
  Object Timer{&lt;br /&gt;
    def  action(callback() : () =&amp;gt; unit){              // callback() is the function passed as an argument&lt;br /&gt;
         while( some condition)                                                            &lt;br /&gt;
         { &lt;br /&gt;
            callback();&lt;br /&gt;
            Thread.sleep(3000)&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
     def event(){                                      //  event is the method which tells what to do &lt;br /&gt;
         /* process the event here&lt;br /&gt;
         &lt;br /&gt;
         */      &lt;br /&gt;
     }&lt;br /&gt;
        &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Another example of functional concept in object oriented design is the concept of blocks especially in Ruby. Blocks are basically nameless functions which can be passed to a function and then that function can invoke the passed in nameless function. This is a common style called higher order function style among languages that handle functions as first class objects. Basically blocks can be designed for loop abstraction and lets the user to design their own way of iterating the values, thereby hiding the implementation details. &lt;br /&gt;
For example if we want to iterate backwards from the end to the beginning without revealing its internal implementations, we could implement the loop logic inside the block and pass it to a method or function.&lt;br /&gt;
&lt;br /&gt;
Here is a Ruby implementation of block&lt;br /&gt;
 &lt;br /&gt;
 def printlist(array,&amp;amp;reverse)&lt;br /&gt;
    array.each do |element|&lt;br /&gt;
       puts &amp;quot;array element: #{element}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    reverse.call(array)   /* Here it prints the reverse implementation of list*/&lt;br /&gt;
    end&lt;br /&gt;
  &lt;br /&gt;
 printlist([1,2,3,4,5,6]) { |array| &lt;br /&gt;
   array.reverse.each do |element| &lt;br /&gt;
     puts &amp;quot;array element: #{element}&amp;quot; &lt;br /&gt;
   end &lt;br /&gt;
 }&lt;br /&gt;
Next we will take a look at some of the features of Scala which supports a blend of functional and object oriented concepts in a concise manner.&lt;br /&gt;
&lt;br /&gt;
= Scala =&lt;br /&gt;
Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages&lt;br /&gt;
&lt;br /&gt;
Scala can be considered a very good blend of the best of functional and object oriented world. It was conceived(in 2001) and implemented by Martin Odersky who was also the author of Sun javac compiler and played a major role in the design and retrofitting of generics into Java. The latest version of Scala is 2.8 and includes a number of performance improvements over previous versions. We shall see more about the features of Scala in the following pages, but for fun, some facts!&lt;br /&gt;
&lt;br /&gt;
=== Features ===&lt;br /&gt;
==== Statically Typed ====&lt;br /&gt;
Scala belongs to the family of languages which do not allow the type of variables once they have been defined. In other words, the type information of each and every variable is encoded in the generated code and the compiler makes sure, you are not doing something funny. For example, lets consider the following example,&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
   irb(main):001:0&amp;gt; str=&amp;quot;Hello World&amp;quot;&lt;br /&gt;
    =&amp;gt; &amp;quot;Hello World&amp;quot;&lt;br /&gt;
    irb(main):002:0&amp;gt; puts str.class&lt;br /&gt;
    String&lt;br /&gt;
    irb(main):003:0&amp;gt; str=123&lt;br /&gt;
    =&amp;gt; 123&lt;br /&gt;
    irb(main):004:0&amp;gt; puts str.class&lt;br /&gt;
    Fixnum&lt;br /&gt;
&lt;br /&gt;
In the above example, first we define 'str' variable to be of type String and then as a number. So basically, no type information is associated with the variable at any point in the program. Hence we can easily reassociate that variable with an object/instance&lt;br /&gt;
of a different type. This is a feature of all dynamically typed languages(like Ruby, Python, Clojure). However most of the statically typed languages such as Scala, Java, C/C++ do not allow you do so. In Scala,&lt;br /&gt;
&lt;br /&gt;
  scala&amp;gt; var str=&amp;quot;Hello World&amp;quot;&lt;br /&gt;
  str: java.lang.String = Hello World&lt;br /&gt;
  scala&amp;gt; str=123&lt;br /&gt;
  &amp;lt;console&amp;gt;:6: error: type mismatch;&lt;br /&gt;
   found   : Int(123)&lt;br /&gt;
   required: java.lang.String&lt;br /&gt;
         str=123&lt;br /&gt;
==== Mixed paradigm ====&lt;br /&gt;
Scala is a strange specimen. It is a complete blend of object oriented and functional programming. How can this happen? Functional world advocates immutability but object oriented world is all about state and how do you mutate it to fit your needs. Scala allows you create both mutable and immutable objects. Consider the following example,&lt;br /&gt;
 val str: String = &amp;quot;Hello World&amp;quot;&lt;br /&gt;
In the first line, I am creating a immutable variable(a misnomer indeed). Roughly translating the above line to Java is below:&lt;br /&gt;
&lt;br /&gt;
 final String str = &amp;quot;Hello World&amp;quot;; //note the extra semi-colon&lt;br /&gt;
Consider the next example below,&lt;br /&gt;
 val list: List[String] = List(&amp;quot;Scala&amp;quot;, &amp;quot;is&amp;quot;, &amp;quot;the coolest&amp;quot;, &amp;quot;language&amp;quot;, &amp;quot;in the world!&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The same code can be written in Java as below&lt;br /&gt;
&lt;br /&gt;
  List&amp;lt;String&amp;gt; mutableList = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
  list.add(&amp;quot;Scala&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;is&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;the coolest&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;language&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;in the world!&amp;quot;);&lt;br /&gt;
  List&amp;lt;String&amp;gt; immutableList = Collections.immutableList(mutableList);&lt;br /&gt;
&lt;br /&gt;
Compared to Java, Scala reduces the total number of lines you need to type and at the same time looks so much simpler and less verbose. Even though the above example shows immutable collection example, scala also provides mutable collection and object types under the package &amp;lt;code&amp;gt;scala.collection.mutable.*&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As it can be observed, even though scala advocates immutability, it does not restrict you from creating mutable objects. So its the best of both worlds. Another part of functional world is the concept of functions as ''''first class members'''' of the language. Some of the common languages like Ruby, Python and JavaScript all incorporate this feature. In the sense you can pass around functions to methods as a parameter. Enough talking;&lt;br /&gt;
&lt;br /&gt;
Let us have a class say &amp;lt;code&amp;gt;'Person'&amp;lt;/code&amp;gt; which has properties, &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; and another variable &amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt;.&lt;br /&gt;
In Java:&lt;br /&gt;
&lt;br /&gt;
 class Person{&lt;br /&gt;
    private String name;&lt;br /&gt;
    private int age;&lt;br /&gt;
    public String getName(){&lt;br /&gt;
        return name;&lt;br /&gt;
    }&lt;br /&gt;
    public void setName(String s){&lt;br /&gt;
        this.name = s;&lt;br /&gt;
    }&lt;br /&gt;
    public int getAge(){&lt;br /&gt;
        return age;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAge(int age){&lt;br /&gt;
        this.age = age;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
 case class Person(name: String, age: Int)&lt;br /&gt;
&lt;br /&gt;
This single line is equivalent to the above java code! So much cooler.. I would not be doing justice if I don't mention the fact that getters for &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt; are generated. Also equals() and hashCode() method are automatically generated for you by the compiler. Isn't this an awesome feature? No wonder Scala is considered a beautiful and high level language. Now assume we have a list of persons and we want to filter out all the persons who are of age 23. How would you do that in Java?&lt;br /&gt;
&lt;br /&gt;
  List&amp;lt;Person&amp;gt; persons; //has a bunch of person objects in it.&lt;br /&gt;
  List&amp;lt;Person&amp;gt; twentyThree = new ArrayList&amp;lt;Person&amp;gt;();&lt;br /&gt;
  for(Person p: persons){&lt;br /&gt;
      if(p.getAge() == 23){&lt;br /&gt;
          twentyThree.add(p);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
&lt;br /&gt;
 val twentyThree: List[Person] = persons.filter( _.age == 23)&lt;br /&gt;
&lt;br /&gt;
That's it! In case you are wondering whats going on, filter is a method present on scala.List class and the code block  _.age == 23 is a function created on the fly and passed onto the filter method. More precisely its called a closure - a functional concept which can be emulated in java only using anonymous inner classes. In case you are wondering how scala does this, scala's compiler transforms our one line function into some anonymous inner classes when generating java byte code but allows developer to define functions in a simple manner.&lt;br /&gt;
&lt;br /&gt;
Infact every function is an object in scala. Scala's standard library has a number of functions which are object themselves.&lt;br /&gt;
&lt;br /&gt;
==== Sophisticated Syntax/Features ====&lt;br /&gt;
When we say sophisticated, its really an awesome feature which is present in other functional languages like Haskell, (O)Caml and Erlang. Consider the following example,&lt;br /&gt;
&lt;br /&gt;
   val name = &amp;quot;Jackie Chan&amp;quot;&lt;br /&gt;
   println(name.getClass) //prints out String.&lt;br /&gt;
&lt;br /&gt;
Previously we saw that scala is a statically typed language and yet in the above example we don't specify what type is the variable name. In Java, you would type in the following:&lt;br /&gt;
&lt;br /&gt;
 String name = &amp;quot;Jackie Chan&amp;quot;&lt;br /&gt;
&lt;br /&gt;
So basically in Java you specify what type the name variable is. Scala compiler is very smart in the manner that it can automatically detect the type of the variable even if you don't specify it. Consider the following example,&lt;br /&gt;
&lt;br /&gt;
 case class Person(name: String, age: Int)&lt;br /&gt;
 val persons = List(&lt;br /&gt;
                        Person(&amp;quot;Jackie Chan&amp;quot;, 51), Person(&amp;quot;Jet Li&amp;quot;, 41),&lt;br /&gt;
                        Person(&amp;quot;RajniKanth&amp;quot;, 51), Person(&amp;quot;Kamal Hasan&amp;quot;, 44),&lt;br /&gt;
                        Person(&amp;quot;Martin Odersky&amp;quot;, 18), Person(&amp;quot;Steve Yegge&amp;quot;, 23))&lt;br /&gt;
 //notice that you don't have to use 'new' keyword to create new instances. This is taken care of by the companion objects.&lt;br /&gt;
&lt;br /&gt;
The above lines of code create a list with 6 different persons(An immutable list)&lt;br /&gt;
&lt;br /&gt;
Now, as in ruby scala supports the concepts of closure. Let's see what it is.&lt;br /&gt;
&lt;br /&gt;
 val twentyThree: List[Person] = persons.filter( { p: Person =&amp;gt; p.age == 3})&lt;br /&gt;
&lt;br /&gt;
In the above set of lines, the code within the '{ }' brackets is a function's body. Basically what the compiler did was to create a function which takes a single argument and returns either true or false. Now that's called a high level language. Ready for more?&lt;br /&gt;
&lt;br /&gt;
==== A Scalable Language ====&lt;br /&gt;
In almost all programming languages, there is a necessity to profile the running time of the procedures and tune its performance. The old way of doing things in Java would be:&lt;br /&gt;
&lt;br /&gt;
 public class Main{&lt;br /&gt;
 public static int factorial(int x){&lt;br /&gt;
   if(x == 0 || x == 1) return 1&lt;br /&gt;
   else return x * factorial(x-1)&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String [] args){&lt;br /&gt;
    long beforeTime = System.currentTimeMillis();&lt;br /&gt;
    int result = factorial(5);&lt;br /&gt;
    System.out.println(&amp;quot;Total time taken to execute factorial(5) is &amp;quot; + (System.currentTimeMillis() - result)+&amp;quot;ms&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;factorial(5) = &amp;quot; + result);&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
However, if you want to find out the execution time of some other method, you would have to type in all the above lines again. So much for code re-use! Lets see how its done in Scala. Hold on to your seats...&lt;br /&gt;
&lt;br /&gt;
 object Main{&lt;br /&gt;
  def main(args: Array[String]){&lt;br /&gt;
    def factorial(x: Int):Int = x match{&lt;br /&gt;
      case 0 | 1 =&amp;gt; 1&lt;br /&gt;
      case _ =&amp;gt; x * factorial(x-1)&lt;br /&gt;
  }&lt;br /&gt;
  import System.currentTimeMillis&lt;br /&gt;
  def timeIt(msg: String = &amp;quot;Time to execute is: &amp;quot;)(func: =&amp;gt; Unit){&lt;br /&gt;
    val before = currentTimeMillis&lt;br /&gt;
    func&lt;br /&gt;
    println(msg + (currentTimeMillis - before) + &amp;quot;ms&amp;quot;)&lt;br /&gt;
  }&lt;br /&gt;
  timeIt(&amp;quot;Total time taken to execute factorial(5) is &amp;quot;){&lt;br /&gt;
      val result = factorial(5)&lt;br /&gt;
      println(&amp;quot;factorial(5) = &amp;quot; + result)&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 //would print: factorial(5) = 120&lt;br /&gt;
 // Total time taken to execute factorial(5) is 1ms&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That's not right! The function &amp;lt;code&amp;gt;timeIt&amp;lt;/code&amp;gt; looks like a feature inbuilt into the language. Turn your disbelief into amazement;save the above code into a separate file and run it. Infact using this feature, it is possible to build powerful DSL languages which can run at full speed on JVM.&lt;br /&gt;
&lt;br /&gt;
Ruby allows you to add new methods to objects. This feature is often called as '''Monkey Patching''' because you are splitting open the class and adding/overriding methods (in)|(to) the class. Often this leads to unexpected behavior at runtime because, other libraries including into build path may depend on some methods which you changed. Scala provides yet another feature called implicits.&lt;br /&gt;
&lt;br /&gt;
In Java, String class is final so there is no way you can add new methods to it. But with implicits you can do more! Suppose you have some text in memory and you know it represents a number. But in Java the usual way to convert string to int is to do a Integer.parseInt(string) which seems so unnecessary. Let us make our lives easier...&lt;br /&gt;
&lt;br /&gt;
  implicit def strToInt(str: String) = new {&lt;br /&gt;
    def asInt = Integer.parseInt(str)&lt;br /&gt;
  }&lt;br /&gt;
  val str = &amp;quot;1234&amp;quot;&lt;br /&gt;
  println(str.asInt * 3) //prints out 3702&lt;br /&gt;
&lt;br /&gt;
The above code gives us an impression as if String class has gained new 'asInt' method. Can static object oriented languages do this? No! Infact scala cheats by calling our implicits to convert it into a different object as and when needed.&lt;br /&gt;
&lt;br /&gt;
The above functionalities are purely object oriented, yet they are safe. They don't pollute the global namespace unlike Ruby or Python&lt;br /&gt;
&lt;br /&gt;
==== Broad Classification of Functions ====&lt;br /&gt;
===== Predicate =====&lt;br /&gt;
It is a function which receives an object as argument and either returns true or false. They are useful in a number of situations. Suppose you had a list of integers and you wanted to filter out all odd numbers you could apply the predicate on every element in the list and&lt;br /&gt;
* If the predicate returned true, add it to the new list&lt;br /&gt;
* else ignore the current entry and try next.&lt;br /&gt;
&lt;br /&gt;
 scala&amp;gt; val numbers = 0 to 20&lt;br /&gt;
 numbers: scala.collection.immutable.Range.Inclusive with  scala.collection.immutable.Range.ByOne = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)&lt;br /&gt;
 scala&amp;gt; val evenNumbers = numbers.filter( _ % 2 == 0)&lt;br /&gt;
 evenNumbers: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20)&lt;br /&gt;
&lt;br /&gt;
===== Closure =====&lt;br /&gt;
A function which does some operation on an object but does not return any result. In otherwords, the return type of closure is void. Say we have a list of strings and we need to print them all to the console; we can do it the following way:&lt;br /&gt;
&lt;br /&gt;
 scala&amp;gt; val strs = List(&amp;quot;print&amp;quot;, &amp;quot;all&amp;quot;, &amp;quot;to&amp;quot;, &amp;quot;console&amp;quot;)&lt;br /&gt;
 strs: List[java.lang.String] = List(print, all, to, console)&lt;br /&gt;
 scala&amp;gt; strs.foreach(println) //println == System.out.println&lt;br /&gt;
 print&lt;br /&gt;
 all&lt;br /&gt;
 to&lt;br /&gt;
 console&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; is a method which is executed for every element present in the list.&lt;br /&gt;
* &amp;lt;code&amp;gt; str.foreach(println)&amp;lt;/code&amp;gt; - Now, this is getting little confusing. Whats &amp;lt;code&amp;gt;println&amp;lt;/code&amp;gt; doing without any arguments? Scala's compiler is intelligent enough to determine that println takes in a single argument and so allows us to skip specifying it.&lt;br /&gt;
===== Transformer =====&lt;br /&gt;
A function which recieves an entity/object and returns an another object(possibly of different type). Infact both predicate and a closure are transformers. To understand more what a transformer is, let us change the case of all the strings in a list.&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
   val list = List(&amp;quot;Scala&amp;quot;, &amp;quot;is&amp;quot;, &amp;quot;the coolest&amp;quot;, &amp;quot;language&amp;quot;, &amp;quot;in the world!&amp;quot;)&lt;br /&gt;
 scala&amp;gt; list.map(_.toUpperCase)&lt;br /&gt;
 res5: List[java.lang.String] = List(SCALA, IS, THE COOLEST, LANGUAGE, IN THE WORLD!)&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;scala.List's&amp;lt;/code&amp;gt; method which takes in a function as a parameter and for every element in the list invokes the function. The result returned by the function is added to a new list.&lt;br /&gt;
* &amp;lt;code&amp;gt;_.toUpperCase&amp;lt;/code&amp;gt; is the transformer function upcasing all the strings.&lt;br /&gt;
&lt;br /&gt;
==== Currying ====&lt;br /&gt;
Excellent tutorial here: [http://bit.ly/16Qs6l]&lt;br /&gt;
Named after its inventor 'Haskell Curry', it is a way of converting a function which takes multiple arguments, into a function with lesser number of arguments. Theory is always boring or we feel so. Let's jump right in and work out an example. Consider a simple function which adds two numbers.&lt;br /&gt;
&lt;br /&gt;
 def add(x:Int)(y:Int) = x + y&lt;br /&gt;
&lt;br /&gt;
Now assume you want to create a function which will add '5' to all its input and return the sum. Can you re-use the code above? Currying works great in these scenarios..&lt;br /&gt;
 val add5ToInput = add(5) _  // the '_' is to tell the compiler to create a curried version of the above function.&lt;br /&gt;
 println(add5ToInput(10))  //surprise surprise - prints 15!&lt;br /&gt;
&lt;br /&gt;
So basically what happens is that compiler creates an intermediate function which always has &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; as the first parameter. So when we say &amp;lt;code&amp;gt;add5ToInput(10)&amp;lt;/code&amp;gt; in reality, it inturn calls &amp;lt;code&amp;gt;add(5)(10)&amp;lt;/code&amp;gt;. So currying basically helps us reduce the total number of parameters which needs to passed to functions and in turn helps re-use code in a functional way!&lt;br /&gt;
&lt;br /&gt;
==== Pattern Matching ====&lt;br /&gt;
also see [http://bit.ly/3o5JA1]&lt;br /&gt;
Yet another unique feature of functional programming languages is the concept of pattern matching. Let us see what pattern matching is all about. Continuing our previous example of Person objects, let us write some code to guess the profession of a person based on his name.(I know it sounds lame..)&lt;br /&gt;
&lt;br /&gt;
 def guessProfession(p: Person) = p.name match {&lt;br /&gt;
        case &amp;quot;Jackie Chan&amp;quot; | &amp;quot;Jet Li&amp;quot; =&amp;gt; &amp;quot;Martial Artist and Actor&amp;quot;&lt;br /&gt;
        case &amp;quot;RajniKanth&amp;quot; | &amp;quot;Kamal Hasan&amp;quot; =&amp;gt; &amp;quot;South Indian Actor&amp;quot;&lt;br /&gt;
        case &amp;quot;Martin Odersky&amp;quot; | &amp;quot;Steve Yegge&amp;quot; =&amp;gt; &amp;quot;Famous Programmer&amp;quot;&lt;br /&gt;
        case _ =&amp;gt; &amp;quot;I don't know %s&amp;quot;.format(p.name)&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 val p = Person(&amp;quot;Kovalan Venkatesan&amp;quot;, 24)&lt;br /&gt;
 val p1 = Person(&amp;quot;Martin Odersky&amp;quot;, 31)&lt;br /&gt;
 println(guessProfession(p)) //prints I don't know Kovalan Venkatesan.&lt;br /&gt;
 println(guessProfession(p1)) //prints &amp;quot;Famous Programmer&amp;quot;&lt;br /&gt;
&lt;br /&gt;
As you may have noticed, the above example is very similar to switch statements in imperative languages such as C, Java. However, it is more powerful and extensible. Why?&lt;br /&gt;
&lt;br /&gt;
# In both C and Java only numeric types can be used in switch statements.(including enums which are simply names mapped onto integers). In Scala you can use almost use anything you want. Infact scala supports custom extractor patterns, whose scope is beyond this text. Please consider visiting scala resources to learn more.&lt;br /&gt;
# The cases do not allow fall through. In Java and C, you have to manually type 'break' after the end of every case. Scala does not have any 'break' nor 'continue' keywords(shocking?)&lt;br /&gt;
&lt;br /&gt;
A purely object oriented person may argue that we could use Visitor Pattern to do the same. That is true, but how many lines of code would you have to write to accomplish the same functionality? Wanna guess?[http://www.artima.com/weblogs/viewpost.jsp?thread=166742]&lt;br /&gt;
&lt;br /&gt;
More explanation of case statements:&lt;br /&gt;
* 'match' is equivalent to switch keyword in C, C++ and Java.&lt;br /&gt;
* 'case' keyword signifies the beginning of the particular case.&lt;br /&gt;
* The '_'(underscore) signifies many things in scala. When we say &amp;quot;case _ =&amp;gt; &amp;quot;, it is equivalent to the 'default' keyword in C/Java.&lt;br /&gt;
&lt;br /&gt;
===== Handling exceptions - Scala way! =====&lt;br /&gt;
In Java, exception matching is considered verbose and ugly. To open and write a file,&lt;br /&gt;
 try {&lt;br /&gt;
    File file = new File(&amp;quot;/tmp/sample.txt&amp;quot;);&lt;br /&gt;
    PrintStream printStream = new PrintStream(file);&lt;br /&gt;
    printStream.write(&amp;quot;something&amp;quot;.getBytes());&lt;br /&gt;
 } catch (FileNotFoundException e) {&lt;br /&gt;
    System.out.println(&amp;quot;Unable to find the file /tmp/sample.txt&amp;quot;);&lt;br /&gt;
    e.printStackTrace();&lt;br /&gt;
 } catch (IOException e) {&lt;br /&gt;
   System.out.println(&amp;quot;Unknown IO error&amp;quot;);&lt;br /&gt;
    e.printStackTrace();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
  try{&lt;br /&gt;
    val file = new File(&amp;quot;/tmp/sample.txt&amp;quot;)&lt;br /&gt;
    val ps = new PrintStream(file)&lt;br /&gt;
    ps.write(&amp;quot;something&amp;quot;.getBytes())&lt;br /&gt;
  }catch{&lt;br /&gt;
    case e: FileNotFoundException =&amp;gt; println(&amp;quot;Unable to find file /tmp/sample.txt&amp;quot;)&lt;br /&gt;
    case IOException =&amp;gt; println(&amp;quot;Unknown IO error&amp;quot;)&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
We have just scratched the surface of Scala, but it has innumerable features which show a beautiful blend of functional and object oriented features. Scala proves we can use them both together.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Functional programming integrated with object oriented style leads to:&lt;br /&gt;
&lt;br /&gt;
#better understanding of program behavior as a result of immutability features&lt;br /&gt;
#use of efficient recursive techniques such as tail call optimization&lt;br /&gt;
#Reduction in number of lines of code&lt;br /&gt;
&lt;br /&gt;
By using these techniques we utilize the best of both functional and object oriented techniques.&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
*[1]: http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)&lt;br /&gt;
*[2]: http://www.haskell.org&lt;br /&gt;
*[3]: http://www.scala-lang.org&lt;br /&gt;
*[4]: http://bit.ly/16Qs6l&lt;br /&gt;
*[5]: http://bit.ly/3o5JA1&lt;br /&gt;
*[6]: http://www.artima.com/weblogs/viewpost.jsp?thread=166742&lt;br /&gt;
*[7]: http://lamp.epfl.ch/~odersky/&lt;br /&gt;
*[8]: http://www.scala-lang.org/docu/files/collections-api/collections.html#&lt;br /&gt;
*[9]: http://www.codecommit.com/blog/java/interop-between-java-and-scala&lt;br /&gt;
*[10]: http://www.codecommit.com/blog/scala/what-is-hindley-milner-and-why-is-it-cool&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_az&amp;diff=34541</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1e az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_az&amp;diff=34541"/>
		<updated>2010-09-09T02:39:29Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Programming Paradigms */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Programming Paradigms==&lt;br /&gt;
 &lt;br /&gt;
Every computer program needs a style of writing which specifies how to solve a software engineering problem. This style is represented by the paradigm. Each computer program follows one or more paradigm which differs in representing the elements of a program(such as variables and objects) and the steps needed to compute a task.&lt;br /&gt;
&lt;br /&gt;
Diferent paradigms are:&lt;br /&gt;
&lt;br /&gt;
# Procedural/imperative paradigms: Assembly, C, C++, Java, C\#&lt;br /&gt;
&lt;br /&gt;
# Object Oriented paradigm : C++, Java, Python, Ruby, Scala, C\#&lt;br /&gt;
&lt;br /&gt;
# Functional Paradigm : Lisp, Haskell, Clojure, Scala, OCaml, Ruby&lt;br /&gt;
&lt;br /&gt;
# Logic Paradigm: Prolog&lt;br /&gt;
&lt;br /&gt;
==Multi-Paradigm Programming==&lt;br /&gt;
&lt;br /&gt;
Multiparadigm refers to use of a combination of programming paradigms for solving a computer problem. Some languages subscribe strictly to a single paradigm like Assembly and C. Others like Java, C++, Scala and C# employ more than one paradigm. Every paradigm comes with its own strength and weakness and this quite motivates us to take advantage of each paradigm and use it in a manner that best fits the problem at hand.&lt;br /&gt;
&lt;br /&gt;
==Overview of Functional Programming==&lt;br /&gt;
&lt;br /&gt;
Functional programming is derived from lambda calculus which models computations as the evaluation of functions and recursions. More emphasis is laid on application of functions rather than changing the state of variables in a program. Functional programming relies heavily on recursions and it is the only way to iterate instead of loops.&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;How it differs from imperative programming&amp;lt;/b&amp;gt;===&lt;br /&gt;
    &lt;br /&gt;
Imperative programming follows the Von-Neumann architecture and mainly consists of loops and usage of globa states to perform a computation. For example consider the task of computing the sum of numbers from 1 to n. &lt;br /&gt;
&lt;br /&gt;
In imperative style,&lt;br /&gt;
        &lt;br /&gt;
        sum := 0         // global state&lt;br /&gt;
        for i &amp;lt;- 1 to n do&lt;br /&gt;
            sum := sum + i&lt;br /&gt;
            &lt;br /&gt;
In Functional style,&lt;br /&gt;
    &lt;br /&gt;
         func sum(n)        // note that sum is a function and is recursive&lt;br /&gt;
           if  n = 1 then&lt;br /&gt;
               return 1&lt;br /&gt;
           else&lt;br /&gt;
               return n + sum(n-1)&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Pure and impure functional programming&amp;lt;/b&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
Purely functional programming exhibit referential transparency [http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)] which does not involve any global state or I/O changes. If the same functional expression results in the same output value for the same argument x at different stages of execution , then the function is said to be pure which is devoid of any global state change. For example Haskell [http://www.haskell.org] is purely functional.&lt;br /&gt;
&lt;br /&gt;
==Overview of object oriented programming==&lt;br /&gt;
&lt;br /&gt;
Typially programs were written with a procedural view where the program's logic was given utmost importance and it follows a sequential structure. But object oriented techniques really cares about the data or objects we want to mainpulate with rather than the logic of  a program. Every object contains its own state(in the form of variables) and and a number of methods to manipulate the variables.For example consider the class Add:&lt;br /&gt;
           &lt;br /&gt;
           class Add{&lt;br /&gt;
           /* Object variables */&lt;br /&gt;
                  private int sum = 0;&lt;br /&gt;
          /* Object Methods */&lt;br /&gt;
                  public void calculate_sum(int n){&lt;br /&gt;
                      int i=0;&lt;br /&gt;
                      while(i &amp;lt;= n){&lt;br /&gt;
                          sum = sum + i;&lt;br /&gt;
                          i++;&lt;br /&gt;
                      }&lt;br /&gt;
                  }&lt;br /&gt;
            }&lt;br /&gt;
       &lt;br /&gt;
            Add instance1 = new Add();     // instance1 is an object of type Add&lt;br /&gt;
            instance1.calculate_sum(100);&lt;br /&gt;
           &lt;br /&gt;
===&amp;lt;b&amp;gt;Principles of object oriented design&amp;lt;/b&amp;gt;===&lt;br /&gt;
           &lt;br /&gt;
1. &amp;lt;b&amp;gt;Encapsulation:&amp;lt;/b&amp;gt; Encapsulation refers to hiding the internal representation of the object from the outside view. For example algorithm to compute the calculate_sum() method may be hidden, but it will present the user with the expected results.&lt;br /&gt;
&lt;br /&gt;
2. &amp;lt;b&amp;gt;Polymorphism:&amp;lt;/b&amp;gt; Polymorphism means ability to take multiple forms. For example an operation may exhibit different behaviour at different instances. Consider the operator '+', for numbers it will generate the sum, but for strings it will produce a third string which concatenates the input strings.&lt;br /&gt;
&lt;br /&gt;
3. &amp;lt;b&amp;gt;Inheritance:&amp;lt;/b&amp;gt; Inheritance involves base and derived classes with derived class inheriting all the methods and states frm the base class. Inheritance basically forms a hirerachy. For example if shape is an object, then objects square, rectangle, circle all derive the same characteristic as shape does.&lt;br /&gt;
&lt;br /&gt;
==Functional + OOP code==&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Functions as Objects&amp;lt;/b&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
One of the cornerstone of functional and object oriented code is treating functions as objects.It means that we can pass functions as arguments, store it and return them from other functions. This is highly useful in an user interface code where it can be used as call back functions which get called when an event occurs( in event driven programming)&lt;br /&gt;
&lt;br /&gt;
An example in Scala [http://www.scala-lang.org]:&lt;br /&gt;
&lt;br /&gt;
  Object Timer{&lt;br /&gt;
    def  action(callback() : () =&amp;gt; unit){              // callback() is the function passed as an argument&lt;br /&gt;
         while( some condition)                                                            &lt;br /&gt;
         { &lt;br /&gt;
            callback();&lt;br /&gt;
            Thread.sleep(3000)&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
     def event(){                                      //  event is the method which tells what to do &lt;br /&gt;
         /* process the event here&lt;br /&gt;
         &lt;br /&gt;
         */      &lt;br /&gt;
     }&lt;br /&gt;
        &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Another example of functional concept in object oriented design is the concept of blocks especially in Ruby. Blocks are basically nameless functions which can be passed to a function and then that function can invoke the passed in nameless function. This is a common style called higher order function style among languages that handle functions as first class objects. Basically blocks can be designed for loop abstraction and lets the user to design their own way of iterating the values, thereby hiding the implementation details. &lt;br /&gt;
For example if we want to iterate backwards from the end to the beginning without revealing its internal implementations, we could implement the loop logic inside the block and pass it to a method or function.&lt;br /&gt;
&lt;br /&gt;
Here is a Ruby implementation of block&lt;br /&gt;
 &lt;br /&gt;
 def printlist(array,&amp;amp;reverse)&lt;br /&gt;
    array.each do |element|&lt;br /&gt;
       puts &amp;quot;array element: #{element}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    reverse.call(array)   /* Here it prints the reverse implementation of list*/&lt;br /&gt;
    end&lt;br /&gt;
  &lt;br /&gt;
 printlist([1,2,3,4,5,6]) { |array| &lt;br /&gt;
   array.reverse.each do |element| &lt;br /&gt;
     puts &amp;quot;array element: #{element}&amp;quot; &lt;br /&gt;
   end &lt;br /&gt;
 }&lt;br /&gt;
Next we will take a look at some of the features of Scala which supports a blend of functional and object oriented concepts in a concise manner.&lt;br /&gt;
&lt;br /&gt;
= Scala =&lt;br /&gt;
Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages&lt;br /&gt;
&lt;br /&gt;
Scala can be considered a very good blend of the best of functional and object oriented world. It was conceived(in 2001) and implemented by Martin Odersky who was also the author of Sun javac compiler and played a major role in the design and retrofitting of generics into Java. The latest version of Scala is 2.8 and includes a number of performance improvements over previous versions. We shall see more about the features of Scala in the following pages, but for fun, some facts!&lt;br /&gt;
&lt;br /&gt;
=== Features ===&lt;br /&gt;
==== Statically Typed ====&lt;br /&gt;
Scala belongs to the family of languages which do not allow the type of variables once they have been defined. In other words, the type information of each and every variable is encoded in the generated code and the compiler makes sure, you are not doing something funny. For example, lets consider the following example,&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
   irb(main):001:0&amp;gt; str=&amp;quot;Hello World&amp;quot;&lt;br /&gt;
    =&amp;gt; &amp;quot;Hello World&amp;quot;&lt;br /&gt;
    irb(main):002:0&amp;gt; puts str.class&lt;br /&gt;
    String&lt;br /&gt;
    irb(main):003:0&amp;gt; str=123&lt;br /&gt;
    =&amp;gt; 123&lt;br /&gt;
    irb(main):004:0&amp;gt; puts str.class&lt;br /&gt;
    Fixnum&lt;br /&gt;
&lt;br /&gt;
In the above example, first we define 'str' variable to be of type String and then as a number. So basically, no type information is associated with the variable at any point in the program. Hence we can easily reassociate that variable with an object/instance&lt;br /&gt;
of a different type. This is a feature of all dynamically typed languages(like Ruby, Python, Clojure). However most of the statically typed languages such as Scala, Java, C/C++ do not allow you do so. In Scala,&lt;br /&gt;
&lt;br /&gt;
  scala&amp;gt; var str=&amp;quot;Hello World&amp;quot;&lt;br /&gt;
  str: java.lang.String = Hello World&lt;br /&gt;
  scala&amp;gt; str=123&lt;br /&gt;
  &amp;lt;console&amp;gt;:6: error: type mismatch;&lt;br /&gt;
   found   : Int(123)&lt;br /&gt;
   required: java.lang.String&lt;br /&gt;
         str=123&lt;br /&gt;
==== Mixed paradigm ====&lt;br /&gt;
Scala is a strange specimen. It is a complete blend of object oriented and functional programming. How can this happen? Functional world advocates immutability but object oriented world is all about state and how do you mutate it to fit your needs. Scala allows you create both mutable and immutable objects. Consider the following example,&lt;br /&gt;
 val str: String = &amp;quot;Hello World&amp;quot;&lt;br /&gt;
In the first line, I am creating a immutable variable(a misnomer indeed). Roughly translating the above line to Java is below:&lt;br /&gt;
&lt;br /&gt;
 final String str = &amp;quot;Hello World&amp;quot;; //note the extra semi-colon&lt;br /&gt;
Consider the next example below,&lt;br /&gt;
 val list: List[String] = List(&amp;quot;Scala&amp;quot;, &amp;quot;is&amp;quot;, &amp;quot;the coolest&amp;quot;, &amp;quot;language&amp;quot;, &amp;quot;in the world!&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The same code can be written in Java as below&lt;br /&gt;
&lt;br /&gt;
  List&amp;lt;String&amp;gt; mutableList = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
  list.add(&amp;quot;Scala&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;is&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;the coolest&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;language&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;in the world!&amp;quot;);&lt;br /&gt;
  List&amp;lt;String&amp;gt; immutableList = Collections.immutableList(mutableList);&lt;br /&gt;
&lt;br /&gt;
Compared to Java, Scala reduces the total number of lines you need to type and at the same time looks so much simpler and less verbose. Even though the above example shows immutable collection example, scala also provides mutable collection and object types under the package &amp;lt;code&amp;gt;scala.collection.mutable.*&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As it can be observed, even though scala advocates immutability, it does not restrict you from creating mutable objects. So its the best of both worlds. Another part of functional world is the concept of functions as ''''first class members'''' of the language. Some of the common languages like Ruby, Python and JavaScript all incorporate this feature. In the sense you can pass around functions to methods as a parameter. Enough talking;&lt;br /&gt;
&lt;br /&gt;
Let us have a class say &amp;lt;code&amp;gt;'Person'&amp;lt;/code&amp;gt; which has properties, &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; and another variable &amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt;.&lt;br /&gt;
In Java:&lt;br /&gt;
&lt;br /&gt;
 class Person{&lt;br /&gt;
    private String name;&lt;br /&gt;
    private int age;&lt;br /&gt;
    public String getName(){&lt;br /&gt;
        return name;&lt;br /&gt;
    }&lt;br /&gt;
    public void setName(String s){&lt;br /&gt;
        this.name = s;&lt;br /&gt;
    }&lt;br /&gt;
    public int getAge(){&lt;br /&gt;
        return age;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAge(int age){&lt;br /&gt;
        this.age = age;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
 case class Person(name: String, age: Int)&lt;br /&gt;
&lt;br /&gt;
This single line is equivalent to the above java code! So much cooler.. I would not be doing justice if I don't mention the fact that getters for &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt; are generated. Also equals() and hashCode() method are automatically generated for you by the compiler. Isn't this an awesome feature? No wonder Scala is considered a beautiful and high level language. Now assume we have a list of persons and we want to filter out all the persons who are of age 23. How would you do that in Java?&lt;br /&gt;
&lt;br /&gt;
  List&amp;lt;Person&amp;gt; persons; //has a bunch of person objects in it.&lt;br /&gt;
  List&amp;lt;Person&amp;gt; twentyThree = new ArrayList&amp;lt;Person&amp;gt;();&lt;br /&gt;
  for(Person p: persons){&lt;br /&gt;
      if(p.getAge() == 23){&lt;br /&gt;
          twentyThree.add(p);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
&lt;br /&gt;
 val twentyThree: List[Person] = persons.filter( _.age == 23)&lt;br /&gt;
&lt;br /&gt;
That's it! In case you are wondering whats going on, filter is a method present on scala.List class and the code block  _.age == 23 is a function created on the fly and passed onto the filter method. More precisely its called a closure - a functional concept which can be emulated in java only using anonymous inner classes. In case you are wondering how scala does this, scala's compiler transforms our one line function into some anonymous inner classes when generating java byte code but allows developer to define functions in a simple manner.&lt;br /&gt;
&lt;br /&gt;
Infact every function is an object in scala. Scala's standard library has a number of functions which are object themselves.&lt;br /&gt;
&lt;br /&gt;
==== Sophisticated Syntax/Features ====&lt;br /&gt;
When we say sophisticated, its really an awesome feature which is present in other functional languages like Haskell, (O)Caml and Erlang. Consider the following example,&lt;br /&gt;
&lt;br /&gt;
   val name = &amp;quot;Jackie Chan&amp;quot;&lt;br /&gt;
   println(name.getClass) //prints out String.&lt;br /&gt;
&lt;br /&gt;
Previously we saw that scala is a statically typed language and yet in the above example we don't specify what type is the variable name. In Java, you would type in the following:&lt;br /&gt;
&lt;br /&gt;
 String name = &amp;quot;Jackie Chan&amp;quot;&lt;br /&gt;
&lt;br /&gt;
So basically in Java you specify what type the name variable is. Scala compiler is very smart in the manner that it can automatically detect the type of the variable even if you don't specify it. Consider the following example,&lt;br /&gt;
&lt;br /&gt;
 case class Person(name: String, age: Int)&lt;br /&gt;
 val persons = List(&lt;br /&gt;
                        Person(&amp;quot;Jackie Chan&amp;quot;, 51), Person(&amp;quot;Jet Li&amp;quot;, 41),&lt;br /&gt;
                        Person(&amp;quot;RajniKanth&amp;quot;, 51), Person(&amp;quot;Kamal Hasan&amp;quot;, 44),&lt;br /&gt;
                        Person(&amp;quot;Martin Odersky&amp;quot;, 18), Person(&amp;quot;Steve Yegge&amp;quot;, 23))&lt;br /&gt;
 //notice that you don't have to use 'new' keyword to create new instances. This is taken care of by the companion objects.&lt;br /&gt;
&lt;br /&gt;
The above lines of code create a list with 6 different persons(An immutable list)&lt;br /&gt;
&lt;br /&gt;
Now, as in ruby scala supports the concepts of closure. Let's see what it is.&lt;br /&gt;
&lt;br /&gt;
 val twentyThree: List[Person] = persons.filter( { p: Person =&amp;gt; p.age == 3})&lt;br /&gt;
&lt;br /&gt;
In the above set of lines, the code within the '{ }' brackets is a function's body. Basically what the compiler did was to create a function which takes a single argument and returns either true or false. Now that's called a high level language. Ready for more?&lt;br /&gt;
&lt;br /&gt;
==== A Scalable Language ====&lt;br /&gt;
In almost all programming languages, there is a necessity to profile the running time of the procedures and tune its performance. The old way of doing things in Java would be:&lt;br /&gt;
&lt;br /&gt;
 public class Main{&lt;br /&gt;
 public static int factorial(int x){&lt;br /&gt;
   if(x == 0 || x == 1) return 1&lt;br /&gt;
   else return x * factorial(x-1)&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String [] args){&lt;br /&gt;
    long beforeTime = System.currentTimeMillis();&lt;br /&gt;
    int result = factorial(5);&lt;br /&gt;
    System.out.println(&amp;quot;Total time taken to execute factorial(5) is &amp;quot; + (System.currentTimeMillis() - result)+&amp;quot;ms&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;factorial(5) = &amp;quot; + result);&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
However, if you want to find out the execution time of some other method, you would have to type in all the above lines again. So much for code re-use! Lets see how its done in Scala. Hold on to your seats...&lt;br /&gt;
&lt;br /&gt;
 object Main{&lt;br /&gt;
  def main(args: Array[String]){&lt;br /&gt;
    def factorial(x: Int):Int = x match{&lt;br /&gt;
      case 0 | 1 =&amp;gt; 1&lt;br /&gt;
      case _ =&amp;gt; x * factorial(x-1)&lt;br /&gt;
  }&lt;br /&gt;
  import System.currentTimeMillis&lt;br /&gt;
  def timeIt(msg: String = &amp;quot;Time to execute is: &amp;quot;)(func: =&amp;gt; Unit){&lt;br /&gt;
    val before = currentTimeMillis&lt;br /&gt;
    func&lt;br /&gt;
    println(msg + (currentTimeMillis - before) + &amp;quot;ms&amp;quot;)&lt;br /&gt;
  }&lt;br /&gt;
  timeIt(&amp;quot;Total time taken to execute factorial(5) is &amp;quot;){&lt;br /&gt;
      val result = factorial(5)&lt;br /&gt;
      println(&amp;quot;factorial(5) = &amp;quot; + result)&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 //would print: factorial(5) = 120&lt;br /&gt;
 // Total time taken to execute factorial(5) is 1ms&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That's not right! The function &amp;lt;code&amp;gt;timeIt&amp;lt;/code&amp;gt; looks like a feature inbuilt into the language. Turn your disbelief into amazement;save the above code into a separate file and run it. Infact using this feature, it is possible to build powerful DSL languages which can run at full speed on JVM.&lt;br /&gt;
&lt;br /&gt;
Ruby allows you to add new methods to objects. This feature is often called as '''Monkey Patching''' because you are splitting open the class and adding/overriding methods (in)|(to) the class. Often this leads to unexpected behavior at runtime because, other libraries including into build path may depend on some methods which you changed. Scala provides yet another feature called implicits.&lt;br /&gt;
&lt;br /&gt;
In Java, String class is final so there is no way you can add new methods to it. But with implicits you can do more! Suppose you have some text in memory and you know it represents a number. But in Java the usual way to convert string to int is to do a Integer.parseInt(string) which seems so unnecessary. Let us make our lives easier...&lt;br /&gt;
&lt;br /&gt;
  implicit def strToInt(str: String) = new {&lt;br /&gt;
    def asInt = Integer.parseInt(str)&lt;br /&gt;
  }&lt;br /&gt;
  val str = &amp;quot;1234&amp;quot;&lt;br /&gt;
  println(str.asInt * 3) //prints out 3702&lt;br /&gt;
&lt;br /&gt;
The above code gives us an impression as if String class has gained new 'asInt' method. Can static object oriented languages do this? No! Infact scala cheats by calling our implicits to convert it into a different object as and when needed.&lt;br /&gt;
&lt;br /&gt;
The above functionalities are purely object oriented, yet they are safe. They don't pollute the global namespace unlike Ruby or Python&lt;br /&gt;
&lt;br /&gt;
==== Broad Classification of Functions ====&lt;br /&gt;
===== Predicate =====&lt;br /&gt;
It is a function which receives an object as argument and either returns true or false. They are useful in a number of situations. Suppose you had a list of integers and you wanted to filter out all odd numbers you could apply the predicate on every element in the list and&lt;br /&gt;
* If the predicate returned true, add it to the new list&lt;br /&gt;
* else ignore the current entry and try next.&lt;br /&gt;
&lt;br /&gt;
 scala&amp;gt; val numbers = 0 to 20&lt;br /&gt;
 numbers: scala.collection.immutable.Range.Inclusive with  scala.collection.immutable.Range.ByOne = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)&lt;br /&gt;
 scala&amp;gt; val evenNumbers = numbers.filter( _ % 2 == 0)&lt;br /&gt;
 evenNumbers: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20)&lt;br /&gt;
&lt;br /&gt;
===== Closure =====&lt;br /&gt;
A function which does some operation on an object but does not return any result. In otherwords, the return type of closure is void. Say we have a list of strings and we need to print them all to the console; we can do it the following way:&lt;br /&gt;
&lt;br /&gt;
 scala&amp;gt; val strs = List(&amp;quot;print&amp;quot;, &amp;quot;all&amp;quot;, &amp;quot;to&amp;quot;, &amp;quot;console&amp;quot;)&lt;br /&gt;
 strs: List[java.lang.String] = List(print, all, to, console)&lt;br /&gt;
 scala&amp;gt; strs.foreach(println) //println == System.out.println&lt;br /&gt;
 print&lt;br /&gt;
 all&lt;br /&gt;
 to&lt;br /&gt;
 console&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; is a method which is executed for every element present in the list.&lt;br /&gt;
* &amp;lt;code&amp;gt; str.foreach(println)&amp;lt;/code&amp;gt; - Now, this is getting little confusing. Whats &amp;lt;code&amp;gt;println&amp;lt;/code&amp;gt; doing without any arguments? Scala's compiler is intelligent enough to determine that println takes in a single argument and so allows us to skip specifying it.&lt;br /&gt;
===== Transformer =====&lt;br /&gt;
A function which recieves an entity/object and returns an another object(possibly of different type). Infact both predicate and a closure are transformers. To understand more what a transformer is, let us change the case of all the strings in a list.&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
   val list = List(&amp;quot;Scala&amp;quot;, &amp;quot;is&amp;quot;, &amp;quot;the coolest&amp;quot;, &amp;quot;language&amp;quot;, &amp;quot;in the world!&amp;quot;)&lt;br /&gt;
 scala&amp;gt; list.map(_.toUpperCase)&lt;br /&gt;
 res5: List[java.lang.String] = List(SCALA, IS, THE COOLEST, LANGUAGE, IN THE WORLD!)&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;scala.List's&amp;lt;/code&amp;gt; method which takes in a function as a parameter and for every element in the list invokes the function. The result returned by the function is added to a new list.&lt;br /&gt;
* &amp;lt;code&amp;gt;_.toUpperCase&amp;lt;/code&amp;gt; is the transformer function upcasing all the strings.&lt;br /&gt;
&lt;br /&gt;
==== Currying ====&lt;br /&gt;
Excellent tutorial here: [http://bit.ly/16Qs6l]&lt;br /&gt;
Named after its inventor 'Haskell Curry', it is a way of converting a function which takes multiple arguments, into a function with lesser number of arguments. Theory is always boring or we feel so. Let's jump right in and work out an example. Consider a simple function which adds two numbers.&lt;br /&gt;
&lt;br /&gt;
 def add(x:Int)(y:Int) = x + y&lt;br /&gt;
&lt;br /&gt;
Now assume you want to create a function which will add '5' to all its input and return the sum. Can you re-use the code above? Currying works great in these scenarios..&lt;br /&gt;
 val add5ToInput = add(5) _  // the '_' is to tell the compiler to create a curried version of the above function.&lt;br /&gt;
 println(add5ToInput(10))  //surprise surprise - prints 15!&lt;br /&gt;
&lt;br /&gt;
So basically what happens is that compiler creates an intermediate function which always has &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; as the first parameter. So when we say &amp;lt;code&amp;gt;add5ToInput(10)&amp;lt;/code&amp;gt; in reality, it inturn calls &amp;lt;code&amp;gt;add(5)(10)&amp;lt;/code&amp;gt;. So currying basically helps us reduce the total number of parameters which needs to passed to functions and in turn helps re-use code in a functional way!&lt;br /&gt;
&lt;br /&gt;
==== Pattern Matching ====&lt;br /&gt;
also see [http://bit.ly/3o5JA1]&lt;br /&gt;
Yet another unique feature of functional programming languages is the concept of pattern matching. Let us see what pattern matching is all about. Continuing our previous example of Person objects, let us write some code to guess the profession of a person based on his name.(I know it sounds lame..)&lt;br /&gt;
&lt;br /&gt;
 def guessProfession(p: Person) = p.name match {&lt;br /&gt;
        case &amp;quot;Jackie Chan&amp;quot; | &amp;quot;Jet Li&amp;quot; =&amp;gt; &amp;quot;Martial Artist and Actor&amp;quot;&lt;br /&gt;
        case &amp;quot;RajniKanth&amp;quot; | &amp;quot;Kamal Hasan&amp;quot; =&amp;gt; &amp;quot;South Indian Actor&amp;quot;&lt;br /&gt;
        case &amp;quot;Martin Odersky&amp;quot; | &amp;quot;Steve Yegge&amp;quot; =&amp;gt; &amp;quot;Famous Programmer&amp;quot;&lt;br /&gt;
        case _ =&amp;gt; &amp;quot;I don't know %s&amp;quot;.format(p.name)&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 val p = Person(&amp;quot;Kovalan Venkatesan&amp;quot;, 24)&lt;br /&gt;
 val p1 = Person(&amp;quot;Martin Odersky&amp;quot;, 31)&lt;br /&gt;
 println(guessProfession(p)) //prints I don't know Kovalan Venkatesan.&lt;br /&gt;
 println(guessProfession(p1)) //prints &amp;quot;Famous Programmer&amp;quot;&lt;br /&gt;
&lt;br /&gt;
As you may have noticed, the above example is very similar to switch statements in imperative languages such as C, Java. However, it is more powerful and extensible. Why?&lt;br /&gt;
&lt;br /&gt;
# In both C and Java only numeric types can be used in switch statements.(including enums which are simply names mapped onto integers). In Scala you can use almost use anything you want. Infact scala supports custom extractor patterns, whose scope is beyond this text. Please consider visiting scala resources to learn more.&lt;br /&gt;
# The cases do not allow fall through. In Java and C, you have to manually type 'break' after the end of every case. Scala does not have any 'break' nor 'continue' keywords(shocking?)&lt;br /&gt;
&lt;br /&gt;
A purely object oriented person may argue that we could use Visitor Pattern to do the same. That is true, but how many lines of code would you have to write to accomplish the same functionality? Wanna guess?[http://www.artima.com/weblogs/viewpost.jsp?thread=166742]&lt;br /&gt;
&lt;br /&gt;
More explanation of case statements:&lt;br /&gt;
* 'match' is equivalent to switch keyword in C, C++ and Java.&lt;br /&gt;
* 'case' keyword signifies the beginning of the particular case.&lt;br /&gt;
* The '_'(underscore) signifies many things in scala. When we say &amp;quot;case _ =&amp;gt; &amp;quot;, it is equivalent to the 'default' keyword in C/Java.&lt;br /&gt;
&lt;br /&gt;
===== Handling exceptions - Scala way! =====&lt;br /&gt;
In Java, exception matching is considered verbose and ugly. To open and write a file,&lt;br /&gt;
 try {&lt;br /&gt;
    File file = new File(&amp;quot;/tmp/sample.txt&amp;quot;);&lt;br /&gt;
    PrintStream printStream = new PrintStream(file);&lt;br /&gt;
    printStream.write(&amp;quot;something&amp;quot;.getBytes());&lt;br /&gt;
 } catch (FileNotFoundException e) {&lt;br /&gt;
    System.out.println(&amp;quot;Unable to find the file /tmp/sample.txt&amp;quot;);&lt;br /&gt;
    e.printStackTrace();&lt;br /&gt;
 } catch (IOException e) {&lt;br /&gt;
   System.out.println(&amp;quot;Unknown IO error&amp;quot;);&lt;br /&gt;
    e.printStackTrace();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
  try{&lt;br /&gt;
    val file = new File(&amp;quot;/tmp/sample.txt&amp;quot;)&lt;br /&gt;
    val ps = new PrintStream(file)&lt;br /&gt;
    ps.write(&amp;quot;something&amp;quot;.getBytes())&lt;br /&gt;
  }catch{&lt;br /&gt;
    case e: FileNotFoundException =&amp;gt; println(&amp;quot;Unable to find file /tmp/sample.txt&amp;quot;)&lt;br /&gt;
    case IOException =&amp;gt; println(&amp;quot;Unknown IO error&amp;quot;)&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
We have just scratched the surface of Scala, but it has innumerable features which show a beautiful blend of functional and object oriented features. Scala proves we can use them both together.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Functional programming integrated with object oriented style leads to:&lt;br /&gt;
&lt;br /&gt;
#better understanding of program behavior as a result of immutability features&lt;br /&gt;
#use of efficient recursive techniques such as tail call optimization&lt;br /&gt;
#Reduction in number of lines of code&lt;br /&gt;
&lt;br /&gt;
By using these techniques we utilize the best of both functional and object oriented techniques.&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
*[1]: http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)&lt;br /&gt;
*[2]: http://www.haskell.org&lt;br /&gt;
*[3]: http://www.scala-lang.org&lt;br /&gt;
*[4]: http://bit.ly/16Qs6l&lt;br /&gt;
*[5]: http://bit.ly/3o5JA1&lt;br /&gt;
*[6]: http://www.artima.com/weblogs/viewpost.jsp?thread=166742&lt;br /&gt;
*[7]: http://lamp.epfl.ch/~odersky/&lt;br /&gt;
*[8]: http://www.scala-lang.org/docu/files/collections-api/collections.html#&lt;br /&gt;
*[9]: http://www.codecommit.com/blog/java/interop-between-java-and-scala&lt;br /&gt;
*[10]: http://www.codecommit.com/blog/scala/what-is-hindley-milner-and-why-is-it-cool&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_az&amp;diff=34538</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1e az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_az&amp;diff=34538"/>
		<updated>2010-09-09T02:38:54Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Programming Paradigms */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Programming Paradigms==&lt;br /&gt;
 &lt;br /&gt;
Every computer program needs a style of writing which specifies how to solve a software engineering problem. This style is represented by the paradigm. Each computer program follows one or more paradigm which differs in representing the elements of a program(such as variables and objects) and the steps needed to compute a task.&lt;br /&gt;
&lt;br /&gt;
Diferent paradigms are:&lt;br /&gt;
&lt;br /&gt;
#Procedural/imperative paradigms: Assembly, C, C++, Java, C#&lt;br /&gt;
&lt;br /&gt;
#Object Oriented paradigm : C++, Java, Python, Ruby, Scala, C#&lt;br /&gt;
&lt;br /&gt;
#Functional Paradigm : Lisp, Haskell, Clojure, Scala, OCaml, Ruby&lt;br /&gt;
&lt;br /&gt;
#Logic Paradigm: Prolog&lt;br /&gt;
&lt;br /&gt;
==Multi-Paradigm Programming==&lt;br /&gt;
&lt;br /&gt;
Multiparadigm refers to use of a combination of programming paradigms for solving a computer problem. Some languages subscribe strictly to a single paradigm like Assembly and C. Others like Java, C++, Scala and C# employ more than one paradigm. Every paradigm comes with its own strength and weakness and this quite motivates us to take advantage of each paradigm and use it in a manner that best fits the problem at hand.&lt;br /&gt;
&lt;br /&gt;
==Overview of Functional Programming==&lt;br /&gt;
&lt;br /&gt;
Functional programming is derived from lambda calculus which models computations as the evaluation of functions and recursions. More emphasis is laid on application of functions rather than changing the state of variables in a program. Functional programming relies heavily on recursions and it is the only way to iterate instead of loops.&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;How it differs from imperative programming&amp;lt;/b&amp;gt;===&lt;br /&gt;
    &lt;br /&gt;
Imperative programming follows the Von-Neumann architecture and mainly consists of loops and usage of globa states to perform a computation. For example consider the task of computing the sum of numbers from 1 to n. &lt;br /&gt;
&lt;br /&gt;
In imperative style,&lt;br /&gt;
        &lt;br /&gt;
        sum := 0         // global state&lt;br /&gt;
        for i &amp;lt;- 1 to n do&lt;br /&gt;
            sum := sum + i&lt;br /&gt;
            &lt;br /&gt;
In Functional style,&lt;br /&gt;
    &lt;br /&gt;
         func sum(n)        // note that sum is a function and is recursive&lt;br /&gt;
           if  n = 1 then&lt;br /&gt;
               return 1&lt;br /&gt;
           else&lt;br /&gt;
               return n + sum(n-1)&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Pure and impure functional programming&amp;lt;/b&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
Purely functional programming exhibit referential transparency [http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)] which does not involve any global state or I/O changes. If the same functional expression results in the same output value for the same argument x at different stages of execution , then the function is said to be pure which is devoid of any global state change. For example Haskell [http://www.haskell.org] is purely functional.&lt;br /&gt;
&lt;br /&gt;
==Overview of object oriented programming==&lt;br /&gt;
&lt;br /&gt;
Typially programs were written with a procedural view where the program's logic was given utmost importance and it follows a sequential structure. But object oriented techniques really cares about the data or objects we want to mainpulate with rather than the logic of  a program. Every object contains its own state(in the form of variables) and and a number of methods to manipulate the variables.For example consider the class Add:&lt;br /&gt;
           &lt;br /&gt;
           class Add{&lt;br /&gt;
           /* Object variables */&lt;br /&gt;
                  private int sum = 0;&lt;br /&gt;
          /* Object Methods */&lt;br /&gt;
                  public void calculate_sum(int n){&lt;br /&gt;
                      int i=0;&lt;br /&gt;
                      while(i &amp;lt;= n){&lt;br /&gt;
                          sum = sum + i;&lt;br /&gt;
                          i++;&lt;br /&gt;
                      }&lt;br /&gt;
                  }&lt;br /&gt;
            }&lt;br /&gt;
       &lt;br /&gt;
            Add instance1 = new Add();     // instance1 is an object of type Add&lt;br /&gt;
            instance1.calculate_sum(100);&lt;br /&gt;
           &lt;br /&gt;
===&amp;lt;b&amp;gt;Principles of object oriented design&amp;lt;/b&amp;gt;===&lt;br /&gt;
           &lt;br /&gt;
1. &amp;lt;b&amp;gt;Encapsulation:&amp;lt;/b&amp;gt; Encapsulation refers to hiding the internal representation of the object from the outside view. For example algorithm to compute the calculate_sum() method may be hidden, but it will present the user with the expected results.&lt;br /&gt;
&lt;br /&gt;
2. &amp;lt;b&amp;gt;Polymorphism:&amp;lt;/b&amp;gt; Polymorphism means ability to take multiple forms. For example an operation may exhibit different behaviour at different instances. Consider the operator '+', for numbers it will generate the sum, but for strings it will produce a third string which concatenates the input strings.&lt;br /&gt;
&lt;br /&gt;
3. &amp;lt;b&amp;gt;Inheritance:&amp;lt;/b&amp;gt; Inheritance involves base and derived classes with derived class inheriting all the methods and states frm the base class. Inheritance basically forms a hirerachy. For example if shape is an object, then objects square, rectangle, circle all derive the same characteristic as shape does.&lt;br /&gt;
&lt;br /&gt;
==Functional + OOP code==&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Functions as Objects&amp;lt;/b&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
One of the cornerstone of functional and object oriented code is treating functions as objects.It means that we can pass functions as arguments, store it and return them from other functions. This is highly useful in an user interface code where it can be used as call back functions which get called when an event occurs( in event driven programming)&lt;br /&gt;
&lt;br /&gt;
An example in Scala [http://www.scala-lang.org]:&lt;br /&gt;
&lt;br /&gt;
  Object Timer{&lt;br /&gt;
    def  action(callback() : () =&amp;gt; unit){              // callback() is the function passed as an argument&lt;br /&gt;
         while( some condition)                                                            &lt;br /&gt;
         { &lt;br /&gt;
            callback();&lt;br /&gt;
            Thread.sleep(3000)&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
     def event(){                                      //  event is the method which tells what to do &lt;br /&gt;
         /* process the event here&lt;br /&gt;
         &lt;br /&gt;
         */      &lt;br /&gt;
     }&lt;br /&gt;
        &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Another example of functional concept in object oriented design is the concept of blocks especially in Ruby. Blocks are basically nameless functions which can be passed to a function and then that function can invoke the passed in nameless function. This is a common style called higher order function style among languages that handle functions as first class objects. Basically blocks can be designed for loop abstraction and lets the user to design their own way of iterating the values, thereby hiding the implementation details. &lt;br /&gt;
For example if we want to iterate backwards from the end to the beginning without revealing its internal implementations, we could implement the loop logic inside the block and pass it to a method or function.&lt;br /&gt;
&lt;br /&gt;
Here is a Ruby implementation of block&lt;br /&gt;
 &lt;br /&gt;
 def printlist(array,&amp;amp;reverse)&lt;br /&gt;
    array.each do |element|&lt;br /&gt;
       puts &amp;quot;array element: #{element}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    reverse.call(array)   /* Here it prints the reverse implementation of list*/&lt;br /&gt;
    end&lt;br /&gt;
  &lt;br /&gt;
 printlist([1,2,3,4,5,6]) { |array| &lt;br /&gt;
   array.reverse.each do |element| &lt;br /&gt;
     puts &amp;quot;array element: #{element}&amp;quot; &lt;br /&gt;
   end &lt;br /&gt;
 }&lt;br /&gt;
Next we will take a look at some of the features of Scala which supports a blend of functional and object oriented concepts in a concise manner.&lt;br /&gt;
&lt;br /&gt;
= Scala =&lt;br /&gt;
Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages&lt;br /&gt;
&lt;br /&gt;
Scala can be considered a very good blend of the best of functional and object oriented world. It was conceived(in 2001) and implemented by Martin Odersky who was also the author of Sun javac compiler and played a major role in the design and retrofitting of generics into Java. The latest version of Scala is 2.8 and includes a number of performance improvements over previous versions. We shall see more about the features of Scala in the following pages, but for fun, some facts!&lt;br /&gt;
&lt;br /&gt;
=== Features ===&lt;br /&gt;
==== Statically Typed ====&lt;br /&gt;
Scala belongs to the family of languages which do not allow the type of variables once they have been defined. In other words, the type information of each and every variable is encoded in the generated code and the compiler makes sure, you are not doing something funny. For example, lets consider the following example,&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
   irb(main):001:0&amp;gt; str=&amp;quot;Hello World&amp;quot;&lt;br /&gt;
    =&amp;gt; &amp;quot;Hello World&amp;quot;&lt;br /&gt;
    irb(main):002:0&amp;gt; puts str.class&lt;br /&gt;
    String&lt;br /&gt;
    irb(main):003:0&amp;gt; str=123&lt;br /&gt;
    =&amp;gt; 123&lt;br /&gt;
    irb(main):004:0&amp;gt; puts str.class&lt;br /&gt;
    Fixnum&lt;br /&gt;
&lt;br /&gt;
In the above example, first we define 'str' variable to be of type String and then as a number. So basically, no type information is associated with the variable at any point in the program. Hence we can easily reassociate that variable with an object/instance&lt;br /&gt;
of a different type. This is a feature of all dynamically typed languages(like Ruby, Python, Clojure). However most of the statically typed languages such as Scala, Java, C/C++ do not allow you do so. In Scala,&lt;br /&gt;
&lt;br /&gt;
  scala&amp;gt; var str=&amp;quot;Hello World&amp;quot;&lt;br /&gt;
  str: java.lang.String = Hello World&lt;br /&gt;
  scala&amp;gt; str=123&lt;br /&gt;
  &amp;lt;console&amp;gt;:6: error: type mismatch;&lt;br /&gt;
   found   : Int(123)&lt;br /&gt;
   required: java.lang.String&lt;br /&gt;
         str=123&lt;br /&gt;
==== Mixed paradigm ====&lt;br /&gt;
Scala is a strange specimen. It is a complete blend of object oriented and functional programming. How can this happen? Functional world advocates immutability but object oriented world is all about state and how do you mutate it to fit your needs. Scala allows you create both mutable and immutable objects. Consider the following example,&lt;br /&gt;
 val str: String = &amp;quot;Hello World&amp;quot;&lt;br /&gt;
In the first line, I am creating a immutable variable(a misnomer indeed). Roughly translating the above line to Java is below:&lt;br /&gt;
&lt;br /&gt;
 final String str = &amp;quot;Hello World&amp;quot;; //note the extra semi-colon&lt;br /&gt;
Consider the next example below,&lt;br /&gt;
 val list: List[String] = List(&amp;quot;Scala&amp;quot;, &amp;quot;is&amp;quot;, &amp;quot;the coolest&amp;quot;, &amp;quot;language&amp;quot;, &amp;quot;in the world!&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The same code can be written in Java as below&lt;br /&gt;
&lt;br /&gt;
  List&amp;lt;String&amp;gt; mutableList = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
  list.add(&amp;quot;Scala&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;is&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;the coolest&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;language&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;in the world!&amp;quot;);&lt;br /&gt;
  List&amp;lt;String&amp;gt; immutableList = Collections.immutableList(mutableList);&lt;br /&gt;
&lt;br /&gt;
Compared to Java, Scala reduces the total number of lines you need to type and at the same time looks so much simpler and less verbose. Even though the above example shows immutable collection example, scala also provides mutable collection and object types under the package &amp;lt;code&amp;gt;scala.collection.mutable.*&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As it can be observed, even though scala advocates immutability, it does not restrict you from creating mutable objects. So its the best of both worlds. Another part of functional world is the concept of functions as ''''first class members'''' of the language. Some of the common languages like Ruby, Python and JavaScript all incorporate this feature. In the sense you can pass around functions to methods as a parameter. Enough talking;&lt;br /&gt;
&lt;br /&gt;
Let us have a class say &amp;lt;code&amp;gt;'Person'&amp;lt;/code&amp;gt; which has properties, &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; and another variable &amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt;.&lt;br /&gt;
In Java:&lt;br /&gt;
&lt;br /&gt;
 class Person{&lt;br /&gt;
    private String name;&lt;br /&gt;
    private int age;&lt;br /&gt;
    public String getName(){&lt;br /&gt;
        return name;&lt;br /&gt;
    }&lt;br /&gt;
    public void setName(String s){&lt;br /&gt;
        this.name = s;&lt;br /&gt;
    }&lt;br /&gt;
    public int getAge(){&lt;br /&gt;
        return age;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAge(int age){&lt;br /&gt;
        this.age = age;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
 case class Person(name: String, age: Int)&lt;br /&gt;
&lt;br /&gt;
This single line is equivalent to the above java code! So much cooler.. I would not be doing justice if I don't mention the fact that getters for &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt; are generated. Also equals() and hashCode() method are automatically generated for you by the compiler. Isn't this an awesome feature? No wonder Scala is considered a beautiful and high level language. Now assume we have a list of persons and we want to filter out all the persons who are of age 23. How would you do that in Java?&lt;br /&gt;
&lt;br /&gt;
  List&amp;lt;Person&amp;gt; persons; //has a bunch of person objects in it.&lt;br /&gt;
  List&amp;lt;Person&amp;gt; twentyThree = new ArrayList&amp;lt;Person&amp;gt;();&lt;br /&gt;
  for(Person p: persons){&lt;br /&gt;
      if(p.getAge() == 23){&lt;br /&gt;
          twentyThree.add(p);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
&lt;br /&gt;
 val twentyThree: List[Person] = persons.filter( _.age == 23)&lt;br /&gt;
&lt;br /&gt;
That's it! In case you are wondering whats going on, filter is a method present on scala.List class and the code block  _.age == 23 is a function created on the fly and passed onto the filter method. More precisely its called a closure - a functional concept which can be emulated in java only using anonymous inner classes. In case you are wondering how scala does this, scala's compiler transforms our one line function into some anonymous inner classes when generating java byte code but allows developer to define functions in a simple manner.&lt;br /&gt;
&lt;br /&gt;
Infact every function is an object in scala. Scala's standard library has a number of functions which are object themselves.&lt;br /&gt;
&lt;br /&gt;
==== Sophisticated Syntax/Features ====&lt;br /&gt;
When we say sophisticated, its really an awesome feature which is present in other functional languages like Haskell, (O)Caml and Erlang. Consider the following example,&lt;br /&gt;
&lt;br /&gt;
   val name = &amp;quot;Jackie Chan&amp;quot;&lt;br /&gt;
   println(name.getClass) //prints out String.&lt;br /&gt;
&lt;br /&gt;
Previously we saw that scala is a statically typed language and yet in the above example we don't specify what type is the variable name. In Java, you would type in the following:&lt;br /&gt;
&lt;br /&gt;
 String name = &amp;quot;Jackie Chan&amp;quot;&lt;br /&gt;
&lt;br /&gt;
So basically in Java you specify what type the name variable is. Scala compiler is very smart in the manner that it can automatically detect the type of the variable even if you don't specify it. Consider the following example,&lt;br /&gt;
&lt;br /&gt;
 case class Person(name: String, age: Int)&lt;br /&gt;
 val persons = List(&lt;br /&gt;
                        Person(&amp;quot;Jackie Chan&amp;quot;, 51), Person(&amp;quot;Jet Li&amp;quot;, 41),&lt;br /&gt;
                        Person(&amp;quot;RajniKanth&amp;quot;, 51), Person(&amp;quot;Kamal Hasan&amp;quot;, 44),&lt;br /&gt;
                        Person(&amp;quot;Martin Odersky&amp;quot;, 18), Person(&amp;quot;Steve Yegge&amp;quot;, 23))&lt;br /&gt;
 //notice that you don't have to use 'new' keyword to create new instances. This is taken care of by the companion objects.&lt;br /&gt;
&lt;br /&gt;
The above lines of code create a list with 6 different persons(An immutable list)&lt;br /&gt;
&lt;br /&gt;
Now, as in ruby scala supports the concepts of closure. Let's see what it is.&lt;br /&gt;
&lt;br /&gt;
 val twentyThree: List[Person] = persons.filter( { p: Person =&amp;gt; p.age == 3})&lt;br /&gt;
&lt;br /&gt;
In the above set of lines, the code within the '{ }' brackets is a function's body. Basically what the compiler did was to create a function which takes a single argument and returns either true or false. Now that's called a high level language. Ready for more?&lt;br /&gt;
&lt;br /&gt;
==== A Scalable Language ====&lt;br /&gt;
In almost all programming languages, there is a necessity to profile the running time of the procedures and tune its performance. The old way of doing things in Java would be:&lt;br /&gt;
&lt;br /&gt;
 public class Main{&lt;br /&gt;
 public static int factorial(int x){&lt;br /&gt;
   if(x == 0 || x == 1) return 1&lt;br /&gt;
   else return x * factorial(x-1)&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String [] args){&lt;br /&gt;
    long beforeTime = System.currentTimeMillis();&lt;br /&gt;
    int result = factorial(5);&lt;br /&gt;
    System.out.println(&amp;quot;Total time taken to execute factorial(5) is &amp;quot; + (System.currentTimeMillis() - result)+&amp;quot;ms&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;factorial(5) = &amp;quot; + result);&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
However, if you want to find out the execution time of some other method, you would have to type in all the above lines again. So much for code re-use! Lets see how its done in Scala. Hold on to your seats...&lt;br /&gt;
&lt;br /&gt;
 object Main{&lt;br /&gt;
  def main(args: Array[String]){&lt;br /&gt;
    def factorial(x: Int):Int = x match{&lt;br /&gt;
      case 0 | 1 =&amp;gt; 1&lt;br /&gt;
      case _ =&amp;gt; x * factorial(x-1)&lt;br /&gt;
  }&lt;br /&gt;
  import System.currentTimeMillis&lt;br /&gt;
  def timeIt(msg: String = &amp;quot;Time to execute is: &amp;quot;)(func: =&amp;gt; Unit){&lt;br /&gt;
    val before = currentTimeMillis&lt;br /&gt;
    func&lt;br /&gt;
    println(msg + (currentTimeMillis - before) + &amp;quot;ms&amp;quot;)&lt;br /&gt;
  }&lt;br /&gt;
  timeIt(&amp;quot;Total time taken to execute factorial(5) is &amp;quot;){&lt;br /&gt;
      val result = factorial(5)&lt;br /&gt;
      println(&amp;quot;factorial(5) = &amp;quot; + result)&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 //would print: factorial(5) = 120&lt;br /&gt;
 // Total time taken to execute factorial(5) is 1ms&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That's not right! The function &amp;lt;code&amp;gt;timeIt&amp;lt;/code&amp;gt; looks like a feature inbuilt into the language. Turn your disbelief into amazement;save the above code into a separate file and run it. Infact using this feature, it is possible to build powerful DSL languages which can run at full speed on JVM.&lt;br /&gt;
&lt;br /&gt;
Ruby allows you to add new methods to objects. This feature is often called as '''Monkey Patching''' because you are splitting open the class and adding/overriding methods (in)|(to) the class. Often this leads to unexpected behavior at runtime because, other libraries including into build path may depend on some methods which you changed. Scala provides yet another feature called implicits.&lt;br /&gt;
&lt;br /&gt;
In Java, String class is final so there is no way you can add new methods to it. But with implicits you can do more! Suppose you have some text in memory and you know it represents a number. But in Java the usual way to convert string to int is to do a Integer.parseInt(string) which seems so unnecessary. Let us make our lives easier...&lt;br /&gt;
&lt;br /&gt;
  implicit def strToInt(str: String) = new {&lt;br /&gt;
    def asInt = Integer.parseInt(str)&lt;br /&gt;
  }&lt;br /&gt;
  val str = &amp;quot;1234&amp;quot;&lt;br /&gt;
  println(str.asInt * 3) //prints out 3702&lt;br /&gt;
&lt;br /&gt;
The above code gives us an impression as if String class has gained new 'asInt' method. Can static object oriented languages do this? No! Infact scala cheats by calling our implicits to convert it into a different object as and when needed.&lt;br /&gt;
&lt;br /&gt;
The above functionalities are purely object oriented, yet they are safe. They don't pollute the global namespace unlike Ruby or Python&lt;br /&gt;
&lt;br /&gt;
==== Broad Classification of Functions ====&lt;br /&gt;
===== Predicate =====&lt;br /&gt;
It is a function which receives an object as argument and either returns true or false. They are useful in a number of situations. Suppose you had a list of integers and you wanted to filter out all odd numbers you could apply the predicate on every element in the list and&lt;br /&gt;
* If the predicate returned true, add it to the new list&lt;br /&gt;
* else ignore the current entry and try next.&lt;br /&gt;
&lt;br /&gt;
 scala&amp;gt; val numbers = 0 to 20&lt;br /&gt;
 numbers: scala.collection.immutable.Range.Inclusive with  scala.collection.immutable.Range.ByOne = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)&lt;br /&gt;
 scala&amp;gt; val evenNumbers = numbers.filter( _ % 2 == 0)&lt;br /&gt;
 evenNumbers: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20)&lt;br /&gt;
&lt;br /&gt;
===== Closure =====&lt;br /&gt;
A function which does some operation on an object but does not return any result. In otherwords, the return type of closure is void. Say we have a list of strings and we need to print them all to the console; we can do it the following way:&lt;br /&gt;
&lt;br /&gt;
 scala&amp;gt; val strs = List(&amp;quot;print&amp;quot;, &amp;quot;all&amp;quot;, &amp;quot;to&amp;quot;, &amp;quot;console&amp;quot;)&lt;br /&gt;
 strs: List[java.lang.String] = List(print, all, to, console)&lt;br /&gt;
 scala&amp;gt; strs.foreach(println) //println == System.out.println&lt;br /&gt;
 print&lt;br /&gt;
 all&lt;br /&gt;
 to&lt;br /&gt;
 console&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; is a method which is executed for every element present in the list.&lt;br /&gt;
* &amp;lt;code&amp;gt; str.foreach(println)&amp;lt;/code&amp;gt; - Now, this is getting little confusing. Whats &amp;lt;code&amp;gt;println&amp;lt;/code&amp;gt; doing without any arguments? Scala's compiler is intelligent enough to determine that println takes in a single argument and so allows us to skip specifying it.&lt;br /&gt;
===== Transformer =====&lt;br /&gt;
A function which recieves an entity/object and returns an another object(possibly of different type). Infact both predicate and a closure are transformers. To understand more what a transformer is, let us change the case of all the strings in a list.&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
   val list = List(&amp;quot;Scala&amp;quot;, &amp;quot;is&amp;quot;, &amp;quot;the coolest&amp;quot;, &amp;quot;language&amp;quot;, &amp;quot;in the world!&amp;quot;)&lt;br /&gt;
 scala&amp;gt; list.map(_.toUpperCase)&lt;br /&gt;
 res5: List[java.lang.String] = List(SCALA, IS, THE COOLEST, LANGUAGE, IN THE WORLD!)&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;scala.List's&amp;lt;/code&amp;gt; method which takes in a function as a parameter and for every element in the list invokes the function. The result returned by the function is added to a new list.&lt;br /&gt;
* &amp;lt;code&amp;gt;_.toUpperCase&amp;lt;/code&amp;gt; is the transformer function upcasing all the strings.&lt;br /&gt;
&lt;br /&gt;
==== Currying ====&lt;br /&gt;
Excellent tutorial here: [http://bit.ly/16Qs6l]&lt;br /&gt;
Named after its inventor 'Haskell Curry', it is a way of converting a function which takes multiple arguments, into a function with lesser number of arguments. Theory is always boring or we feel so. Let's jump right in and work out an example. Consider a simple function which adds two numbers.&lt;br /&gt;
&lt;br /&gt;
 def add(x:Int)(y:Int) = x + y&lt;br /&gt;
&lt;br /&gt;
Now assume you want to create a function which will add '5' to all its input and return the sum. Can you re-use the code above? Currying works great in these scenarios..&lt;br /&gt;
 val add5ToInput = add(5) _  // the '_' is to tell the compiler to create a curried version of the above function.&lt;br /&gt;
 println(add5ToInput(10))  //surprise surprise - prints 15!&lt;br /&gt;
&lt;br /&gt;
So basically what happens is that compiler creates an intermediate function which always has &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; as the first parameter. So when we say &amp;lt;code&amp;gt;add5ToInput(10)&amp;lt;/code&amp;gt; in reality, it inturn calls &amp;lt;code&amp;gt;add(5)(10)&amp;lt;/code&amp;gt;. So currying basically helps us reduce the total number of parameters which needs to passed to functions and in turn helps re-use code in a functional way!&lt;br /&gt;
&lt;br /&gt;
==== Pattern Matching ====&lt;br /&gt;
also see [http://bit.ly/3o5JA1]&lt;br /&gt;
Yet another unique feature of functional programming languages is the concept of pattern matching. Let us see what pattern matching is all about. Continuing our previous example of Person objects, let us write some code to guess the profession of a person based on his name.(I know it sounds lame..)&lt;br /&gt;
&lt;br /&gt;
 def guessProfession(p: Person) = p.name match {&lt;br /&gt;
        case &amp;quot;Jackie Chan&amp;quot; | &amp;quot;Jet Li&amp;quot; =&amp;gt; &amp;quot;Martial Artist and Actor&amp;quot;&lt;br /&gt;
        case &amp;quot;RajniKanth&amp;quot; | &amp;quot;Kamal Hasan&amp;quot; =&amp;gt; &amp;quot;South Indian Actor&amp;quot;&lt;br /&gt;
        case &amp;quot;Martin Odersky&amp;quot; | &amp;quot;Steve Yegge&amp;quot; =&amp;gt; &amp;quot;Famous Programmer&amp;quot;&lt;br /&gt;
        case _ =&amp;gt; &amp;quot;I don't know %s&amp;quot;.format(p.name)&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 val p = Person(&amp;quot;Kovalan Venkatesan&amp;quot;, 24)&lt;br /&gt;
 val p1 = Person(&amp;quot;Martin Odersky&amp;quot;, 31)&lt;br /&gt;
 println(guessProfession(p)) //prints I don't know Kovalan Venkatesan.&lt;br /&gt;
 println(guessProfession(p1)) //prints &amp;quot;Famous Programmer&amp;quot;&lt;br /&gt;
&lt;br /&gt;
As you may have noticed, the above example is very similar to switch statements in imperative languages such as C, Java. However, it is more powerful and extensible. Why?&lt;br /&gt;
&lt;br /&gt;
# In both C and Java only numeric types can be used in switch statements.(including enums which are simply names mapped onto integers). In Scala you can use almost use anything you want. Infact scala supports custom extractor patterns, whose scope is beyond this text. Please consider visiting scala resources to learn more.&lt;br /&gt;
# The cases do not allow fall through. In Java and C, you have to manually type 'break' after the end of every case. Scala does not have any 'break' nor 'continue' keywords(shocking?)&lt;br /&gt;
&lt;br /&gt;
A purely object oriented person may argue that we could use Visitor Pattern to do the same. That is true, but how many lines of code would you have to write to accomplish the same functionality? Wanna guess?[http://www.artima.com/weblogs/viewpost.jsp?thread=166742]&lt;br /&gt;
&lt;br /&gt;
More explanation of case statements:&lt;br /&gt;
* 'match' is equivalent to switch keyword in C, C++ and Java.&lt;br /&gt;
* 'case' keyword signifies the beginning of the particular case.&lt;br /&gt;
* The '_'(underscore) signifies many things in scala. When we say &amp;quot;case _ =&amp;gt; &amp;quot;, it is equivalent to the 'default' keyword in C/Java.&lt;br /&gt;
&lt;br /&gt;
===== Handling exceptions - Scala way! =====&lt;br /&gt;
In Java, exception matching is considered verbose and ugly. To open and write a file,&lt;br /&gt;
 try {&lt;br /&gt;
    File file = new File(&amp;quot;/tmp/sample.txt&amp;quot;);&lt;br /&gt;
    PrintStream printStream = new PrintStream(file);&lt;br /&gt;
    printStream.write(&amp;quot;something&amp;quot;.getBytes());&lt;br /&gt;
 } catch (FileNotFoundException e) {&lt;br /&gt;
    System.out.println(&amp;quot;Unable to find the file /tmp/sample.txt&amp;quot;);&lt;br /&gt;
    e.printStackTrace();&lt;br /&gt;
 } catch (IOException e) {&lt;br /&gt;
   System.out.println(&amp;quot;Unknown IO error&amp;quot;);&lt;br /&gt;
    e.printStackTrace();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
  try{&lt;br /&gt;
    val file = new File(&amp;quot;/tmp/sample.txt&amp;quot;)&lt;br /&gt;
    val ps = new PrintStream(file)&lt;br /&gt;
    ps.write(&amp;quot;something&amp;quot;.getBytes())&lt;br /&gt;
  }catch{&lt;br /&gt;
    case e: FileNotFoundException =&amp;gt; println(&amp;quot;Unable to find file /tmp/sample.txt&amp;quot;)&lt;br /&gt;
    case IOException =&amp;gt; println(&amp;quot;Unknown IO error&amp;quot;)&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
We have just scratched the surface of Scala, but it has innumerable features which show a beautiful blend of functional and object oriented features. Scala proves we can use them both together.&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
Functional programming integrated with object oriented style leads to:&lt;br /&gt;
&lt;br /&gt;
#better understanding of program behavior as a result of immutability features&lt;br /&gt;
#use of efficient recursive techniques such as tail call optimization&lt;br /&gt;
#Reduction in number of lines of code&lt;br /&gt;
&lt;br /&gt;
By using these techniques we utilize the best of both functional and object oriented techniques.&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
*[1]: http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)&lt;br /&gt;
*[2]: http://www.haskell.org&lt;br /&gt;
*[3]: http://www.scala-lang.org&lt;br /&gt;
*[4]: http://bit.ly/16Qs6l&lt;br /&gt;
*[5]: http://bit.ly/3o5JA1&lt;br /&gt;
*[6]: http://www.artima.com/weblogs/viewpost.jsp?thread=166742&lt;br /&gt;
*[7]: http://lamp.epfl.ch/~odersky/&lt;br /&gt;
*[8]: http://www.scala-lang.org/docu/files/collections-api/collections.html#&lt;br /&gt;
*[9]: http://www.codecommit.com/blog/java/interop-between-java-and-scala&lt;br /&gt;
*[10]: http://www.codecommit.com/blog/scala/what-is-hindley-milner-and-why-is-it-cool&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_az&amp;diff=34488</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1e az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_az&amp;diff=34488"/>
		<updated>2010-09-09T02:23:40Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Mixed paradigm */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Programming Paradigms==&lt;br /&gt;
 &lt;br /&gt;
Every computer program needs a style of writing which specifies how to solve a software engineering problem. This style is represented by the paradigm. Each computer program follows one or more paradigm which differs in representing the elements of a program(such as variables and objects) and the steps needed to compute a task.&lt;br /&gt;
&lt;br /&gt;
Diferent paradigms are:&lt;br /&gt;
&lt;br /&gt;
1. Procedural/imperative paradigms: Assembly, C, C++, Java, C#&lt;br /&gt;
&lt;br /&gt;
2. Object Oriented paradigm : C++, Java, Python, Ruby, Scala, C#&lt;br /&gt;
&lt;br /&gt;
3. Functional Paradigm : Lisp, Haskell, Clojure, Scala, OCaml, Ruby&lt;br /&gt;
&lt;br /&gt;
4. Logic Paradigm: Prolog&lt;br /&gt;
&lt;br /&gt;
==Multi-Paradigm Programming==&lt;br /&gt;
&lt;br /&gt;
Multiparadigm refers to use of a combination of programming paradigms for solving a computer problem. Some languages subscribe strictly to a single paradigm like Assembly and C. Others like Java, C++, Scala and C# employ more than one paradigm. Every paradigm comes with its own strength and weakness and this quite motivates us to take advantage of each paradigm and use it in a manner that best fits the problem at hand.&lt;br /&gt;
&lt;br /&gt;
==Overview of Functional Programming==&lt;br /&gt;
&lt;br /&gt;
Functional programming is derived from lambda calculus which models computations as the evaluation of functions and recursions. More emphasis is laid on application of functions rather than changing the state of variables in a program. Functional programming relies heavily on recursions and it is the only way to iterate instead of loops.&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;How it differs from imperative programming&amp;lt;/b&amp;gt;===&lt;br /&gt;
    &lt;br /&gt;
Imperative programming follows the Von-Neumann architecture and mainly consists of loops and usage of globa states to perform a computation. For example consider the task of computing the sum of numbers from 1 to n. &lt;br /&gt;
&lt;br /&gt;
In imperative style,&lt;br /&gt;
        &lt;br /&gt;
        sum := 0         // global state&lt;br /&gt;
        for i &amp;lt;- 1 to n do&lt;br /&gt;
            sum := sum + i&lt;br /&gt;
            &lt;br /&gt;
In Functional style,&lt;br /&gt;
    &lt;br /&gt;
         func sum(n)        // note that sum is a function and is recursive&lt;br /&gt;
           if  n = 1 then&lt;br /&gt;
               return 1&lt;br /&gt;
           else&lt;br /&gt;
               return n + sum(n-1)&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Pure and impure functional programming&amp;lt;/b&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
Purely functional programming exhibit referential transparency [http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)] which does not involve any global state or I/O changes. If the same functional expression results in the same output value for the same argument x at different stages of execution , then the function is said to be pure which is devoid of any global state change. For example Haskell [http://www.haskell.org] is purely functional.&lt;br /&gt;
&lt;br /&gt;
==Overview of object oriented programming==&lt;br /&gt;
&lt;br /&gt;
Typially programs were written with a procedural view where the program's logic was given utmost importance and it follows a sequential structure. But object oriented techniques really cares about the data or objects we want to mainpulate with rather than the logic of  a program. Every object contains its own state(in the form of variables) and and a number of methods to manipulate the variables.For example consider the class Add:&lt;br /&gt;
           &lt;br /&gt;
           class Add{&lt;br /&gt;
           /* Object variables */&lt;br /&gt;
                  private int sum = 0;&lt;br /&gt;
          /* Object Methods */&lt;br /&gt;
                  public void calculate_sum(int n){&lt;br /&gt;
                      int i=0;&lt;br /&gt;
                      while(i &amp;lt;= n){&lt;br /&gt;
                          sum = sum + i;&lt;br /&gt;
                          i++;&lt;br /&gt;
                      }&lt;br /&gt;
                  }&lt;br /&gt;
            }&lt;br /&gt;
       &lt;br /&gt;
            Add instance1 = new Add();     // instance1 is an object of type Add&lt;br /&gt;
            instance1.calculate_sum(100);&lt;br /&gt;
           &lt;br /&gt;
===&amp;lt;b&amp;gt;Principles of object oriented design&amp;lt;/b&amp;gt;===&lt;br /&gt;
           &lt;br /&gt;
1. &amp;lt;b&amp;gt;Encapsulation:&amp;lt;/b&amp;gt; Encapsulation refers to hiding the internal representation of the object from the outside view. For example algorithm to compute the calculate_sum() method may be hidden, but it will present the user with the expected results.&lt;br /&gt;
&lt;br /&gt;
2. &amp;lt;b&amp;gt;Polymorphism:&amp;lt;/b&amp;gt; Polymorphism means ability to take multiple forms. For example an operation may exhibit different behaviour at different instances. Consider the operator '+', for numbers it will generate the sum, but for strings it will produce a third string which concatenates the input strings.&lt;br /&gt;
&lt;br /&gt;
3. &amp;lt;b&amp;gt;Inheritance:&amp;lt;/b&amp;gt; Inheritance involves base and derived classes with derived class inheriting all the methods and states frm the base class. Inheritance basically forms a hirerachy. For example if shape is an object, then objects square, rectangle, circle all derive the same characteristic as shape does.&lt;br /&gt;
&lt;br /&gt;
==Functional + OOP code==&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Functions as Objects&amp;lt;/b&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
One of the cornerstone of functional and object oriented code is treating functions as objects.It means that we can pass functions as arguments, store it and return them from other functions. This is highly useful in an user interface code where it can be used as call back functions which get called when an event occurs( in event driven programming)&lt;br /&gt;
&lt;br /&gt;
An example in Scala [http://www.scala-lang.org]:&lt;br /&gt;
&lt;br /&gt;
  Object Timer{&lt;br /&gt;
    def  action(callback() : () =&amp;gt; unit){              // callback() is the function passed as an argument&lt;br /&gt;
         while( some condition)                                                            &lt;br /&gt;
         { &lt;br /&gt;
            callback();&lt;br /&gt;
            Thread.sleep(3000)&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
     def event(){                                      //  event is the method which tells what to do &lt;br /&gt;
         /* process the event here&lt;br /&gt;
         &lt;br /&gt;
         */      &lt;br /&gt;
     }&lt;br /&gt;
        &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Another example of functional concept in object oriented design is the concept of blocks especially in Ruby. Blocks are basically nameless functions which can be passed to a function and then that function can invoke the passed in nameless function. This is a common style called higher order function style among languages that handle functions as first class objects. Basically blocks can be designed for loop abstraction and lets the user to design their own way of iterating the values, thereby hiding the implementation details. &lt;br /&gt;
For example if we want to iterate backwards from the end to the beginning without revealing its internal implementations, we could implement the loop logic inside the block and pass it to a method or function.&lt;br /&gt;
&lt;br /&gt;
Here is a Ruby implementation of block&lt;br /&gt;
 &lt;br /&gt;
 def printlist(array,&amp;amp;reverse)&lt;br /&gt;
    array.each do |element|&lt;br /&gt;
       puts &amp;quot;array element: #{element}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    reverse.call(array)   /* Here it prints the reverse implementation of list*/&lt;br /&gt;
    end&lt;br /&gt;
  &lt;br /&gt;
 printlist([1,2,3,4,5,6]) { |array| &lt;br /&gt;
   array.reverse.each do |element| &lt;br /&gt;
     puts &amp;quot;array element: #{element}&amp;quot; &lt;br /&gt;
   end &lt;br /&gt;
 }&lt;br /&gt;
Next we will take a look at some of the features of Scala which supports a blend of functional and object oriented concepts in a concise manner.&lt;br /&gt;
&lt;br /&gt;
= Scala =&lt;br /&gt;
Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages&lt;br /&gt;
&lt;br /&gt;
Scala can be considered a very good blend of the best of functional and object oriented world. It was conceived(in 2001) and implemented by Martin Odersky who was also the author of Sun javac compiler and played a major role in the design and retrofitting of generics into Java. The latest version of Scala is 2.8 and includes a number of performance improvements over previous versions. We shall see more about the features of Scala in the following pages, but for fun, some facts!&lt;br /&gt;
&lt;br /&gt;
=== Features ===&lt;br /&gt;
==== Statically Typed ====&lt;br /&gt;
Scala belongs to the family of languages which do not allow the type of variables once they have been defined. In other words, the type information of each and every variable is encoded in the generated code and the compiler makes sure, you are not doing something funny. For example, lets consider the following example,&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
   irb(main):001:0&amp;gt; str=&amp;quot;Hello World&amp;quot;&lt;br /&gt;
    =&amp;gt; &amp;quot;Hello World&amp;quot;&lt;br /&gt;
    irb(main):002:0&amp;gt; puts str.class&lt;br /&gt;
    String&lt;br /&gt;
    irb(main):003:0&amp;gt; str=123&lt;br /&gt;
    =&amp;gt; 123&lt;br /&gt;
    irb(main):004:0&amp;gt; puts str.class&lt;br /&gt;
    Fixnum&lt;br /&gt;
&lt;br /&gt;
In the above example, first we define 'str' variable to be of type String and then as a number. So basically, no type information is associated with the variable at any point in the program. Hence we can easily reassociate that variable with an object/instance&lt;br /&gt;
of a different type. This is a feature of all dynamically typed languages(like Ruby, Python, Clojure). However most of the statically typed languages such as Scala, Java, C/C++ do not allow you do so. In Scala,&lt;br /&gt;
&lt;br /&gt;
  scala&amp;gt; var str=&amp;quot;Hello World&amp;quot;&lt;br /&gt;
  str: java.lang.String = Hello World&lt;br /&gt;
  scala&amp;gt; str=123&lt;br /&gt;
  &amp;lt;console&amp;gt;:6: error: type mismatch;&lt;br /&gt;
   found   : Int(123)&lt;br /&gt;
   required: java.lang.String&lt;br /&gt;
         str=123&lt;br /&gt;
==== Mixed paradigm ====&lt;br /&gt;
Scala is a strange specimen. It is a complete blend of object oriented and functional programming. How can this happen? Functional world advocates immutability but object oriented world is all about state and how do you mutate it to fit your needs. Scala allows you create both mutable and immutable objects. Consider the following example,&lt;br /&gt;
 val str: String = &amp;quot;Hello World&amp;quot;&lt;br /&gt;
In the first line, I am creating a immutable variable(a misnomer indeed). Roughly translating the above line to Java is below:&lt;br /&gt;
&lt;br /&gt;
 final String str = &amp;quot;Hello World&amp;quot;; //note the extra semi-colon&lt;br /&gt;
Consider the next example below,&lt;br /&gt;
 val list: List[String] = List(&amp;quot;Scala&amp;quot;, &amp;quot;is&amp;quot;, &amp;quot;the coolest&amp;quot;, &amp;quot;language&amp;quot;, &amp;quot;in the world!&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The same code can be written in Java as below&lt;br /&gt;
&lt;br /&gt;
  List&amp;lt;String&amp;gt; mutableList = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
  list.add(&amp;quot;Scala&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;is&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;the coolest&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;language&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;in the world!&amp;quot;);&lt;br /&gt;
  List&amp;lt;String&amp;gt; immutableList = Collections.immutableList(mutableList);&lt;br /&gt;
&lt;br /&gt;
Compared to Java, Scala reduces the total number of lines you need to type and at the same time looks so much simpler and less verbose. Even though the above example shows immutable collection example, scala also provides mutable collection and object types under the package &amp;lt;code&amp;gt;scala.collection.mutable.*&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As it can be observed, even though scala advocates immutability, it does not restrict you from creating mutable objects. So its the best of both worlds. Another part of functional world is the concept of functions as ''''first class members'''' of the language. Some of the common languages like Ruby, Python and JavaScript all incorporate this feature. In the sense you can pass around functions to methods as a parameter. Enough talking;&lt;br /&gt;
&lt;br /&gt;
Let us have a class say &amp;lt;code&amp;gt;'Person'&amp;lt;/code&amp;gt; which has properties, &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; and another variable &amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt;.&lt;br /&gt;
In Java:&lt;br /&gt;
&lt;br /&gt;
 class Person{&lt;br /&gt;
    private String name;&lt;br /&gt;
    private int age;&lt;br /&gt;
    public String getName(){&lt;br /&gt;
        return name;&lt;br /&gt;
    }&lt;br /&gt;
    public void setName(String s){&lt;br /&gt;
        this.name = s;&lt;br /&gt;
    }&lt;br /&gt;
    public int getAge(){&lt;br /&gt;
        return age;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAge(int age){&lt;br /&gt;
        this.age = age;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
 case class Person(name: String, age: Int)&lt;br /&gt;
&lt;br /&gt;
This single line is equivalent to the above java code! So much cooler.. I would not be doing justice if I don't mention the fact that getters for &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt; are generated. Also equals() and hashCode() method are automatically generated for you by the compiler. Isn't this an awesome feature? No wonder Scala is considered a beautiful and high level language. Now assume we have a list of persons and we want to filter out all the persons who are of age 23. How would you do that in Java?&lt;br /&gt;
&lt;br /&gt;
  List&amp;lt;Person&amp;gt; persons; //has a bunch of person objects in it.&lt;br /&gt;
  List&amp;lt;Person&amp;gt; twentyThree = new ArrayList&amp;lt;Person&amp;gt;();&lt;br /&gt;
  for(Person p: persons){&lt;br /&gt;
      if(p.getAge() == 23){&lt;br /&gt;
          twentyThree.add(p);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
&lt;br /&gt;
 val twentyThree: List[Person] = persons.filter( _.age == 23)&lt;br /&gt;
&lt;br /&gt;
That's it! In case you are wondering whats going on, filter is a method present on scala.List class and the code block  _.age == 23 is a function created on the fly and passed onto the filter method. More precisely its called a closure - a functional concept which can be emulated in java only using anonymous inner classes. In case you are wondering how scala does this, scala's compiler transforms our one line function into some anonymous inner classes when generating java byte code but allows developer to define functions in a simple manner.&lt;br /&gt;
&lt;br /&gt;
Infact every function is an object in scala. Scala's standard library has a number of functions which are object themselves.&lt;br /&gt;
&lt;br /&gt;
==== Sophisticated Syntax/Features ====&lt;br /&gt;
When we say sophisticated, its really an awesome feature which is present in other functional languages like Haskell, (O)Caml and Erlang. Consider the following example,&lt;br /&gt;
&lt;br /&gt;
   val name = &amp;quot;Jackie Chan&amp;quot;&lt;br /&gt;
   println(name.getClass) //prints out String.&lt;br /&gt;
&lt;br /&gt;
Previously we saw that scala is a statically typed language and yet in the above example we don't specify what type is the variable name. In Java, you would type in the following:&lt;br /&gt;
&lt;br /&gt;
 String name = &amp;quot;Jackie Chan&amp;quot;&lt;br /&gt;
&lt;br /&gt;
So basically in Java you specify what type the name variable is. Scala compiler is very smart in the manner that it can automatically detect the type of the variable even if you don't specify it. Consider the following example,&lt;br /&gt;
&lt;br /&gt;
 case class Person(name: String, age: Int)&lt;br /&gt;
 val persons = List(&lt;br /&gt;
                        Person(&amp;quot;Jackie Chan&amp;quot;, 51), Person(&amp;quot;Jet Li&amp;quot;, 41),&lt;br /&gt;
                        Person(&amp;quot;RajniKanth&amp;quot;, 51), Person(&amp;quot;Kamal Hasan&amp;quot;, 44),&lt;br /&gt;
                        Person(&amp;quot;Martin Odersky&amp;quot;, 18), Person(&amp;quot;Steve Yegge&amp;quot;, 23))&lt;br /&gt;
 //notice that you don't have to use 'new' keyword to create new instances. This is taken care of by the companion objects.&lt;br /&gt;
&lt;br /&gt;
The above lines of code create a list with 6 different persons(An immutable list)&lt;br /&gt;
&lt;br /&gt;
Now, as in ruby scala supports the concepts of closure. Let's see what it is.&lt;br /&gt;
&lt;br /&gt;
 val twentyThree: List[Person] = persons.filter( { p: Person =&amp;gt; p.age == 3})&lt;br /&gt;
&lt;br /&gt;
In the above set of lines, the code within the '{ }' brackets is a function's body. Basically what the compiler did was to create a function which takes a single argument and returns either true or false. Now that's called a high level language. Ready for more?&lt;br /&gt;
&lt;br /&gt;
==== A Scalable Language ====&lt;br /&gt;
In almost all programming languages, there is a necessity to profile the running time of the procedures and tune its performance. The old way of doing things in Java would be:&lt;br /&gt;
&lt;br /&gt;
 public class Main{&lt;br /&gt;
 public static int factorial(int x){&lt;br /&gt;
   if(x == 0 || x == 1) return 1&lt;br /&gt;
   else return x * factorial(x-1)&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String [] args){&lt;br /&gt;
    long beforeTime = System.currentTimeMillis();&lt;br /&gt;
    int result = factorial(5);&lt;br /&gt;
    System.out.println(&amp;quot;Total time taken to execute factorial(5) is &amp;quot; + (System.currentTimeMillis() - result)+&amp;quot;ms&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;factorial(5) = &amp;quot; + result);&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
However, if you want to find out the execution time of some other method, you would have to type in all the above lines again. So much for code re-use! Lets see how its done in Scala. Hold on to your seats...&lt;br /&gt;
&lt;br /&gt;
 object Main{&lt;br /&gt;
  def main(args: Array[String]){&lt;br /&gt;
    def factorial(x: Int):Int = x match{&lt;br /&gt;
      case 0 | 1 =&amp;gt; 1&lt;br /&gt;
      case _ =&amp;gt; x * factorial(x-1)&lt;br /&gt;
  }&lt;br /&gt;
  import System.currentTimeMillis&lt;br /&gt;
  def timeIt(msg: String = &amp;quot;Time to execute is: &amp;quot;)(func: =&amp;gt; Unit){&lt;br /&gt;
    val before = currentTimeMillis&lt;br /&gt;
    func&lt;br /&gt;
    println(msg + (currentTimeMillis - before) + &amp;quot;ms&amp;quot;)&lt;br /&gt;
  }&lt;br /&gt;
  timeIt(&amp;quot;Total time taken to execute factorial(5) is &amp;quot;){&lt;br /&gt;
      val result = factorial(5)&lt;br /&gt;
      println(&amp;quot;factorial(5) = &amp;quot; + result)&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 //would print: factorial(5) = 120&lt;br /&gt;
 // Total time taken to execute factorial(5) is 1ms&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That's not right! The function &amp;lt;code&amp;gt;timeIt&amp;lt;/code&amp;gt; looks like a feature inbuilt into the language. Turn your disbelief into amazement;save the above code into a separate file and run it. Infact using this feature, it is possible to build powerful DSL languages which can run at full speed on JVM.&lt;br /&gt;
&lt;br /&gt;
Ruby allows you to add new methods to objects. This feature is often called as '''Monkey Patching''' because you are splitting open the class and adding/overriding methods (in)|(to) the class. Often this leads to unexpected behavior at runtime because, other libraries including into build path may depend on some methods which you changed. Scala provides yet another feature called implicits.&lt;br /&gt;
&lt;br /&gt;
In Java, String class is final so there is no way you can add new methods to it. But with implicits you can do more! Suppose you have some text in memory and you know it represents a number. But in Java the usual way to convert string to int is to do a Integer.parseInt(string) which seems so unnecessary. Let us make our lives easier...&lt;br /&gt;
&lt;br /&gt;
  implicit def strToInt(str: String) = new {&lt;br /&gt;
    def asInt = Integer.parseInt(str)&lt;br /&gt;
  }&lt;br /&gt;
  val str = &amp;quot;1234&amp;quot;&lt;br /&gt;
  println(str.asInt * 3) //prints out 3702&lt;br /&gt;
&lt;br /&gt;
The above code gives us an impression as if String class has gained new 'asInt' method. Can static object oriented languages do this? No! Infact scala cheats by calling our implicits to convert it into a different object as and when needed.&lt;br /&gt;
&lt;br /&gt;
The above functionalities are purely object oriented, yet they are safe. They don't pollute the global namespace unlike Ruby or Python&lt;br /&gt;
&lt;br /&gt;
==== Broad Classification of Functions ====&lt;br /&gt;
===== Predicate =====&lt;br /&gt;
It is a function which receives an object as argument and either returns true or false. They are useful in a number of situations. Suppose you had a list of integers and you wanted to filter out all odd numbers you could apply the predicate on every element in the list and&lt;br /&gt;
* If the predicate returned true, add it to the new list&lt;br /&gt;
* else ignore the current entry and try next.&lt;br /&gt;
&lt;br /&gt;
 scala&amp;gt; val numbers = 0 to 20&lt;br /&gt;
 numbers: scala.collection.immutable.Range.Inclusive with  scala.collection.immutable.Range.ByOne = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)&lt;br /&gt;
 scala&amp;gt; val evenNumbers = numbers.filter( _ % 2 == 0)&lt;br /&gt;
 evenNumbers: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20)&lt;br /&gt;
&lt;br /&gt;
===== Closure =====&lt;br /&gt;
A function which does some operation on an object but does not return any result. In otherwords, the return type of closure is void. Say we have a list of strings and we need to print them all to the console; we can do it the following way:&lt;br /&gt;
&lt;br /&gt;
 scala&amp;gt; val strs = List(&amp;quot;print&amp;quot;, &amp;quot;all&amp;quot;, &amp;quot;to&amp;quot;, &amp;quot;console&amp;quot;)&lt;br /&gt;
 strs: List[java.lang.String] = List(print, all, to, console)&lt;br /&gt;
 scala&amp;gt; strs.foreach(println) //println == System.out.println&lt;br /&gt;
 print&lt;br /&gt;
 all&lt;br /&gt;
 to&lt;br /&gt;
 console&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; is a method which is executed for every element present in the list.&lt;br /&gt;
* &amp;lt;code&amp;gt; str.foreach(println)&amp;lt;/code&amp;gt; - Now, this is getting little confusing. Whats &amp;lt;code&amp;gt;println&amp;lt;/code&amp;gt; doing without any arguments? Scala's compiler is intelligent enough to determine that println takes in a single argument and so allows us to skip specifying it.&lt;br /&gt;
===== Transformer =====&lt;br /&gt;
A function which recieves an entity/object and returns an another object(possibly of different type). Infact both predicate and a closure are transformers. To understand more what a transformer is, let us change the case of all the strings in a list.&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
   val list = List(&amp;quot;Scala&amp;quot;, &amp;quot;is&amp;quot;, &amp;quot;the coolest&amp;quot;, &amp;quot;language&amp;quot;, &amp;quot;in the world!&amp;quot;)&lt;br /&gt;
 scala&amp;gt; list.map(_.toUpperCase)&lt;br /&gt;
 res5: List[java.lang.String] = List(SCALA, IS, THE COOLEST, LANGUAGE, IN THE WORLD!)&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;scala.List's&amp;lt;/code&amp;gt; method which takes in a function as a parameter and for every element in the list invokes the function. The result returned by the function is added to a new list.&lt;br /&gt;
* &amp;lt;code&amp;gt;_.toUpperCase&amp;lt;/code&amp;gt; is the transformer function upcasing all the strings.&lt;br /&gt;
&lt;br /&gt;
==== Currying ====&lt;br /&gt;
Excellent tutorial here: [http://bit.ly/16Qs6l]&lt;br /&gt;
Named after its inventor 'Haskell Curry', it is a way of converting a function which takes multiple arguments, into a function with lesser number of arguments. Theory is always boring or we feel so. Let's jump right in and work out an example. Consider a simple function which adds two numbers.&lt;br /&gt;
&lt;br /&gt;
 def add(x:Int)(y:Int) = x + y&lt;br /&gt;
&lt;br /&gt;
Now assume you want to create a function which will add '5' to all its input and return the sum. Can you re-use the code above? Currying works great in these scenarios..&lt;br /&gt;
 val add5ToInput = add(5) _  // the '_' is to tell the compiler to create a curried version of the above function.&lt;br /&gt;
 println(add5ToInput(10))  //surprise surprise - prints 15!&lt;br /&gt;
&lt;br /&gt;
So basically what happens is that compiler creates an intermediate function which always has &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; as the first parameter. So when we say &amp;lt;code&amp;gt;add5ToInput(10)&amp;lt;/code&amp;gt; in reality, it inturn calls &amp;lt;code&amp;gt;add(5)(10)&amp;lt;/code&amp;gt;. So currying basically helps us reduce the total number of parameters which needs to passed to functions and in turn helps re-use code in a functional way!&lt;br /&gt;
&lt;br /&gt;
==== Pattern Matching ====&lt;br /&gt;
also see [http://bit.ly/3o5JA1]&lt;br /&gt;
Yet another unique feature of functional programming languages is the concept of pattern matching. Let us see what pattern matching is all about. Continuing our previous example of Person objects, let us write some code to guess the profession of a person based on his name.(I know it sounds lame..)&lt;br /&gt;
&lt;br /&gt;
 def guessProfession(p: Person) = p.name match {&lt;br /&gt;
        case &amp;quot;Jackie Chan&amp;quot; | &amp;quot;Jet Li&amp;quot; =&amp;gt; &amp;quot;Martial Artist and Actor&amp;quot;&lt;br /&gt;
        case &amp;quot;RajniKanth&amp;quot; | &amp;quot;Kamal Hasan&amp;quot; =&amp;gt; &amp;quot;South Indian Actor&amp;quot;&lt;br /&gt;
        case &amp;quot;Martin Odersky&amp;quot; | &amp;quot;Steve Yegge&amp;quot; =&amp;gt; &amp;quot;Famous Programmer&amp;quot;&lt;br /&gt;
        case _ =&amp;gt; &amp;quot;I don't know %s&amp;quot;.format(p.name)&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 val p = Person(&amp;quot;Kovalan Venkatesan&amp;quot;, 24)&lt;br /&gt;
 val p1 = Person(&amp;quot;Martin Odersky&amp;quot;, 31)&lt;br /&gt;
 println(guessProfession(p)) //prints I don't know Kovalan Venkatesan.&lt;br /&gt;
 println(guessProfession(p1)) //prints &amp;quot;Famous Programmer&amp;quot;&lt;br /&gt;
&lt;br /&gt;
As you may have noticed, the above example is very similar to switch statements in imperative languages such as C, Java. However, it is more powerful and extensible. Why?&lt;br /&gt;
&lt;br /&gt;
# In both C and Java only numeric types can be used in switch statements.(including enums which are simply names mapped onto integers). In Scala you can use almost use anything you want. Infact scala supports custom extractor patterns, whose scope is beyond this text. Please consider visiting scala resources to learn more.&lt;br /&gt;
# The cases do not allow fall through. In Java and C, you have to manually type 'break' after the end of every case. Scala does not have any 'break' nor 'continue' keywords(shocking?)&lt;br /&gt;
&lt;br /&gt;
A purely object oriented person may argue that we could use Visitor Pattern to do the same. That is true, but how many lines of code would you have to write to accomplish the same functionality? Wanna guess?[http://www.artima.com/weblogs/viewpost.jsp?thread=166742]&lt;br /&gt;
&lt;br /&gt;
More explanation of case statements:&lt;br /&gt;
* 'match' is equivalent to switch keyword in C, C++ and Java.&lt;br /&gt;
* 'case' keyword signifies the beginning of the particular case.&lt;br /&gt;
* The '_'(underscore) signifies many things in scala. When we say &amp;quot;case _ =&amp;gt; &amp;quot;, it is equivalent to the 'default' keyword in C/Java.&lt;br /&gt;
&lt;br /&gt;
===== Handling exceptions - Scala way! =====&lt;br /&gt;
In Java, exception matching is considered verbose and ugly. To open and write a file,&lt;br /&gt;
 try {&lt;br /&gt;
    File file = new File(&amp;quot;/tmp/sample.txt&amp;quot;);&lt;br /&gt;
    PrintStream printStream = new PrintStream(file);&lt;br /&gt;
    printStream.write(&amp;quot;something&amp;quot;.getBytes());&lt;br /&gt;
 } catch (FileNotFoundException e) {&lt;br /&gt;
    System.out.println(&amp;quot;Unable to find the file /tmp/sample.txt&amp;quot;);&lt;br /&gt;
    e.printStackTrace();&lt;br /&gt;
 } catch (IOException e) {&lt;br /&gt;
   System.out.println(&amp;quot;Unknown IO error&amp;quot;);&lt;br /&gt;
    e.printStackTrace();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
  try{&lt;br /&gt;
    val file = new File(&amp;quot;/tmp/sample.txt&amp;quot;)&lt;br /&gt;
    val ps = new PrintStream(file)&lt;br /&gt;
    ps.write(&amp;quot;something&amp;quot;.getBytes())&lt;br /&gt;
  }catch{&lt;br /&gt;
    case e: FileNotFoundException =&amp;gt; println(&amp;quot;Unable to find file /tmp/sample.txt&amp;quot;)&lt;br /&gt;
    case IOException =&amp;gt; println(&amp;quot;Unknown IO error&amp;quot;)&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
We have just scratched the surface of Scala, but it has innumerable features which show a beautiful blend of functional and object oriented features. Scala proves we can use them both together.&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
*[1]: http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)&lt;br /&gt;
*[2]: http://www.haskell.org&lt;br /&gt;
*[3]: http://www.scala-lang.org&lt;br /&gt;
*[4]: http://bit.ly/16Qs6l&lt;br /&gt;
*[5]: http://bit.ly/3o5JA1&lt;br /&gt;
*[6]: http://www.artima.com/weblogs/viewpost.jsp?thread=166742&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_az&amp;diff=34469</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1e az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_az&amp;diff=34469"/>
		<updated>2010-09-09T02:10:11Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* Mixed paradigm */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Programming Paradigms==&lt;br /&gt;
 &lt;br /&gt;
Every computer program needs a style of writing which specifies how to solve a software engineering problem. This style is represented by the paradigm. Each computer program follows one or more paradigm which differs in representing the elements of a program(such as variables and objects) and the steps needed to compute a task.&lt;br /&gt;
&lt;br /&gt;
Diferent paradigms are:&lt;br /&gt;
&lt;br /&gt;
1. Procedural/imperative paradigms: Assembly, C, C++, Java, C#&lt;br /&gt;
&lt;br /&gt;
2. Object Oriented paradigm : C++, Java, Python, Ruby, Scala, C#&lt;br /&gt;
&lt;br /&gt;
3. Functional Paradigm : Lisp, Haskell, Clojure, Scala, OCaml, Ruby&lt;br /&gt;
&lt;br /&gt;
4. Logic Paradigm: Prolog&lt;br /&gt;
&lt;br /&gt;
==Multi-Paradigm Programming==&lt;br /&gt;
&lt;br /&gt;
Multiparadigm refers to use of a combination of programming paradigms for solving a computer problem. Some languages subscribe strictly to a single paradigm like Assembly and C. Others like Java, C++, Scala and C# employ more than one paradigm. Every paradigm comes with its own strength and weakness and this quite motivates us to take advantage of each paradigm and use it in a manner that best fits the problem at hand.&lt;br /&gt;
&lt;br /&gt;
==Overview of Functional Programming==&lt;br /&gt;
&lt;br /&gt;
Functional programming is derived from lambda calculus which models computations as the evaluation of functions and recursions. More emphasis is laid on application of functions rather than changing the state of variables in a program. Functional programming relies heavily on recursions and it is the only way to iterate instead of loops.&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;How it differs from imperative programming&amp;lt;/b&amp;gt;===&lt;br /&gt;
    &lt;br /&gt;
Imperative programming follows the Von-Neumann architecture and mainly consists of loops and usage of globa states to perform a computation. For example consider the task of computing the sum of numbers from 1 to n. &lt;br /&gt;
&lt;br /&gt;
In imperative style,&lt;br /&gt;
        &lt;br /&gt;
        sum := 0         // global state&lt;br /&gt;
        for i &amp;lt;- 1 to n do&lt;br /&gt;
            sum := sum + i&lt;br /&gt;
            &lt;br /&gt;
In Functional style,&lt;br /&gt;
    &lt;br /&gt;
         func sum(n)        // note that sum is a function and is recursive&lt;br /&gt;
           if  n = 1 then&lt;br /&gt;
               return 1&lt;br /&gt;
           else&lt;br /&gt;
               return n + sum(n-1)&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Pure and impure functional programming&amp;lt;/b&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
Purely functional programming exhibit referential transparency [http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)] which does not involve any global state or I/O changes. If the same functional expression results in the same output value for the same argument x at different stages of execution , then the function is said to be pure which is devoid of any global state change. For example Haskell [http://www.haskell.org] is purely functional.&lt;br /&gt;
&lt;br /&gt;
==Overview of object oriented programming==&lt;br /&gt;
&lt;br /&gt;
Typially programs were written with a procedural view where the program's logic was given utmost importance and it follows a sequential structure. But object oriented techniques really cares about the data or objects we want to mainpulate with rather than the logic of  a program. Every object contains its own state(in the form of variables) and and a number of methods to manipulate the variables.For example consider the class Add:&lt;br /&gt;
           &lt;br /&gt;
           class Add{&lt;br /&gt;
           /* Object variables */&lt;br /&gt;
                  private int sum = 0;&lt;br /&gt;
          /* Object Methods */&lt;br /&gt;
                  public void calculate_sum(int n){&lt;br /&gt;
                      int i=0;&lt;br /&gt;
                      while(i &amp;lt;= n){&lt;br /&gt;
                          sum = sum + i;&lt;br /&gt;
                          i++;&lt;br /&gt;
                      }&lt;br /&gt;
                  }&lt;br /&gt;
            }&lt;br /&gt;
       &lt;br /&gt;
            Add instance1 = new Add();     // instance1 is an object of type Add&lt;br /&gt;
            instance1.calculate_sum(100);&lt;br /&gt;
           &lt;br /&gt;
===&amp;lt;b&amp;gt;Principles of object oriented design&amp;lt;/b&amp;gt;===&lt;br /&gt;
           &lt;br /&gt;
1. &amp;lt;b&amp;gt;Encapsulation:&amp;lt;/b&amp;gt; Encapsulation refers to hiding the internal representation of the object from the outside view. For example algorithm to compute the calculate_sum() method may be hidden, but it will present the user with the expected results.&lt;br /&gt;
&lt;br /&gt;
2. &amp;lt;b&amp;gt;Polymorphism:&amp;lt;/b&amp;gt; Polymorphism means ability to take multiple forms. For example an operation may exhibit different behaviour at different instances. Consider the operator '+', for numbers it will generate the sum, but for strings it will produce a third string which concatenates the input strings.&lt;br /&gt;
&lt;br /&gt;
3. &amp;lt;b&amp;gt;Inheritance:&amp;lt;/b&amp;gt; Inheritance involves base and derived classes with derived class inheriting all the methods and states frm the base class. Inheritance basically forms a hirerachy. For example if shape is an object, then objects square, rectangle, circle all derive the same characteristic as shape does.&lt;br /&gt;
&lt;br /&gt;
==Functional + OOP code==&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Functions as Objects&amp;lt;/b&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
One of the cornerstone of functional and object oriented code is treating functions as objects.It means that we can pass functions as arguments, store it and return them from other functions. This is highly useful in an user interface code where it can be used as call back functions which get called when an event occurs( in event driven programming)&lt;br /&gt;
&lt;br /&gt;
An example in Scala [http://www.scala-lang.org]:&lt;br /&gt;
&lt;br /&gt;
  Object Timer{&lt;br /&gt;
    def  action(callback() : () =&amp;gt; unit){              // callback() is the function passed as an argument&lt;br /&gt;
         while( some condition)                                                            &lt;br /&gt;
         { &lt;br /&gt;
            callback();&lt;br /&gt;
            Thread.sleep(3000)&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
     def event(){                                      //  event is the method which tells what to do &lt;br /&gt;
         /* process the event here&lt;br /&gt;
         &lt;br /&gt;
         */      &lt;br /&gt;
     }&lt;br /&gt;
        &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Another example of functional concept in object oriented design is the concept of blocks especially in Ruby. Blocks are basically nameless functions which can be passed to a function and then that function can invoke the passed in nameless function. This is a common style called higher order function style among languages that handle functions as first class objects. Basically blocks can be designed for loop abstraction and lets the user to design their own way of iterating the values, thereby hiding the implementation details. &lt;br /&gt;
For example if we want to iterate backwards from the end to the beginning without revealing its internal implementations, we could implement the loop logic inside the block and pass it to a method or function.&lt;br /&gt;
&lt;br /&gt;
Here is a Ruby implementation of block&lt;br /&gt;
 &lt;br /&gt;
 def printlist(array,&amp;amp;reverse)&lt;br /&gt;
    array.each do |element|&lt;br /&gt;
       puts &amp;quot;array element: #{element}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    reverse.call(array)   /* Here it prints the reverse implementation of list*/&lt;br /&gt;
    end&lt;br /&gt;
  &lt;br /&gt;
 printlist([1,2,3,4,5,6]) { |array| &lt;br /&gt;
   array.reverse.each do |element| &lt;br /&gt;
     puts &amp;quot;array element: #{element}&amp;quot; &lt;br /&gt;
   end &lt;br /&gt;
 }&lt;br /&gt;
Next we will take a look at some of the features of Scala which supports a blend of functional and object oriented concepts in a concise manner.&lt;br /&gt;
&lt;br /&gt;
= Scala =&lt;br /&gt;
Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages&lt;br /&gt;
&lt;br /&gt;
Scala can be considered a very good blend of the best of functional and object oriented world. It was conceived(in 2001) and implemented by Martin Odersky who was also the author of Sun javac compiler and played a major role in the design and retrofitting of generics into Java. The latest version of Scala is 2.8 and includes a number of performance improvements over previous versions. We shall see more about the features of Scala in the following pages, but for fun, some facts!&lt;br /&gt;
&lt;br /&gt;
=== Features ===&lt;br /&gt;
==== Statically Typed ====&lt;br /&gt;
Scala belongs to the family of languages which do not allow the type of variables once they have been defined. In other words, the type information of each and every variable is encoded in the generated code and the compiler makes sure, you are not doing something funny. For example, lets consider the following example,&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
   irb(main):001:0&amp;gt; str=&amp;quot;Hello World&amp;quot;&lt;br /&gt;
    =&amp;gt; &amp;quot;Hello World&amp;quot;&lt;br /&gt;
    irb(main):002:0&amp;gt; puts str.class&lt;br /&gt;
    String&lt;br /&gt;
    irb(main):003:0&amp;gt; str=123&lt;br /&gt;
    =&amp;gt; 123&lt;br /&gt;
    irb(main):004:0&amp;gt; puts str.class&lt;br /&gt;
    Fixnum&lt;br /&gt;
&lt;br /&gt;
In the above example, first we define 'str' variable to be of type String and then as a number. So basically, no type information is associated with the variable at any point in the program. Hence we can easily reassociate that variable with an object/instance&lt;br /&gt;
of a different type. This is a feature of all dynamically typed languages(like Ruby, Python, Clojure). However most of the statically typed languages such as Scala, Java, C/C++ do not allow you do so. In Scala,&lt;br /&gt;
&lt;br /&gt;
  scala&amp;gt; var str=&amp;quot;Hello World&amp;quot;&lt;br /&gt;
  str: java.lang.String = Hello World&lt;br /&gt;
  scala&amp;gt; str=123&lt;br /&gt;
  &amp;lt;console&amp;gt;:6: error: type mismatch;&lt;br /&gt;
   found   : Int(123)&lt;br /&gt;
   required: java.lang.String&lt;br /&gt;
         str=123&lt;br /&gt;
==== Mixed paradigm ====&lt;br /&gt;
Scala is a strange specimen. It is a complete blend of object oriented and functional programming. How can this happen? Functional world advocates immutability but object oriented world is all about state and how do you mutate it to fit your needs. Scala allows you create both mutable and immutable objects. Consider the following example,&lt;br /&gt;
 val str: String = &amp;quot;Hello World&amp;quot;&lt;br /&gt;
In the first line, I am creating a immutable variable(a misnomer indeed). Roughly translating the above line to Java is below:&lt;br /&gt;
&lt;br /&gt;
 final String str = &amp;quot;Hello World&amp;quot;; //note the extra semi-colon&lt;br /&gt;
Consider the next example below,&lt;br /&gt;
 val list: List[String] = List(&amp;quot;Scala&amp;quot;, &amp;quot;is&amp;quot;, &amp;quot;the coolest&amp;quot;, &amp;quot;language&amp;quot;, &amp;quot;in the world!&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The same code can be written in Java as below&lt;br /&gt;
&lt;br /&gt;
  List&amp;lt;String&amp;gt; mutableList = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
  list.add(&amp;quot;Scala&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;is&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;the coolest&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;language&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;in the world!&amp;quot;);&lt;br /&gt;
  List&amp;lt;String&amp;gt; immutableList = Collections.immutableList(mutableList);&lt;br /&gt;
&lt;br /&gt;
Compared to Java, Scala reduces the total number of lines you need to type and at the same time looks so much simpler and less verbose. Even though the above example shows immutable collection example, scala also provides mutable collection and object types under the package &amp;lt;code&amp;gt;scala.collection.mutable.*&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As it can be observed, even though scala advocates immutability, it does not restrict you from creating mutable objects. So its the best of both worlds. Another part of functional world is the concept of functions as ''''first class members'''' of the language. Some of the common languages like Ruby, Python and JavaScript all incorporate this feature. In the sense you can pass around functions to methods as a parameter. Enough talking;&lt;br /&gt;
&lt;br /&gt;
Let us have a class say &amp;lt;code&amp;gt;'Person'&amp;lt;/code&amp;gt; which has properties, &amp;lt;code&amp;gt;name&amp;lt;/code&amp;gt; and another variable &amp;lt;code&amp;gt;age&amp;lt;/code&amp;gt;.&lt;br /&gt;
In Java:&lt;br /&gt;
&lt;br /&gt;
 class Person{&lt;br /&gt;
    private String name;&lt;br /&gt;
    private int age;&lt;br /&gt;
    public String getName(){&lt;br /&gt;
        return name;&lt;br /&gt;
    }&lt;br /&gt;
    public void setName(String s){&lt;br /&gt;
        this.name = s;&lt;br /&gt;
    }&lt;br /&gt;
    public int getAge(){&lt;br /&gt;
        return age;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAge(int age){&lt;br /&gt;
        this.age = age;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
 case class Person(name: String, age: Int)&lt;br /&gt;
&lt;br /&gt;
This single line is equivalent to the above java code! So much cooler.. I would not be doing justice if I don't mention the fact that both equals() and hashCode() method are automatically generated for you by the compiler. Isn't this an awesome feature? No wonder Scala is considered a beautiful and high level language. Now assume we have a list of persons and we want to filter out all the persons who are of age 23. How would you do that in Java?&lt;br /&gt;
&lt;br /&gt;
  List&amp;lt;Person&amp;gt; persons; //has a bunch of person objects in it.&lt;br /&gt;
  List&amp;lt;Person&amp;gt; twentyThree = new ArrayList&amp;lt;Person&amp;gt;();&lt;br /&gt;
  for(Person p: persons){&lt;br /&gt;
      if(p.getAge() == 23){&lt;br /&gt;
          twentyThree.add(p);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
&lt;br /&gt;
 val twentyThree: List[Person] = persons.filter( _.age == 23)&lt;br /&gt;
&lt;br /&gt;
That's it! In case you are wondering whats going on, filter is a method present on scala.List class and the code block  _.age == 23 is a function created on the fly and passed onto the filter method. More precisely its called a closure - a functional concept which can be emulated in java only using anonymous inner classes. In case you are wondering how scala does this, scala's compiler transforms our one line function into some anonymous inner classes when generating java byte code but allows developer to define functions in a simple manner.&lt;br /&gt;
&lt;br /&gt;
Infact every function is an object in scala. Scala's standard library has a number of functions which are object themselves.&lt;br /&gt;
&lt;br /&gt;
==== Sophisticated Syntax/Features ====&lt;br /&gt;
When we say sophisticated, its really an awesome feature which is present in other functional languages like Haskell, (O)Caml and Erlang. Consider the following example,&lt;br /&gt;
&lt;br /&gt;
   val name = &amp;quot;Jackie Chan&amp;quot;&lt;br /&gt;
   println(name.getClass) //prints out String.&lt;br /&gt;
&lt;br /&gt;
Previously we saw that scala is a statically typed language and yet in the above example we don't specify what type is the variable name. In Java, you would type in the following:&lt;br /&gt;
&lt;br /&gt;
 String name = &amp;quot;Jackie Chan&amp;quot;&lt;br /&gt;
&lt;br /&gt;
So basically in Java you specify what type the name variable is. Scala compiler is very smart in the manner that it can automatically detect the type of the variable even if you don't specify it. Consider the following example,&lt;br /&gt;
&lt;br /&gt;
 case class Person(name: String, age: Int)&lt;br /&gt;
 val persons = List(&lt;br /&gt;
                        Person(&amp;quot;Jackie Chan&amp;quot;, 51), Person(&amp;quot;Jet Li&amp;quot;, 41),&lt;br /&gt;
                        Person(&amp;quot;RajniKanth&amp;quot;, 51), Person(&amp;quot;Kamal Hasan&amp;quot;, 44),&lt;br /&gt;
                        Person(&amp;quot;Martin Odersky&amp;quot;, 18), Person(&amp;quot;Steve Yegge&amp;quot;, 23))&lt;br /&gt;
 //notice that you don't have to use 'new' keyword to create new instances. This is taken care of by the companion objects.&lt;br /&gt;
&lt;br /&gt;
The above lines of code create a list with 6 different persons(An immutable list)&lt;br /&gt;
&lt;br /&gt;
Now, as in ruby scala supports the concepts of closure. Let's see what it is.&lt;br /&gt;
&lt;br /&gt;
 val twentyThree: List[Person] = persons.filter( { p: Person =&amp;gt; p.age == 3})&lt;br /&gt;
&lt;br /&gt;
In the above set of lines, the code within the '{ }' brackets is a function's body. Basically what the compiler did was to create a function which takes a single argument and returns either true or false. Now that's called a high level language. Ready for more?&lt;br /&gt;
&lt;br /&gt;
==== A Scalable Language ====&lt;br /&gt;
In almost all programming languages, there is a necessity to profile the running time of the procedures and tune its performance. The old way of doing things in Java would be:&lt;br /&gt;
&lt;br /&gt;
 public class Main{&lt;br /&gt;
 public static int factorial(int x){&lt;br /&gt;
   if(x == 0 || x == 1) return 1&lt;br /&gt;
   else return x * factorial(x-1)&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String [] args){&lt;br /&gt;
    long beforeTime = System.currentTimeMillis();&lt;br /&gt;
    int result = factorial(5);&lt;br /&gt;
    System.out.println(&amp;quot;Total time taken to execute factorial(5) is &amp;quot; + (System.currentTimeMillis() - result)+&amp;quot;ms&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;factorial(5) = &amp;quot; + result);&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
However, if you want to find out the execution time of some other method, you would have to type in all the above lines again. So much for code re-use! Lets see how its done in Scala. Hold on to your seats...&lt;br /&gt;
&lt;br /&gt;
 object Main{&lt;br /&gt;
  def main(args: Array[String]){&lt;br /&gt;
    def factorial(x: Int):Int = x match{&lt;br /&gt;
      case 0 | 1 =&amp;gt; 1&lt;br /&gt;
      case _ =&amp;gt; x * factorial(x-1)&lt;br /&gt;
  }&lt;br /&gt;
  import System.currentTimeMillis&lt;br /&gt;
  def timeIt(msg: String = &amp;quot;Time to execute is: &amp;quot;)(func: =&amp;gt; Unit){&lt;br /&gt;
    val before = currentTimeMillis&lt;br /&gt;
    func&lt;br /&gt;
    println(msg + (currentTimeMillis - before) + &amp;quot;ms&amp;quot;)&lt;br /&gt;
  }&lt;br /&gt;
  timeIt(&amp;quot;Total time taken to execute factorial(5) is &amp;quot;){&lt;br /&gt;
      val result = factorial(5)&lt;br /&gt;
      println(&amp;quot;factorial(5) = &amp;quot; + result)&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 //would print: factorial(5) = 120&lt;br /&gt;
 // Total time taken to execute factorial(5) is 1ms&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That's not right! The function &amp;lt;code&amp;gt;timeIt&amp;lt;/code&amp;gt; looks like a feature inbuilt into the language. Turn your disbelief into amazement;save the above code into a separate file and run it. Infact using this feature, it is possible to build powerful DSL languages which can run at full speed on JVM.&lt;br /&gt;
&lt;br /&gt;
Ruby allows you to add new methods to objects. This feature is often called as '''Monkey Patching''' because you are splitting open the class and adding/overriding methods (in)|(to) the class. Often this leads to unexpected behavior at runtime because, other libraries including into build path may depend on some methods which you changed. Scala provides yet another feature called implicits.&lt;br /&gt;
&lt;br /&gt;
In Java, String class is final so there is no way you can add new methods to it. But with implicits you can do more! Suppose you have some text in memory and you know it represents a number. But in Java the usual way to convert string to int is to do a Integer.parseInt(string) which seems so unnecessary. Let us make our lives easier...&lt;br /&gt;
&lt;br /&gt;
  implicit def strToInt(str: String) = new {&lt;br /&gt;
    def asInt = Integer.parseInt(str)&lt;br /&gt;
  }&lt;br /&gt;
  val str = &amp;quot;1234&amp;quot;&lt;br /&gt;
  println(str.asInt * 3) //prints out 3702&lt;br /&gt;
&lt;br /&gt;
The above code gives us an impression as if String class has gained new 'asInt' method. Can static object oriented languages do this? No! Infact scala cheats by calling our implicits to convert it into a different object as and when needed.&lt;br /&gt;
&lt;br /&gt;
The above functionalities are purely object oriented, yet they are safe. They don't pollute the global namespace unlike Ruby or Python&lt;br /&gt;
&lt;br /&gt;
==== Broad Classification of Functions ====&lt;br /&gt;
===== Predicate =====&lt;br /&gt;
It is a function which receives an object as argument and either returns true or false. They are useful in a number of situations. Suppose you had a list of integers and you wanted to filter out all odd numbers you could apply the predicate on every element in the list and&lt;br /&gt;
* If the predicate returned true, add it to the new list&lt;br /&gt;
* else ignore the current entry and try next.&lt;br /&gt;
&lt;br /&gt;
 scala&amp;gt; val numbers = 0 to 20&lt;br /&gt;
 numbers: scala.collection.immutable.Range.Inclusive with  scala.collection.immutable.Range.ByOne = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)&lt;br /&gt;
 scala&amp;gt; val evenNumbers = numbers.filter( _ % 2 == 0)&lt;br /&gt;
 evenNumbers: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20)&lt;br /&gt;
&lt;br /&gt;
===== Closure =====&lt;br /&gt;
A function which does some operation on an object but does not return any result. In otherwords, the return type of closure is void. Say we have a list of strings and we need to print them all to the console; we can do it the following way:&lt;br /&gt;
&lt;br /&gt;
 scala&amp;gt; val strs = List(&amp;quot;print&amp;quot;, &amp;quot;all&amp;quot;, &amp;quot;to&amp;quot;, &amp;quot;console&amp;quot;)&lt;br /&gt;
 strs: List[java.lang.String] = List(print, all, to, console)&lt;br /&gt;
 scala&amp;gt; strs.foreach(println) //println == System.out.println&lt;br /&gt;
 print&lt;br /&gt;
 all&lt;br /&gt;
 to&lt;br /&gt;
 console&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; is a method which is executed for every element present in the list.&lt;br /&gt;
* &amp;lt;code&amp;gt; str.foreach(println)&amp;lt;/code&amp;gt; - Now, this is getting little confusing. Whats &amp;lt;code&amp;gt;println&amp;lt;/code&amp;gt; doing without any arguments? Scala's compiler is intelligent enough to determine that println takes in a single argument and so allows us to skip specifying it.&lt;br /&gt;
===== Transformer =====&lt;br /&gt;
A function which recieves an entity/object and returns an another object(possibly of different type). Infact both predicate and a closure are transformers. To understand more what a transformer is, let us change the case of all the strings in a list.&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
   val list = List(&amp;quot;Scala&amp;quot;, &amp;quot;is&amp;quot;, &amp;quot;the coolest&amp;quot;, &amp;quot;language&amp;quot;, &amp;quot;in the world!&amp;quot;)&lt;br /&gt;
 scala&amp;gt; list.map(_.toUpperCase)&lt;br /&gt;
 res5: List[java.lang.String] = List(SCALA, IS, THE COOLEST, LANGUAGE, IN THE WORLD!)&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;scala.List's&amp;lt;/code&amp;gt; method which takes in a function as a parameter and for every element in the list invokes the function. The result returned by the function is added to a new list.&lt;br /&gt;
* &amp;lt;code&amp;gt;_.toUpperCase&amp;lt;/code&amp;gt; is the transformer function upcasing all the strings.&lt;br /&gt;
&lt;br /&gt;
==== Currying ====&lt;br /&gt;
(Excellent tutorial here: http://bit.ly/16Qs6l)&lt;br /&gt;
Named after its inventor 'Haskell Curry', it is a way of converting a function which takes multiple arguments, into a function with lesser number of arguments. Theory is always boring or we feel so. Let's jump right in and work out an example. Consider a simple function which adds two numbers.&lt;br /&gt;
&lt;br /&gt;
 def add(x:Int)(y:Int) = x + y&lt;br /&gt;
&lt;br /&gt;
Now assume you want to create a function which will add '5' to all its input and return the sum. Can you re-use the code above? Currying works great in these scenarios..&lt;br /&gt;
 val add5ToInput = add(5) _  // the '_' is to tell the compiler to create a curried version of the above function.&lt;br /&gt;
 println(add5ToInput(10))  //surprise surprise - prints 15!&lt;br /&gt;
&lt;br /&gt;
So basically what happens is that compiler creates an intermediate function which always has &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; as the first parameter. So when we say &amp;lt;code&amp;gt;add5ToInput(10)&amp;lt;/code&amp;gt; in reality, it inturn calls &amp;lt;code&amp;gt;add(5)(10)&amp;lt;/code&amp;gt;. So currying basically helps us reduce the total number of parameters which needs to passed to functions and in turn helps re-use code in a functional way!&lt;br /&gt;
&lt;br /&gt;
==== Pattern Matching ====&lt;br /&gt;
(also see http://bit.ly/3o5JA1)&lt;br /&gt;
Yet another unique feature of functional programming languages is the concept of pattern matching. Let us see what pattern matching is all about. Continuing our previous example of Person objects, let us write some code to guess the profession of a person based on his name.(I know it sounds lame..)&lt;br /&gt;
&lt;br /&gt;
 def guessProfession(p: Person) = p.name match {&lt;br /&gt;
        case &amp;quot;Jackie Chan&amp;quot; | &amp;quot;Jet Li&amp;quot; =&amp;gt; &amp;quot;Martial Artist and Actor&amp;quot;&lt;br /&gt;
        case &amp;quot;RajniKanth&amp;quot; | &amp;quot;Kamal Hasan&amp;quot; =&amp;gt; &amp;quot;South Indian Actor&amp;quot;&lt;br /&gt;
        case &amp;quot;Martin Odersky&amp;quot; | &amp;quot;Steve Yegge&amp;quot; =&amp;gt; &amp;quot;Famous Programmer&amp;quot;&lt;br /&gt;
        case _ =&amp;gt; &amp;quot;I don't know %s&amp;quot;.format(p.name)&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 val p = Person(&amp;quot;Kovalan Venkatesan&amp;quot;, 24)&lt;br /&gt;
 val p1 = Person(&amp;quot;Martin Odersky&amp;quot;, 31)&lt;br /&gt;
 println(guessProfession(p)) //prints I don't know Kovalan Venkatesan.&lt;br /&gt;
 println(guessProfession(p1)) //prints &amp;quot;Famous Programmer&amp;quot;&lt;br /&gt;
&lt;br /&gt;
As you may have noticed, the above example is very similar to switch statements in imperative languages such as C, Java. However, it is more powerful and extensible. Why?&lt;br /&gt;
&lt;br /&gt;
# In both C and Java only numeric types can be used in switch statements.(including enums which are simply names mapped onto integers). In Scala you can use almost use anything you want. Infact scala supports custom extractor patterns, whose scope is beyond this text. Please consider visiting scala resources to learn more.&lt;br /&gt;
# The cases do not allow fall through. In Java and C, you have to manually type 'break' after the end of every case. Scala does not have any 'break' nor 'continue' keywords(shocking?)&lt;br /&gt;
&lt;br /&gt;
A purely object oriented person may argue that we could use Visitor Pattern to do the same. That is true, but how many lines of code would you have to write to accomplish the same functionality? Wanna guess?(http://www.artima.com/weblogs/viewpost.jsp?thread=166742)&lt;br /&gt;
&lt;br /&gt;
More explanation of case statements:&lt;br /&gt;
* 'match' is equivalent to switch keyword in C, C++ and Java.&lt;br /&gt;
* 'case' keyword signifies the beginning of the particular case.&lt;br /&gt;
* The '_'(underscore) signifies many things in scala. When we say &amp;quot;case _ =&amp;gt; &amp;quot;, it is equivalent to the 'default' keyword in C/Java.&lt;br /&gt;
&lt;br /&gt;
===== Handling exceptions - Scala way! =====&lt;br /&gt;
In Java, exception matching is considered verbose and ugly. To open and write a file,&lt;br /&gt;
 try {&lt;br /&gt;
    File file = new File(&amp;quot;/tmp/sample.txt&amp;quot;);&lt;br /&gt;
    PrintStream printStream = new PrintStream(file);&lt;br /&gt;
    printStream.write(&amp;quot;something&amp;quot;.getBytes());&lt;br /&gt;
 } catch (FileNotFoundException e) {&lt;br /&gt;
    System.out.println(&amp;quot;Unable to find the file /tmp/sample.txt&amp;quot;);&lt;br /&gt;
    e.printStackTrace();&lt;br /&gt;
 } catch (IOException e) {&lt;br /&gt;
   System.out.println(&amp;quot;Unknown IO error&amp;quot;);&lt;br /&gt;
    e.printStackTrace();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
  try{&lt;br /&gt;
    val file = new File(&amp;quot;/tmp/sample.txt&amp;quot;)&lt;br /&gt;
    val ps = new PrintStream(file)&lt;br /&gt;
    ps.write(&amp;quot;something&amp;quot;.getBytes())&lt;br /&gt;
  }catch{&lt;br /&gt;
    case e: FileNotFoundException =&amp;gt; println(&amp;quot;Unable to find file /tmp/sample.txt&amp;quot;)&lt;br /&gt;
    case IOException =&amp;gt; println(&amp;quot;Unknown IO error&amp;quot;)&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
We have just scratched the surface of Scala, but it has innumerable features which show a beautiful blend of functional and object oriented features. Scala proves we can use them both together.&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
*[1]: http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)&lt;br /&gt;
*[2]: http://www.haskell.org&lt;br /&gt;
*[3]: http://www.scala-lang.org&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_az&amp;diff=34464</id>
		<title>CSC/ECE 517 Fall 2010/ch1 1e az</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2010/ch1_1e_az&amp;diff=34464"/>
		<updated>2010-09-09T02:08:34Z</updated>

		<summary type="html">&lt;p&gt;Anadath: /* &amp;lt;b&amp;gt;Functions as Objects&amp;lt;/b&amp;gt; */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Programming Paradigms==&lt;br /&gt;
 &lt;br /&gt;
Every computer program needs a style of writing which specifies how to solve a software engineering problem. This style is represented by the paradigm. Each computer program follows one or more paradigm which differs in representing the elements of a program(such as variables and objects) and the steps needed to compute a task.&lt;br /&gt;
&lt;br /&gt;
Diferent paradigms are:&lt;br /&gt;
&lt;br /&gt;
1. Procedural/imperative paradigms: Assembly, C, C++, Java, C#&lt;br /&gt;
&lt;br /&gt;
2. Object Oriented paradigm : C++, Java, Python, Ruby, Scala, C#&lt;br /&gt;
&lt;br /&gt;
3. Functional Paradigm : Lisp, Haskell, Clojure, Scala, OCaml, Ruby&lt;br /&gt;
&lt;br /&gt;
4. Logic Paradigm: Prolog&lt;br /&gt;
&lt;br /&gt;
==Multi-Paradigm Programming==&lt;br /&gt;
&lt;br /&gt;
Multiparadigm refers to use of a combination of programming paradigms for solving a computer problem. Some languages subscribe strictly to a single paradigm like Assembly and C. Others like Java, C++, Scala and C# employ more than one paradigm. Every paradigm comes with its own strength and weakness and this quite motivates us to take advantage of each paradigm and use it in a manner that best fits the problem at hand.&lt;br /&gt;
&lt;br /&gt;
==Overview of Functional Programming==&lt;br /&gt;
&lt;br /&gt;
Functional programming is derived from lambda calculus which models computations as the evaluation of functions and recursions. More emphasis is laid on application of functions rather than changing the state of variables in a program. Functional programming relies heavily on recursions and it is the only way to iterate instead of loops.&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;How it differs from imperative programming&amp;lt;/b&amp;gt;===&lt;br /&gt;
    &lt;br /&gt;
Imperative programming follows the Von-Neumann architecture and mainly consists of loops and usage of globa states to perform a computation. For example consider the task of computing the sum of numbers from 1 to n. &lt;br /&gt;
&lt;br /&gt;
In imperative style,&lt;br /&gt;
        &lt;br /&gt;
        sum := 0         // global state&lt;br /&gt;
        for i &amp;lt;- 1 to n do&lt;br /&gt;
            sum := sum + i&lt;br /&gt;
            &lt;br /&gt;
In Functional style,&lt;br /&gt;
    &lt;br /&gt;
         func sum(n)        // note that sum is a function and is recursive&lt;br /&gt;
           if  n = 1 then&lt;br /&gt;
               return 1&lt;br /&gt;
           else&lt;br /&gt;
               return n + sum(n-1)&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Pure and impure functional programming&amp;lt;/b&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
Purely functional programming exhibit referential transparency [http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)] which does not involve any global state or I/O changes. If the same functional expression results in the same output value for the same argument x at different stages of execution , then the function is said to be pure which is devoid of any global state change. For example Haskell [http://www.haskell.org] is purely functional.&lt;br /&gt;
&lt;br /&gt;
==Overview of object oriented programming==&lt;br /&gt;
&lt;br /&gt;
Typially programs were written with a procedural view where the program's logic was given utmost importance and it follows a sequential structure. But object oriented techniques really cares about the data or objects we want to mainpulate with rather than the logic of  a program. Every object contains its own state(in the form of variables) and and a number of methods to manipulate the variables.For example consider the class Add:&lt;br /&gt;
           &lt;br /&gt;
           class Add{&lt;br /&gt;
           /* Object variables */&lt;br /&gt;
                  private int sum = 0;&lt;br /&gt;
          /* Object Methods */&lt;br /&gt;
                  public void calculate_sum(int n){&lt;br /&gt;
                      int i=0;&lt;br /&gt;
                      while(i &amp;lt;= n){&lt;br /&gt;
                          sum = sum + i;&lt;br /&gt;
                          i++;&lt;br /&gt;
                      }&lt;br /&gt;
                  }&lt;br /&gt;
            }&lt;br /&gt;
       &lt;br /&gt;
            Add instance1 = new Add();     // instance1 is an object of type Add&lt;br /&gt;
            instance1.calculate_sum(100);&lt;br /&gt;
           &lt;br /&gt;
===&amp;lt;b&amp;gt;Principles of object oriented design&amp;lt;/b&amp;gt;===&lt;br /&gt;
           &lt;br /&gt;
1. &amp;lt;b&amp;gt;Encapsulation:&amp;lt;/b&amp;gt; Encapsulation refers to hiding the internal representation of the object from the outside view. For example algorithm to compute the calculate_sum() method may be hidden, but it will present the user with the expected results.&lt;br /&gt;
&lt;br /&gt;
2. &amp;lt;b&amp;gt;Polymorphism:&amp;lt;/b&amp;gt; Polymorphism means ability to take multiple forms. For example an operation may exhibit different behaviour at different instances. Consider the operator '+', for numbers it will generate the sum, but for strings it will produce a third string which concatenates the input strings.&lt;br /&gt;
&lt;br /&gt;
3. &amp;lt;b&amp;gt;Inheritance:&amp;lt;/b&amp;gt; Inheritance involves base and derived classes with derived class inheriting all the methods and states frm the base class. Inheritance basically forms a hirerachy. For example if shape is an object, then objects square, rectangle, circle all derive the same characteristic as shape does.&lt;br /&gt;
&lt;br /&gt;
==Functional + OOP code==&lt;br /&gt;
&lt;br /&gt;
===&amp;lt;b&amp;gt;Functions as Objects&amp;lt;/b&amp;gt;===&lt;br /&gt;
&lt;br /&gt;
One of the cornerstone of functional and object oriented code is treating functions as objects.It means that we can pass functions as arguments, store it and return them from other functions. This is highly useful in an user interface code where it can be used as call back functions which get called when an event occurs( in event driven programming)&lt;br /&gt;
&lt;br /&gt;
An example in Scala [http://www.scala-lang.org]:&lt;br /&gt;
&lt;br /&gt;
  Object Timer{&lt;br /&gt;
    def  action(callback() : () =&amp;gt; unit){              // callback() is the function passed as an argument&lt;br /&gt;
         while( some condition)                                                            &lt;br /&gt;
         { &lt;br /&gt;
            callback();&lt;br /&gt;
            Thread.sleep(3000)&lt;br /&gt;
         }&lt;br /&gt;
     }&lt;br /&gt;
     def event(){                                      //  event is the method which tells what to do &lt;br /&gt;
         /* process the event here&lt;br /&gt;
         &lt;br /&gt;
         */      &lt;br /&gt;
     }&lt;br /&gt;
        &lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
Another example of functional concept in object oriented design is the concept of blocks especially in Ruby. Blocks are basically nameless functions which can be passed to a function and then that function can invoke the passed in nameless function. This is a common style called higher order function style among languages that handle functions as first class objects. Basically blocks can be designed for loop abstraction and lets the user to design their own way of iterating the values, thereby hiding the implementation details. &lt;br /&gt;
For example if we want to iterate backwards from the end to the beginning without revealing its internal implementations, we could implement the loop logic inside the block and pass it to a method or function.&lt;br /&gt;
&lt;br /&gt;
Here is a Ruby implementation of block&lt;br /&gt;
 &lt;br /&gt;
 def printlist(array,&amp;amp;reverse)&lt;br /&gt;
    array.each do |element|&lt;br /&gt;
       puts &amp;quot;array element: #{element}&amp;quot;&lt;br /&gt;
    end&lt;br /&gt;
    reverse.call(array)   /* Here it prints the reverse implementation of list*/&lt;br /&gt;
    end&lt;br /&gt;
  &lt;br /&gt;
 printlist([1,2,3,4,5,6]) { |array| &lt;br /&gt;
   array.reverse.each do |element| &lt;br /&gt;
     puts &amp;quot;array element: #{element}&amp;quot; &lt;br /&gt;
   end &lt;br /&gt;
 }&lt;br /&gt;
Next we will take a look at some of the features of Scala which supports a blend of functional and object oriented concepts in a concise manner.&lt;br /&gt;
&lt;br /&gt;
= Scala =&lt;br /&gt;
Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages&lt;br /&gt;
&lt;br /&gt;
Scala can be considered a very good blend of the best of functional and object oriented world. It was conceived(in 2001) and implemented by Martin Odersky who was also the author of Sun javac compiler and played a major role in the design and retrofitting of generics into Java. The latest version of Scala is 2.8 and includes a number of performance improvements over previous versions. We shall see more about the features of Scala in the following pages, but for fun, some facts!&lt;br /&gt;
&lt;br /&gt;
=== Features ===&lt;br /&gt;
==== Statically Typed ====&lt;br /&gt;
Scala belongs to the family of languages which do not allow the type of variables once they have been defined. In other words, the type information of each and every variable is encoded in the generated code and the compiler makes sure, you are not doing something funny. For example, lets consider the following example,&lt;br /&gt;
&lt;br /&gt;
In Ruby,&lt;br /&gt;
   irb(main):001:0&amp;gt; str=&amp;quot;Hello World&amp;quot;&lt;br /&gt;
    =&amp;gt; &amp;quot;Hello World&amp;quot;&lt;br /&gt;
    irb(main):002:0&amp;gt; puts str.class&lt;br /&gt;
    String&lt;br /&gt;
    irb(main):003:0&amp;gt; str=123&lt;br /&gt;
    =&amp;gt; 123&lt;br /&gt;
    irb(main):004:0&amp;gt; puts str.class&lt;br /&gt;
    Fixnum&lt;br /&gt;
&lt;br /&gt;
In the above example, first we define 'str' variable to be of type String and then as a number. So basically, no type information is associated with the variable at any point in the program. Hence we can easily reassociate that variable with an object/instance&lt;br /&gt;
of a different type. This is a feature of all dynamically typed languages(like Ruby, Python, Clojure). However most of the statically typed languages such as Scala, Java, C/C++ do not allow you do so. In Scala,&lt;br /&gt;
&lt;br /&gt;
  scala&amp;gt; var str=&amp;quot;Hello World&amp;quot;&lt;br /&gt;
  str: java.lang.String = Hello World&lt;br /&gt;
  scala&amp;gt; str=123&lt;br /&gt;
  &amp;lt;console&amp;gt;:6: error: type mismatch;&lt;br /&gt;
   found   : Int(123)&lt;br /&gt;
   required: java.lang.String&lt;br /&gt;
         str=123&lt;br /&gt;
==== Mixed paradigm ====&lt;br /&gt;
Scala is a strange specimen. It is a complete blend of object oriented and functional programming. How can this happen? Functional world advocates immutability but object oriented world is all about state and how do you mutate it to fit your needs. Scala allows you create both mutable and immutable objects. Consider the following example,&lt;br /&gt;
 val str: String = &amp;quot;Hello World&amp;quot;&lt;br /&gt;
In the first line, I am creating a immutable variable(a misnomer indeed). Roughly translating the above line to Java is below:&lt;br /&gt;
&lt;br /&gt;
 final String str = &amp;quot;Hello World&amp;quot;; //note the extra semi-colon&lt;br /&gt;
Consider the next example below,&lt;br /&gt;
 val list: List[String] = List(&amp;quot;Scala&amp;quot;, &amp;quot;is&amp;quot;, &amp;quot;the coolest&amp;quot;, &amp;quot;language&amp;quot;, &amp;quot;in the world!&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The same code can be written in Java as below&lt;br /&gt;
&lt;br /&gt;
  List&amp;lt;String&amp;gt; mutableList = new ArrayList&amp;lt;String&amp;gt;();&lt;br /&gt;
  list.add(&amp;quot;Scala&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;is&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;the coolest&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;language&amp;quot;);&lt;br /&gt;
  list.add(&amp;quot;in the world!&amp;quot;);&lt;br /&gt;
  List&amp;lt;String&amp;gt; immutableList = Collections.immutableList(mutableList);&lt;br /&gt;
&lt;br /&gt;
Compared to Java, Scala reduces the total number of lines you need to type and at the same time looks so much simpler and less verbose. Even though the above example shows immutable collection example, scala also provides mutable collection and object types under the package scala.collection.mutable.*&lt;br /&gt;
&lt;br /&gt;
As it can be observed, even though scala advocates immutability, it does not restrict you from creating mutable objects. So its the best of both worlds. Another part of functional world is the concept of functions as 'first class members' of the language. Some of the common languages like Ruby, Python and JavaScript all incorporate this feature. In the sense you can pass around functions to methods as a parameter. Enough talking;&lt;br /&gt;
&lt;br /&gt;
Let us have a class say 'Person' which has properties, name and another variable age.&lt;br /&gt;
In Java:&lt;br /&gt;
&lt;br /&gt;
 class Person{&lt;br /&gt;
    private String name;&lt;br /&gt;
    private int age;&lt;br /&gt;
    public String getName(){&lt;br /&gt;
        return name;&lt;br /&gt;
    }&lt;br /&gt;
    public void setName(String s){&lt;br /&gt;
        this.name = s;&lt;br /&gt;
    }&lt;br /&gt;
    public int getAge(){&lt;br /&gt;
        return age;&lt;br /&gt;
    }&lt;br /&gt;
    public void setAge(int age){&lt;br /&gt;
        this.age = age;&lt;br /&gt;
    }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
 case class Person(name: String, age: Int)&lt;br /&gt;
&lt;br /&gt;
This single line is equivalent to the above java code! So much cooler.. I would not be doing justice if I don't mention the fact that both equals() and hashCode() method are automatically generated for you by the compiler. Isn't this an awesome feature? No wonder Scala is considered a beautiful and high level language. Now assume we have a list of persons and we want to filter out all the persons who are of age 23. How would you do that in Java?&lt;br /&gt;
&lt;br /&gt;
  List&amp;lt;Person&amp;gt; persons; //has a bunch of person objects in it.&lt;br /&gt;
  List&amp;lt;Person&amp;gt; twentyThree = new ArrayList&amp;lt;Person&amp;gt;();&lt;br /&gt;
  for(Person p: persons){&lt;br /&gt;
      if(p.getAge() == 23){&lt;br /&gt;
          twentyThree.add(p);&lt;br /&gt;
      }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
&lt;br /&gt;
 val twentyThree: List[Person] = persons.filter( _.age == 23)&lt;br /&gt;
&lt;br /&gt;
That's it! In case you are wondering whats going on, filter is a method present on scala.List class and the code block  _.age == 23 is a function created on the fly and passed onto the filter method. More precisely its called a closure - a functional concept which can be emulated in java only using anonymous inner classes. In case you are wondering how scala does this, scala's compiler transforms our one line function into some anonymous inner classes when generating java byte code but allows developer to define functions in a simple manner.&lt;br /&gt;
&lt;br /&gt;
Infact every function is an object in scala. Scala's standard library has a number of functions which are object themselves.&lt;br /&gt;
&lt;br /&gt;
==== Sophisticated Syntax/Features ====&lt;br /&gt;
When we say sophisticated, its really an awesome feature which is present in other functional languages like Haskell, (O)Caml and Erlang. Consider the following example,&lt;br /&gt;
&lt;br /&gt;
   val name = &amp;quot;Jackie Chan&amp;quot;&lt;br /&gt;
   println(name.getClass) //prints out String.&lt;br /&gt;
&lt;br /&gt;
Previously we saw that scala is a statically typed language and yet in the above example we don't specify what type is the variable name. In Java, you would type in the following:&lt;br /&gt;
&lt;br /&gt;
 String name = &amp;quot;Jackie Chan&amp;quot;&lt;br /&gt;
&lt;br /&gt;
So basically in Java you specify what type the name variable is. Scala compiler is very smart in the manner that it can automatically detect the type of the variable even if you don't specify it. Consider the following example,&lt;br /&gt;
&lt;br /&gt;
 case class Person(name: String, age: Int)&lt;br /&gt;
 val persons = List(&lt;br /&gt;
                        Person(&amp;quot;Jackie Chan&amp;quot;, 51), Person(&amp;quot;Jet Li&amp;quot;, 41),&lt;br /&gt;
                        Person(&amp;quot;RajniKanth&amp;quot;, 51), Person(&amp;quot;Kamal Hasan&amp;quot;, 44),&lt;br /&gt;
                        Person(&amp;quot;Martin Odersky&amp;quot;, 18), Person(&amp;quot;Steve Yegge&amp;quot;, 23))&lt;br /&gt;
 //notice that you don't have to use 'new' keyword to create new instances. This is taken care of by the companion objects.&lt;br /&gt;
&lt;br /&gt;
The above lines of code create a list with 6 different persons(An immutable list)&lt;br /&gt;
&lt;br /&gt;
Now, as in ruby scala supports the concepts of closure. Let's see what it is.&lt;br /&gt;
&lt;br /&gt;
 val twentyThree: List[Person] = persons.filter( { p: Person =&amp;gt; p.age == 3})&lt;br /&gt;
&lt;br /&gt;
In the above set of lines, the code within the '{ }' brackets is a function's body. Basically what the compiler did was to create a function which takes a single argument and returns either true or false. Now that's called a high level language. Ready for more?&lt;br /&gt;
&lt;br /&gt;
==== A Scalable Language ====&lt;br /&gt;
In almost all programming languages, there is a necessity to profile the running time of the procedures and tune its performance. The old way of doing things in Java would be:&lt;br /&gt;
&lt;br /&gt;
 public class Main{&lt;br /&gt;
 public static int factorial(int x){&lt;br /&gt;
   if(x == 0 || x == 1) return 1&lt;br /&gt;
   else return x * factorial(x-1)&lt;br /&gt;
  }&lt;br /&gt;
  public static void main(String [] args){&lt;br /&gt;
    long beforeTime = System.currentTimeMillis();&lt;br /&gt;
    int result = factorial(5);&lt;br /&gt;
    System.out.println(&amp;quot;Total time taken to execute factorial(5) is &amp;quot; + (System.currentTimeMillis() - result)+&amp;quot;ms&amp;quot;);&lt;br /&gt;
    System.out.println(&amp;quot;factorial(5) = &amp;quot; + result);&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
However, if you want to find out the execution time of some other method, you would have to type in all the above lines again. So much for code re-use! Lets see how its done in Scala. Hold on to your seats...&lt;br /&gt;
&lt;br /&gt;
 object Main{&lt;br /&gt;
  def main(args: Array[String]){&lt;br /&gt;
    def factorial(x: Int):Int = x match{&lt;br /&gt;
      case 0 | 1 =&amp;gt; 1&lt;br /&gt;
      case _ =&amp;gt; x * factorial(x-1)&lt;br /&gt;
  }&lt;br /&gt;
  import System.currentTimeMillis&lt;br /&gt;
  def timeIt(msg: String = &amp;quot;Time to execute is: &amp;quot;)(func: =&amp;gt; Unit){&lt;br /&gt;
    val before = currentTimeMillis&lt;br /&gt;
    func&lt;br /&gt;
    println(msg + (currentTimeMillis - before) + &amp;quot;ms&amp;quot;)&lt;br /&gt;
  }&lt;br /&gt;
  timeIt(&amp;quot;Total time taken to execute factorial(5) is &amp;quot;){&lt;br /&gt;
      val result = factorial(5)&lt;br /&gt;
      println(&amp;quot;factorial(5) = &amp;quot; + result)&lt;br /&gt;
  }&lt;br /&gt;
 }&lt;br /&gt;
 //would print: factorial(5) = 120&lt;br /&gt;
 // Total time taken to execute factorial(5) is 1ms&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That's not right! The function &amp;lt;code&amp;gt;timeIt&amp;lt;/code&amp;gt; looks like a feature inbuilt into the language. Turn your disbelief into amazement;save the above code into a separate file and run it. Infact using this feature, it is possible to build powerful DSL languages which can run at full speed on JVM.&lt;br /&gt;
&lt;br /&gt;
Ruby allows you to add new methods to objects. This feature is often called as '''Monkey Patching''' because you are splitting open the class and adding/overriding methods (in)|(to) the class. Often this leads to unexpected behavior at runtime because, other libraries including into build path may depend on some methods which you changed. Scala provides yet another feature called implicits.&lt;br /&gt;
&lt;br /&gt;
In Java, String class is final so there is no way you can add new methods to it. But with implicits you can do more! Suppose you have some text in memory and you know it represents a number. But in Java the usual way to convert string to int is to do a Integer.parseInt(string) which seems so unnecessary. Let us make our lives easier...&lt;br /&gt;
&lt;br /&gt;
  implicit def strToInt(str: String) = new {&lt;br /&gt;
    def asInt = Integer.parseInt(str)&lt;br /&gt;
  }&lt;br /&gt;
  val str = &amp;quot;1234&amp;quot;&lt;br /&gt;
  println(str.asInt * 3) //prints out 3702&lt;br /&gt;
&lt;br /&gt;
The above code gives us an impression as if String class has gained new 'asInt' method. Can static object oriented languages do this? No! Infact scala cheats by calling our implicits to convert it into a different object as and when needed.&lt;br /&gt;
&lt;br /&gt;
The above functionalities are purely object oriented, yet they are safe. They don't pollute the global namespace unlike Ruby or Python&lt;br /&gt;
&lt;br /&gt;
==== Broad Classification of Functions ====&lt;br /&gt;
===== Predicate =====&lt;br /&gt;
It is a function which receives an object as argument and either returns true or false. They are useful in a number of situations. Suppose you had a list of integers and you wanted to filter out all odd numbers you could apply the predicate on every element in the list and&lt;br /&gt;
* If the predicate returned true, add it to the new list&lt;br /&gt;
* else ignore the current entry and try next.&lt;br /&gt;
&lt;br /&gt;
 scala&amp;gt; val numbers = 0 to 20&lt;br /&gt;
 numbers: scala.collection.immutable.Range.Inclusive with  scala.collection.immutable.Range.ByOne = Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)&lt;br /&gt;
 scala&amp;gt; val evenNumbers = numbers.filter( _ % 2 == 0)&lt;br /&gt;
 evenNumbers: scala.collection.immutable.IndexedSeq[Int] = Vector(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20)&lt;br /&gt;
&lt;br /&gt;
===== Closure =====&lt;br /&gt;
A function which does some operation on an object but does not return any result. In otherwords, the return type of closure is void. Say we have a list of strings and we need to print them all to the console; we can do it the following way:&lt;br /&gt;
&lt;br /&gt;
 scala&amp;gt; val strs = List(&amp;quot;print&amp;quot;, &amp;quot;all&amp;quot;, &amp;quot;to&amp;quot;, &amp;quot;console&amp;quot;)&lt;br /&gt;
 strs: List[java.lang.String] = List(print, all, to, console)&lt;br /&gt;
 scala&amp;gt; strs.foreach(println) //println == System.out.println&lt;br /&gt;
 print&lt;br /&gt;
 all&lt;br /&gt;
 to&lt;br /&gt;
 console&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;foreach&amp;lt;/code&amp;gt; is a method which is executed for every element present in the list.&lt;br /&gt;
* &amp;lt;code&amp;gt; str.foreach(println)&amp;lt;/code&amp;gt; - Now, this is getting little confusing. Whats &amp;lt;code&amp;gt;println&amp;lt;/code&amp;gt; doing without any arguments? Scala's compiler is intelligent enough to determine that println takes in a single argument and so allows us to skip specifying it.&lt;br /&gt;
===== Transformer =====&lt;br /&gt;
A function which recieves an entity/object and returns an another object(possibly of different type). Infact both predicate and a closure are transformers. To understand more what a transformer is, let us change the case of all the strings in a list.&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
   val list = List(&amp;quot;Scala&amp;quot;, &amp;quot;is&amp;quot;, &amp;quot;the coolest&amp;quot;, &amp;quot;language&amp;quot;, &amp;quot;in the world!&amp;quot;)&lt;br /&gt;
 scala&amp;gt; list.map(_.toUpperCase)&lt;br /&gt;
 res5: List[java.lang.String] = List(SCALA, IS, THE COOLEST, LANGUAGE, IN THE WORLD!)&lt;br /&gt;
&lt;br /&gt;
Here&lt;br /&gt;
* &amp;lt;code&amp;gt;map&amp;lt;/code&amp;gt; is &amp;lt;code&amp;gt;scala.List's&amp;lt;/code&amp;gt; method which takes in a function as a parameter and for every element in the list invokes the function. The result returned by the function is added to a new list.&lt;br /&gt;
* &amp;lt;code&amp;gt;_.toUpperCase&amp;lt;/code&amp;gt; is the transformer function upcasing all the strings.&lt;br /&gt;
&lt;br /&gt;
==== Currying ====&lt;br /&gt;
(Excellent tutorial here: http://bit.ly/16Qs6l)&lt;br /&gt;
Named after its inventor 'Haskell Curry', it is a way of converting a function which takes multiple arguments, into a function with lesser number of arguments. Theory is always boring or we feel so. Let's jump right in and work out an example. Consider a simple function which adds two numbers.&lt;br /&gt;
&lt;br /&gt;
 def add(x:Int)(y:Int) = x + y&lt;br /&gt;
&lt;br /&gt;
Now assume you want to create a function which will add '5' to all its input and return the sum. Can you re-use the code above? Currying works great in these scenarios..&lt;br /&gt;
 val add5ToInput = add(5) _  // the '_' is to tell the compiler to create a curried version of the above function.&lt;br /&gt;
 println(add5ToInput(10))  //surprise surprise - prints 15!&lt;br /&gt;
&lt;br /&gt;
So basically what happens is that compiler creates an intermediate function which always has &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; as the first parameter. So when we say &amp;lt;code&amp;gt;add5ToInput(10)&amp;lt;/code&amp;gt; in reality, it inturn calls &amp;lt;code&amp;gt;add(5)(10)&amp;lt;/code&amp;gt;. So currying basically helps us reduce the total number of parameters which needs to passed to functions and in turn helps re-use code in a functional way!&lt;br /&gt;
&lt;br /&gt;
==== Pattern Matching ====&lt;br /&gt;
(also see http://bit.ly/3o5JA1)&lt;br /&gt;
Yet another unique feature of functional programming languages is the concept of pattern matching. Let us see what pattern matching is all about. Continuing our previous example of Person objects, let us write some code to guess the profession of a person based on his name.(I know it sounds lame..)&lt;br /&gt;
&lt;br /&gt;
 def guessProfession(p: Person) = p.name match {&lt;br /&gt;
        case &amp;quot;Jackie Chan&amp;quot; | &amp;quot;Jet Li&amp;quot; =&amp;gt; &amp;quot;Martial Artist and Actor&amp;quot;&lt;br /&gt;
        case &amp;quot;RajniKanth&amp;quot; | &amp;quot;Kamal Hasan&amp;quot; =&amp;gt; &amp;quot;South Indian Actor&amp;quot;&lt;br /&gt;
        case &amp;quot;Martin Odersky&amp;quot; | &amp;quot;Steve Yegge&amp;quot; =&amp;gt; &amp;quot;Famous Programmer&amp;quot;&lt;br /&gt;
        case _ =&amp;gt; &amp;quot;I don't know %s&amp;quot;.format(p.name)&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
 val p = Person(&amp;quot;Kovalan Venkatesan&amp;quot;, 24)&lt;br /&gt;
 val p1 = Person(&amp;quot;Martin Odersky&amp;quot;, 31)&lt;br /&gt;
 println(guessProfession(p)) //prints I don't know Kovalan Venkatesan.&lt;br /&gt;
 println(guessProfession(p1)) //prints &amp;quot;Famous Programmer&amp;quot;&lt;br /&gt;
&lt;br /&gt;
As you may have noticed, the above example is very similar to switch statements in imperative languages such as C, Java. However, it is more powerful and extensible. Why?&lt;br /&gt;
&lt;br /&gt;
# In both C and Java only numeric types can be used in switch statements.(including enums which are simply names mapped onto integers). In Scala you can use almost use anything you want. Infact scala supports custom extractor patterns, whose scope is beyond this text. Please consider visiting scala resources to learn more.&lt;br /&gt;
# The cases do not allow fall through. In Java and C, you have to manually type 'break' after the end of every case. Scala does not have any 'break' nor 'continue' keywords(shocking?)&lt;br /&gt;
&lt;br /&gt;
A purely object oriented person may argue that we could use Visitor Pattern to do the same. That is true, but how many lines of code would you have to write to accomplish the same functionality? Wanna guess?(http://www.artima.com/weblogs/viewpost.jsp?thread=166742)&lt;br /&gt;
&lt;br /&gt;
More explanation of case statements:&lt;br /&gt;
* 'match' is equivalent to switch keyword in C, C++ and Java.&lt;br /&gt;
* 'case' keyword signifies the beginning of the particular case.&lt;br /&gt;
* The '_'(underscore) signifies many things in scala. When we say &amp;quot;case _ =&amp;gt; &amp;quot;, it is equivalent to the 'default' keyword in C/Java.&lt;br /&gt;
&lt;br /&gt;
===== Handling exceptions - Scala way! =====&lt;br /&gt;
In Java, exception matching is considered verbose and ugly. To open and write a file,&lt;br /&gt;
 try {&lt;br /&gt;
    File file = new File(&amp;quot;/tmp/sample.txt&amp;quot;);&lt;br /&gt;
    PrintStream printStream = new PrintStream(file);&lt;br /&gt;
    printStream.write(&amp;quot;something&amp;quot;.getBytes());&lt;br /&gt;
 } catch (FileNotFoundException e) {&lt;br /&gt;
    System.out.println(&amp;quot;Unable to find the file /tmp/sample.txt&amp;quot;);&lt;br /&gt;
    e.printStackTrace();&lt;br /&gt;
 } catch (IOException e) {&lt;br /&gt;
   System.out.println(&amp;quot;Unknown IO error&amp;quot;);&lt;br /&gt;
    e.printStackTrace();&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
In Scala:&lt;br /&gt;
  try{&lt;br /&gt;
    val file = new File(&amp;quot;/tmp/sample.txt&amp;quot;)&lt;br /&gt;
    val ps = new PrintStream(file)&lt;br /&gt;
    ps.write(&amp;quot;something&amp;quot;.getBytes())&lt;br /&gt;
  }catch{&lt;br /&gt;
    case e: FileNotFoundException =&amp;gt; println(&amp;quot;Unable to find file /tmp/sample.txt&amp;quot;)&lt;br /&gt;
    case IOException =&amp;gt; println(&amp;quot;Unknown IO error&amp;quot;)&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
We have just scratched the surface of Scala, but it has innumerable features which show a beautiful blend of functional and object oriented features. Scala proves we can use them both together.&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
*[1]: http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)&lt;br /&gt;
*[2]: http://www.haskell.org&lt;br /&gt;
*[3]: http://www.scala-lang.org&lt;/div&gt;</summary>
		<author><name>Anadath</name></author>
	</entry>
</feed>