lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sun, 9 Feb 2014 22:37:08 +0530
rakesh sharma <rakeshsharma14@hotmail.com> wrote:

> Hi ,
> I am new to lua.Any pointers to how I can start with, I have only one
> lua reference pdf, from the lua official website thanks,rakesh

I wrote some material here:

http://www.troubleshooters.com/codecorn/lua/index.htm	 

The rest of my reply isn't reference material, but it's some things I
wish someone would have told me about Lua years ago...

Become acutely conscious that Lua's single complex data structure is the
table, a bunch of key->value pairs, just like Perl hashes or
Pythondicts. But Lua tables' lookup by key is so efficient that its
performance is roughly equivalent to arrays in other languages. Widen
your thoughts to ponder all the things you could do with a bunch of
key->value pairs not limited by normal efficiency matters. In other
words, you can make a useful array of it just by making the keys
consecutive numbers, which is why Lua doesn't have arrays outside of
tables with numeric keys. You could do the same thing with Perl hashes
or Python dicts, but performance would take a big hit.

Understand the implications of Lua's assigning all tables by reference,
so:

a = mytable
b = a

Any change you make inside the table structure of a, b, or mytable
changes the other two. This is both a detriment to encapsulation, and a
guarantee that you won't have multiple copies of the same entity which
need to be kept in sync. Assignment by reference has advantages and
disadvantages: Code to the advantages.

Become acutely conscious that Lua subroutines/functions are data that
can be passed around via assignment, function args and function returns.
Ponder the power of a table with elements including functions. Under
those circumstances, who needs objects? The Lua buzzword for this is
"first class functions".

Become acutely conscious that Lua functions can declare other functions
inside of them. Unless you've worked with Pascal, this is probably a
foreign notion. Understand that the inner function can access the outer
function's variables (Lua buzzword = upvalue). Smile as you consider
the implications of returning the inner function as the function return
of the outer function, so that the outer function's variables create a
data persistence for each inner function returned and used. This is one
of the main ways that iterator functions are done with Lua.

Rethink Object Oriented Programming. As a salesman would say, think
benefits, not features. Philippe Kahn listed the following three
features of OOP:

1: Encapsulation
2: Inheritance
3: Polymorphism

Lua has no OOP feature. There's no "class" keyword, and no
language-enforced way of instantiating an instance of a blueprint
(class in other languages). But Lua gives you many, many easy ways of
using OOP techniques that offer benefits of encapsulation, inheritance,
and polymorphism.

One more thing: Rejoice in the fact that Lua is like a Data Structures
and Algorithms class rolled into a language. Formerly esoteric concepts
are trivial to accomplish in Lua. Learning Lua turbocharged my
understanding of data structures and algorithms, in all languages.

HTH,

SteveT

Steve Litt                *  http://www.troubleshooters.com/
Troubleshooting Training  *  Human Performance