lua-users home
lua-l archive

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


Javier Guerra Giraldez <javier@guerrag.com> wrote:
On Sunday 10 August 2008, Veli-Pekka Tätilä wrote:
Again, Lua does not have all that syntactic sugar to make this very useful.
I don't think it should, either.
IMO, that's the whole point.  a big design choice in both Perl and Python
(don't know about Ruby) is making common idioms very easy to type and read.
the cost is not only lots of syntactic sugar; in some cases it downright deep
magic embedded in the language's core.
Agreed, perl is even famous for that, TIMTOWTDI. For example, the example I showed for map only works for map and grep. you cannot use an expression as a callback function in any user subs. Ruby is extremely programmer convenient and hugely object-oriented in a way that I happen to like, but not everyone might.

http://vtatila.kapsi.fi/luatut.html
PS: in a way the anonymous function syntax of Lua is extremely logical.
Like a function without the name, with the parameter list, return statement
and all.
one pet peeve of mine is how so many developers approach learning new
languages: trying to understand the new language in terms of the old one
True in general, I had that problem when trying to learn C++ after Java. In this case, though, there are lots of hash idioms you can directly lift from Perl to be used equaly validly in Lua. And I find the OOp systems of Perl 5 and LUa rather similar, except that Lua needs much less magic and is much more regular. TAbles being hashes is just great, I hate typeglobs, and always have.

I wrote it for Perl coders to save time, time that would have been spend explaining lots of idioms that are not particular to Lua, strictly speaking. The stuff on regexp and hashes are good examples of that.

The probs I had learning LUa were coroutines and metatables. Once I understood coroutines were about freezing subroutine execution while jumping to the caller, as opposed to Javaish threads, it all made sense. Similarly, the term metatable didn't resonate with me. When I realized what you are doing is determining operator overloading for a particular table, including indexing for inheritance, it suddenly made a lot more sense. Using the class as the metatable for an object, complete with operator overloading, made me understand how things work. I tend to do in a module:

__index = _M

in my class file and later:

setmetatable(o, self)

in a constructor.

Lua metatables are great, though. In Perl I need tie, overload, bless, typeglobs, AUTOLOAD, ISa and a lot of other language and library magic where as in Lua I can use metatables very orthogonally for all these.

when they found something that's "better" in the old, they try
to "compensate" by writing huge libraries to make up for "missing" features.
Yes, I made that mistake for a handful of funcs. They were easy to write, but still. You mentioned LIsp. I've never really programmed it but having read higher-order Perl, most of the functional stuff in Lua was easy.

after learning Lua, i could understand a lot more of Scheme,
Interesting, what bits of Scheme did Lua make clearer?

personally, i think Perl and Python are too magic-heavy to be ideal reference points.
For a Lua tutorial, probably yeah. I only mention the magic as it relates to Lua, and don't spend much time saying haha, this won't work in Lua. The lack of push irked me quite a bit, initially. But now I deeply appreciate the minimalist nature of the Lua standard library. I can effortlessly memorize large bits of it by heart, not having to look up docs.

One thing I still wonder about is serialization. Why doesn't Lua have it at the library level since the need is so common? Sure you can code your own, but that's duplicated effort.

and, about your complain: what i've found is that anonymous functions are very
neat as function parameters, even when you can't reasonably fit them in
one-liners. certainly in "map (function(e) return e end, list)" the 'return'
and 'end' feel too big for the line, but written like this:

map (function (e)
return e
end, list)

makes the verbosity more palatable, and for longer functions the overhead
becomes unimportant.
fully agreed here. I've used that thing a bit in Lua, for longer anonymous functions such as metatble methods, and replacing existing functions with some new additions such as logging or pre and post conditions. I often end up doing a simlar thing in Ruby, too, to keep things readable.

And now back to Lua coding, for work.


--
With kind regards Veli-Pekka Tätilä
Accessibility, Apps and Coding plus Synths and Music:
http://vtatila.kapsi.fi