« Module:TableBuilder » : différence entre les versions
Aller à la navigation
Aller à la recherche
ajout de minsert, tinsert et set |
set et tinsert : + vérification du type ldes 'élément fournit. |
||
| Ligne 3 : | Ligne 3 : | ||
local meta = {} | local meta = {} | ||
meta.insert = function(t, ...) | meta.insert = function ( t, ... ) | ||
table.insert(t, ...) | table.insert( t, ... ) | ||
return t | return t | ||
end | end | ||
meta.remove = function(t, ...) | meta.remove = function ( t, ... ) | ||
table.remove(t, ...) | table.remove( t, ... ) | ||
return t | return t | ||
end | end | ||
meta.sort = function(t, ...) | meta.sort = function ( t, ... ) | ||
table.sort(t, ...) | table.sort( t, ... ) | ||
return t | return t | ||
end | end | ||
meta.maxn = function(t) | meta.maxn = function ( t ) | ||
return table.maxn(t) | return table.maxn( t ) | ||
end | end | ||
meta.concat = function(t, ...) | meta.concat = function ( t, ... ) | ||
return table.concat(t, ...) | return table.concat( t, ... ) | ||
end | end | ||
meta.__index = function(t, key) | meta.__index = function ( t, key ) | ||
local metafunc = meta[key] | local metafunc = meta[key] | ||
if type(metafunc) == 'function' then | if type( metafunc ) == 'function' then | ||
return | return function ( ... ) return metafunc( t, ... ) end | ||
end | end | ||
end | end | ||
| Ligne 45 : | Ligne 45 : | ||
meta.tinsert = function( t, tab, first, last ) | meta.tinsert = function( t, tab, first, last ) | ||
for i = ( first or 1 ), ( last or #tab ) do | if type( tab ) == 'table' then | ||
for i = ( tonumber( first ) or 1 ), ( tonumber( last ) or #tab ) do | |||
table.insert( t, tab[i] ) | |||
end | |||
end | end | ||
return t | return t | ||
| Ligne 59 : | Ligne 61 : | ||
set = function( t ) | set = function( t ) | ||
setmetatable( t, meta ) | if type( t ) == 'table' then | ||
setmetatable( t, meta ) | |||
end | |||
return t | return t | ||
end | end | ||
Version du 4 septembre 2013 à 10:31
--Module appliquant aux fonctions de la librairie Table une inteface fluide.
local meta = {}
meta.insert = function ( t, ... )
table.insert( t, ... ) return t
end
meta.remove = function ( t, ... )
table.remove( t, ... ) return t
end
meta.sort = function ( t, ... )
table.sort( t, ... ) return t
end
meta.maxn = function ( t )
return table.maxn( t )
end
meta.concat = function ( t, ... )
return table.concat( t, ... )
end
meta.__index = function ( t, key )
local metafunc = meta[key]
if type( metafunc ) == 'function' then
return function ( ... ) return metafunc( t, ... ) end
end
end
meta.minsert = function( t, ... )
local sel
for i = 1, select( '#', ... ) do
sel = select( i, ... )
if sel then
table.insert( t, sel )
end
end
return t
end
meta.tinsert = function( t, tab, first, last )
if type( tab ) == 'table' then
for i = ( tonumber( first ) or 1 ), ( tonumber( last ) or #tab ) do
table.insert( t, tab[i] )
end
end
return t
end
local TableBuilder = {
new = function( ... )
local t = { ... }
setmetatable( t, meta )
return t
end,
set = function( t )
if type( t ) == 'table' then
setmetatable( t, meta )
end
return t
end
}
return TableBuilder