CSC/ECE 517 Fall 2010/ch6 6a RJ: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 18: Line 18:
Below is a look at some coding in Lua:
Below is a look at some coding in Lua:


Initially, we define a couple of named pieces of code (functions) which take a table as their parameter, and we provide functionality withing them that manipulates the table:
Initially, a couple of named pieces of code (functions) are defined which take a table as their parameter, and some functionality is provided within them that manipulates the table:
<pre>
<pre>
function effage(animule)
function effage(animule)
Line 28: Line 28:
</pre>
</pre>


Then a number of tables are defined, each representing a single "something" [object] which contains attributes / properties / characteristic values (choose whatever you feel is the most appropriate word. Significantly, we also add in to the table the function code, just defined above, which can operate on the other elements within that table. We can do this because functions in Lua are held in "first class variable" just like any other user supplied elements, and that an assignment copies a reference, so that we're not cloning hundreds of copies of what could be substantial code.
Then a number of tables are defined, each representing a single "something" [object] which contains attributes / properties / characteristic values (choose whatever you feel is the most appropriate word.) Significantly, we also add in to the table the function code, just defined above, which can operate on the other elements within that table. This can be done because functions in Lua are held in <b>"first class variable"</b> just like any other user supplied elements, and that an assignment copies a reference, so that we're not cloning hundreds of copies of what could be substantial code.


<pre>
<pre>
Line 47: Line 47:
</pre>
</pre>


We can then loop through our array of objects, accessing individual member elements (attributes / properties) by name, and running pieces of code which are referenced from within each of them to perform actions on the data they hold ...so that those functions have noe turned (in OO terms) into methods. With different methods referenced within differing objects, you'll see that we've implemented <b>polymorphism</b> via the back door!
We can then loop through our array of objects, accessing individual member elements (attributes / properties) by name, and running pieces of code which are referenced from within each of them to perform actions on the data they hold ...so that those functions have now turned (in OO terms) into methods. With different methods referenced within differing objects, you'll see that we've implemented <b>polymorphism</b> via the back door!


<pre>
<pre>

Revision as of 04:13, 17 November 2010

Survey of selected Delegation-based Programming Languages

Delegation-based programming languages, also known as Prototype-based programming languages, represent a style of object-oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based languages) is performed via a process of cloning existing objects that serve as prototypes. This model can also be known as class-less, prototype-oriented or instance-based programming. Delegation is the language feature that supports prototype-based programming.

The original, and most canonical, example of a prototype-based language is the programming language Self developed by David Ungar and Randall Smith. Since the late 1990s, the classless programming style has grown increasingly popular, and has been adopted for the languages Javascript, ActionScript, Cecil, NewtonScript, Io, Moo, REBOL, Lisaac, Lua and several others.[1]. This topic will briefly review features of three Delegation-based programming languages: Lua, Io, and Actionscript.


Lua

Lua is a powerful, fast, lightweight, embeddable scripting language. It combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping [2]]. Lua is a free open-source software product.

History of Lua

Lua was created in 1993 by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, and Waldemar Celes, members of the Computer Graphics Technology Group (Tecgraf) at the Pontifical Catholic University of Rio de Janeiro]in Brazil. [3] The design of the Lua programming language was influenced by the programming languages Scheme, SNOBAL, Modula-2, CLU, and C++.

Lua and Object Oriented Programming

Lua isn't an object oriented languages in the sense that you define classes, create objects with a new method, define interfaces and declare variables to be private or protected. Lua is a tool in which you can use object oriented techniques with a freedom and flexibility that you don't have with a straitjacket language such as Java.[4]

Below is a look at some coding in Lua:

Initially, a couple of named pieces of code (functions) are defined which take a table as their parameter, and some functionality is provided within them that manipulates the table:

function effage(animule)
  return animule.age * animule.factor
  end
function stone(thecrows)
  return "as da hills"
  end

Then a number of tables are defined, each representing a single "something" [object] which contains attributes / properties / characteristic values (choose whatever you feel is the most appropriate word.) Significantly, we also add in to the table the function code, just defined above, which can operate on the other elements within that table. This can be done because functions in Lua are held in "first class variable" just like any other user supplied elements, and that an assignment copies a reference, so that we're not cloning hundreds of copies of what could be substantial code.

gypsy = {breed = "dog", age = 3, weight = 28,
  factor = 7, name = 'Gypsy Ellis', efa = effage}
rocky = {breed = "rock", weight = 28,
  type = "Granite", name = 'Rocky Bamboa', efa = stone}
charlie = {breed = "cat", age = 13, weight = 4,
  factor = 6, name = "Charlie Ellis", efa = effage}
flipper = {breed = "dolphin", age = 7, weight = 300,
  factor = 2, name = "Flipper Lichtenstein", efa = effage}

Although "structured" programmers might have chosen to have an array of breeds, an array of ages, etc, we have chosen to use a separate table for each of the disparate value types associated with an individual item - and we can now make up a table of each of those items - a table of tables, if you like, or an "array of objects" if you would like me to start using OO terminology:

pets = {gypsy, charlie, rocky, flipper}

We can then loop through our array of objects, accessing individual member elements (attributes / properties) by name, and running pieces of code which are referenced from within each of them to perform actions on the data they hold ...so that those functions have now turned (in OO terms) into methods. With different methods referenced within differing objects, you'll see that we've implemented polymorphism via the back door!

for k,v in pairs(pets) do
  print (k,v,v.name)
  print ("yay",v.efa(v))
  end


Info about Lua Language goes here.

Javascript

Info about Javascript Language goes here.

Actionscript

Info about Actionscript Language goes here.

Lua

Info about Lua Language goes here.


Io

Info about Io Language goes here.

Delegation-based Programming Languages compared to Class-based Programming Languages

Intro

Delegation-based Programming Languages compared to C++

Delegation-based Programming Languages compared to Java

Delegation-based Programming Languages compared to Ruby

References

[1] http://en.wikipedia.org/wiki/Prototype-based_programming