lua-users home
lua-l archive

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


On Fri, Jul 18, 2008 at 2:32 AM, Shmuel Zeigerman <shmuz@bezeqint.net> wrote:
> Alexander Gladysh wrote:
>>
>> print(
>>    extract_keys(
>>        { a = 1, b = 2, c = 3 },
>>        "a", "b", "xxx", "c", "yyy"
>>      )
>>  )
>>  --> 1        2        nil         3        nil
>
> An option is using recursive functions:
>
> -- "mapper" is a table:
> function map(tab, ...)
>  if select("#", ...) > 0 then
>    return tab[...], map(tab, select(2,...))
>  end
> end
>
> -- "mapper" is a function:
> function map(func, ...)
>  if select("#", ...) > 0 then
>    return func(select(1,...)), map(func, select(2,...))
>  end
> end

Brilliant! Thank you.

I wonder, however, how much slower it would be against the generated
code version... It is much shorter anyway, so, for my reasons, -- is
better. (If I needed faster version *that much*, I'd use C.)

Alexander.