lua-users home
lua-l archive

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

on 9/1/05 6:19 AM, Wim Couwenberg at wrote:

> the ipairs generator will stop at the first nil.  To safely iterate over
> all arguments use:
>    for i = 1, select("#", ...) do
> local a = select(i, ...)
>        -- etc...
>    end

This takes time that is quadratic in the number of elements though the
proportionality constant is probably low.

>    local arg = {...}
>    for i = 1, select("#", ...) do
> local a = arg[i]
>        -- etc...
>    end

This takes time linear in the number of elements but it has a higher
constant term and possibly proportionality factor to build the table.

I've been contemplating whether Lua needs:

    function argforeach( fn, ... )
        for I = 1, select( "#", ... ) do
            fn( ( select( I, ... ) ) )

This should be written in C to avoid the performance issues cited above and
probably needs a better name.

An alternative would be to extend the syntax to define:

    for i, v in ... Do
        -- do stuff with the i-th element of ...

So that it implements the appropriate loop. This means that if you want to
pass iteration parameters via ..., then you will need to bind them to locals
outside of the loop or use select( 1, ... ) in the loop.


P.S. The absence of standard support for packing and unpacking argument
lists containing nil is an annoyance.