Training, Open Source computer languages

PerlPythonMySQLTclRubyC & C++LuaJavaTomcatPHPhttpdLinux

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
First class functions in Lua lead to powerful OO facilities
"In Lua, function names are first class variables." Ok - so what does that mean?

At the most direct / simplest, it means that a function can be stored in a variable - so that you can write

function oik()
  print ("does the real work!")
  end


and then

action = oik
action()

to run the function via another variable ....

"So what?" you may ask.

It means that you can set up a series of functions ... and then a series of tables each containing the data about a particular (in my example) person, and within the table have a member variable that defines which particular function is run at a certain time.

By, in turn, storing those other tables into a table of people, it means you can then write a loop which processes data for each person, and which takes a slightly different action depending on which particular function (logic) you have referenced within each table object.

One you've got this set up ... the resultant code can be really elegant ... from a table called team as your team of people ...

quay,bollard = next(team, nil)
while quay do
  print (quay,bollard["name"],
    bollard["approach"](oik),
    bollard["age"],"\n")
  quay,bollard = next(team, quay)
  end


... and different logic will be run from the "approach" member depending on what approach you have defined for that particular member.

This is a powerful technique where you have a number of things (members) each of which may have slightly different logic applied to them - in fact (although I have avoided the words so far) it's an excellent application of Object Oriented principles. You have

• Each table within the master table being an object
• Code that varies depending on the object being handled (polymorphism)
• Common logic only being defined once, with extra added as necessary to specific objects (inheritance)
• The ability to add in extra types (classes) without having to rewrite the main logic in any way

Complete example at this page
(written 2008-08-07 07:55:19)

 
Associated topics are indexed under
U106 - Lua - Functions.
U107 - Object Orientation - the Lua way

Back to
Lua - Table elements v table as a whole
Previous and next
or
Horse's mouth home
Forward to
Lua examples, Lua Courses

Some other Articles
Who is watching you?
London for the visitor, for free
Moodle, Drupal, Django (and Rails)
Lua examples, Lua Courses
First class functions in Lua lead to powerful OO facilities
Lua - Table elements v table as a whole
Age Concern
Vertical London
Bath, Snake or Nag?
Clean code, jump free (Example in Lua)
1770 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2008: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho