lua-users home
lua-l archive

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


I thought I would add the code I envisioned for from(), which probably matches something on the lua-users wiki but I can't find the page...

local from =
    function (t, ...)
        local collected = {} 

        for _, k in ipairs({ ... }) do 
            table.insert(collected, t[k])
        end

        return table.unpack(collected)
    end

local tins, trem, tcat = from(table, 'insert', 'remove', 'concat')

The Power Patches page is cool ;]

PS:  Just want to leave a note saying that I don't like from() -- as I said earlier, it encourages users to make smaller identifiers so they don't have to write each one twice (once after local and again in from)



On Sun, Nov 17, 2013 at 9:03 AM, Paige DePol <lual@serfnet.org> wrote:
Rena, you should check out the "Unpack Tables by Name" patch over on the Power Patches page (http://lua-users.org/wiki/LuaPowerPatches)

I have not patched my Lua with this patch yet, however, it looks like it would do exactly what you desire; namely let you assign locals to the result of table key lookups in a concise manner!

>From the Power Patches page:

Enhancement to the assignment statement to unpack named values from tables using the in keyword:
    local a, b, c in some_table_expression

Is syntactic sugar for:
    local t = some_table_expression
    local a, b, c = t.a, t.b, t.c

~pmd~



On Nov 17, 2013, at 2:37 AM, Rena <hyperhacker@gmail.com> wrote:

> But the point to understand is that this must be a purely _static_
> operation at compile time. So implementing something like 'using
> table.*' hits the problem of determining the contents of 'table' at
> compile time.  So it would have to be an explicit list of entries to
> localize, and understood as syntactical sugar for all those pesky
> 'local insert = table.insert' statements.