[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: map/grep in Lua: Shedding My Functional Perlisms
- From: Veli-Pekka Tätilä <vtatila@...>
- Date: Mon, 11 Aug 2008 09:34:04 +0300
Javier Guerra Giraldez <firstname.lastname@example.org> wrote:
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.
On Sunday 10 August 2008, Veli-Pekka Tätilä wrote:
Again, Lua does not have all that syntactic sugar to make this very
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
magic embedded in the language's core.
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
PS: in a way the anonymous function syntax of Lua is extremely logical.
Like a function without the name, with the parameter list, return
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
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:
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.
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.
when they found something that's "better" in the old, they try
to "compensate" by writing huge libraries to make up for "missing"
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
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
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.
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
and 'end' feel too big for the line, but written like this:
map (function (e)
makes the verbosity more palatable, and for longer functions the overhead
And now back to Lua coding, for work.
With kind regards Veli-Pekka Tätilä
Accessibility, Apps and Coding plus Synths and Music: