I have presented a bespoke Lua course over the last three days ... and it has lead me to some interesting new examples which I'm sharing here.
Object Orientation is more a way of thinking and coding than a language feature in Lua - when you set up a piece of data / structure / object, you do so in a table, and you include in the table references to the functions that are to be called when you perform certain actions on that table. See
here for a first example (which never the less shares some functionality between different types of data - polymorphism) and
here for an example which also overrides various default methods through a metatable. The data these two sample programs use may be found
here.
Coroutines provide the Lua programmer with the ability to have several functions active at the same time - akin to parallel processing or threading, if you like to think of it that way. A comparison is sometimes made to Python's generators, but with Lua coroutines you can easily have several active at the same time. This is a great way of storing "state" within a function - it saves messy parameter passing and globals, and it also saves really long blocks of code too. If you want an iterator - anything that you might say "give me the next ..." to, then it's probably best to use a coroutine. There's a new example showing coroutine basics
here, and an example in which we have used several co-routines to process different steps in a process
here.
If you open a file in Lua, you'll get a file handle back ... or nil if the file fails to open. But WHY did it fail to open? io.open actually returns a second value if the first one is nil - an error string. There's an example
here.
During our courses, we set a number of practical exercises so that delegates can try things out, calling on the tutor to talk about the best way to design and implement a task if they wish, with regards to code readability and maintainability. Such as exercise is set
here ... calling for the delegates to write a module to meet what is, in essence, a specification and test harness. My own sample answer, written after the delegates had come up with their own answers, is
here
In Lua, functions are truely dynamic - you can load them then overwrite them, and you can even define them within conditionals so that they'll be defined in different ways depending on your data. Powerful but dangerous! There's a sample showing you the mechanism
here ... and we discuss the pros and cons of using these techniques on our
Lua Programming and
Learning to program in Lua courses (the former for people with prior programming experience, the latter for those who have never programmed before).
A
log file analysis program that I wrote about 2/3 of the way through the course pulled together all the common coding features you might use in a shortish piece of code - loading data from a file, extracting information from it, sorting and producing a formatted report.
And a
final example shows how you can split a line of text using string.find and string.sub ... there are other ways (such as string.gfind) which make for shorter code - there's an example of string.gfind
here where it is used for filter email addresses from a flow of data.
Please follow the links on this page to see the various source codes ... please have a look at our
Lua courses if you want to learn more from us. Public courses run in Melksham, Wiltshire, England; if you have a group of people, we'll run a private course for you - either at our centre, or at your office almost anywhere in the world.
(written 2009-10-15, updated 2009-10-17)
Associated topics are indexed under
U103 - Lua - Conditionals and loops [3558] Python or Lua - which should I use / learn? - (2011-12-21)
[3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
[2454] For loops in Lua - (2009-10-14)
[2351] Ternary operators alternatives - Perl and Lua lazy operators - (2009-08-12)
[1738] Clean code, jump free (Example in Lua) - (2008-08-06)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[1685] Short circuit evaluation (lazy operator) in Lua - (2008-06-22)
U106 - Lua - Functions. [2949] Lua - variable function arguments and select - (2010-09-08)
[2723] Returning multiple values from a function - Lua - (2010-04-15)
[2356] Passing multiple results back from a function - Lua, Python, etc - (2009-08-13)
[2347] Dot, dot, dot in Lua - variable length parameter lists - (2009-08-11)
[2312] What are closures in Lua? - (2009-07-31)
[1847] Lua - IAQ (Infrequently Answered Questions) - (2008-10-18)
[1744] Lua examples, Lua Courses - (2008-08-08)
[1743] First class functions in Lua lead to powerful OO facilities - (2008-08-07)
[1699] If you are learning Lua, here are some more examples - (2008-07-06)
[1697] Sorting in lua - specifying your own sort routine - (2008-07-05)
U107 - Object Orientation - the Lua way [3524] Metaclasses (Python) and Metatables (Lua) - (2011-11-17)
[3396] Tables as Objects in Lua - a gentle introduction to data driven programming - (2011-08-17)
[3142] Private and Public - and things between - (2011-01-22)
[2710] __index and __newindex in Lua - metatable methods - (2010-04-05)
[2703] Lua Metatables - (2010-04-02)
[2701] Is Lua an Object Oriented language? - (2010-04-01)
[2359] A fresh example - objects the Lua way - (2009-08-13)
[2318] For Lua Programmers AND for Town Planners - (2009-08-02)
[1819] Calling base class constructors - (2008-10-03)
[1692] Towards Object Oriented Programming in Lua - (2008-06-30)
U109 - Lua - Modules [2358] Encapsulating shared code in a Lua module - (2009-08-13)
U111 - Lua - Trapping and handling errors. [2706] Error trapping in Lua - no exceptions. - (2010-04-04)
[2360] Error Handling in Lua with assert and pcall - (2009-08-13)
U114 - Lua - Threading and Coroutines. [3395] Parallel but not really parallel. Moving game characters. Coroutines in Lua. - (2011-08-17)
[2314] Passing parameters to a coroutine in Lua - (2009-08-01)
[1870] What to do with a huge crop of apples - (2008-11-04)
[1691] Co-routines in Lua - co-operative processing - (2008-06-29)
Some other Articles
New Web Site for Melksham Chamber of CommerceCant connect to local MySQL server through socket /tmp/mysql.sockRailway ArithmeticBright EyesLua examples - coroutines, error handling, objects, etcDark to darkMaking executable binaries in Python (or Perl)Gypsy saysFamily Gathering at 404, The Spa