lua-users home
lua-l archive

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


Am 16.12.2012 14:02, schrieb steve donovan:
On Fri, Dec 14, 2012 at 2:01 PM, Michal Kottman <k0mpjut0r@gmail.com> wrote:
1) we have a table and want to add/override fields from another table -
borrowing from jQuery, let's call it extend
2) merge all fields into a new table - easily implemented using the first
Since the functions are so short, maybe they deserve the inclusion into
Microlight? Or has the "top 30" limit been already reached? :)

(1) already exists in Microlight as import  (for map-like) and extend
(for list-like), although they don't take n tables.

import() is one of those strangely named - one of its jobs is to pull
a module's functions into a convenient namespace. (Still think it
_should_ be called update as in Python)

Making these guys work with n-tables is a good idea, although my
over-optimizing side worries about the table creation needed to access
varargs.

Am I missing something here?

function extend( t, ... )
  for i = 1, select( '#', ... ) do
    local a = select( i, ... )
    if type( a ) == "table" then
      for k,v in pairs( a ) do
        t[ k ] = v
      end
    end
  end
  return t
end


The solution there, of course, is to optimize the two-table
case and only fall back to the loop for n > 2

Illustrates an entertaining property of Lua libraries - even their
authors can't always remember their whole functionality ;)

steve d.


Philipp