lua-users home
lua-l archive

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



On Mar 21, 2005, at 17:13, Javier Guerra wrote:

What would be the proper syntax then?

my mistake, use 'in' instead of '=' (that's a mistake i repeat all the time)

Thanks :)

One way or another, I need to read more about this fabled generic for
loop.

you won't regret it. iterators are a great way to traverse anything, specially when done with coroutines (i think they're called 'generators' in that case?)

Yes. I see. I remember now why I don't use the generic for loop. It requires a function. That function is then invoked for each iteration by the loop itself. In my case, anInputStream.read() return the results themselves. So to use the generic for loop I would need the rewrite my loop like this:

for aType, aContent, aName, someAttributes in anInputStream.read

Note the lack of parenthesis. This is a gratuitous deviation from how I invoked functions: always use parenthesizes.

On the other hand, I would gladly use the generic loop if I could write it like this:

for aType, aContent, aName, someAttributes in anInputStream.read()

Note the explicit parenthesis. I favor consistency over magic :)

it's not an optimization, it's more a mindframe thing. in low level languages you mostly choose your algorithms, but in higher level it's important to see
what the language offers you and follow the path of least resistance.

Agree. That said, Lua itself allow many variation on the same theme. Which you may view as either beneficial or detrimental. I think too many variation in how to express the exact same thing is a liability overall. How many "for" construct does a language needs? One seems to be plenty enough.

.... unless you want a good fight! ;-)

I'm careful avoiding any such fight :)

yep. and there are lots of things done best with an object (i think a XML pull
parser IS one of them)

personally, i do use object-like constructs in Lua all the time. i like the
obj:func() form,

I found the colon notation too much of a good thing and I'm not using it at all. I favor the plain dot instead. That way, there is no additional confusion: dot or colon. The dot it is always :)

 and since i seldom use any inheritance, i haven't find the need
for a full OOP scheme in Lua.

Well, even my LU concoction is far from a full blown OO system. It simply boil down to a packaging system to bundle named functions in tables. All in all, its main benefit is to provide an organizational principal. The OO benefit, as I see it, being mostly polyphormism instead of hard core inheritance.

Even in C++, the data-managing parts of my programs are very shallow class hierarchies. the GUI parts do get deep... but mostly because of the framework i
happen to use.

But frankly speaking, I haven't seen a great deal of reuse in Lua. At
all. Quite a let down, if you ask me. And here I am writing a dumb
little function to decode XML strings. Duh.

very true. i think code reuse is more at a module level than at object or
function level.

Well, my so called "objects" are just plain table of functions. It's more a packaging scheme than anything else.

and at the copy-paste level, of course!

Oh, my... :P

Because I'm not sure what this fabled "Lua way" is. Concrete suggestion
warmly welcome :)

rici's example is a nice one (much better than mine!)
concrete 'luaisms' used on his decoder:

- wrap a small module on a do...end block, so a single function is exported (decode()), but it has access to local vars (ents[] and maxutf8) and functions
(entity2char())
- use a lookup table
- use a standard function (gsub()), and give it another function as a parameter
- use 'str and gsub(....)' idiom to check parameters before using them

Yes. I pretty much follow such idioms.

For example, I use anonymous functions quite extensively. As an illustration, in my HTTP server, a service accepts an anonymous function which acts as an authenticator, by returning true or false for a given context:

local anAuthenticator = function( aContext )
if ( aContext.request().headers().get( "authorization" ) ~= nil ) then
	    return true
    end

    return false
end

http://dev.alt.textdrive.com/file/LW/Test.lua

Such function is in turn used when the service has to decide to give access to a given resource:

if ( ( anAuthenticator == nil ) or ( anAuthenticator( aContext ) == true ) ) then
    this[ aMethod ]( aTask, aContext )
else
local aChallenge = string.format( "Basic realm=\"%s\"", aContext.session().realm() )

    aResponse.headers().put( "www-authenticate", aChallenge )
    aResponse.writeStatus( 401 )
end

http://dev.alt.textdrive.com/file/LW/LWService.lua

......
local aValue = thisClass.entities().get( anEntity )
........
Is that Lua-esque enough? :))

is that line a lookup table?

Yes.

if so, why not use tables???

I do. Indirectly. I have a wrapper module/package/library/object/whatever around Lua tables which provides commonly used functionalities:

http://dev.alt.textdrive.com/file/lu/LUMap.lua

Getting to this lookup table involves a couple of steps:

(1) local aBundle = LUBundle.bundleWithName( "LUXMLInputStream" )
(2) local aFile = LUFile.new( aBundle.path(), "LUXMLInputStream.txt" )
(3) cvars.entities = LUMap.new().load( aFile )

(1) locate where the code is
(2) get the relative path to the specific resource
(3) load the table

By itself, the underlying Lua code is not much, but I use such functionality all over the place. And I for once don't enjoy copy & paste programming much :P So the code is centralized and reused if possible :)

http://dev.alt.textdrive.com/file/lu/LUBundle.lua
http://dev.alt.textdrive.com/file/lu/LUFile.lua

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/