« Module:Documentation » et « Module:TableBuilder » : différence entre les pages
(Différence entre les pages)
Aller à la navigation
Aller à la recherche
m
1 version
imported>T12 m (1 version) |
imported>T12 m (1 version) |
||
Ligne 1 : | Ligne 1 : | ||
-- | --Module appliquant aux fonctions de la librairie Table une inteface fluide. | ||
local | local meta = { | ||
insert = function ( t, ... ) | |||
table.insert( t, ... ) | |||
return t | |||
end, | |||
end | |||
remove = function ( t, ... ) | |||
table.remove( t, ... ) | |||
return t | |||
end, | |||
sort = function ( t, ... ) | |||
table.sort( t, ... ) | |||
return t | |||
end, | |||
maxn = function ( t ) | |||
return table.maxn( t ) | |||
end, | |||
concat = function ( t, ... ) | |||
return table.concat( t, ... ) | |||
end, | |||
minsert = function( t, ... ) | |||
return | local sel | ||
end | for i = 1, select( '#', ... ) do | ||
sel = select( i, ... ) | |||
if sel then | |||
function | table.insert( t, sel ) | ||
end | |||
end | |||
end | return t | ||
end, | |||
function | |||
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, | |||
} | |||
tab | |||
meta.__index = function ( t, key ) | |||
local metafunc = meta[key] | |||
if type( metafunc ) == 'function' then | |||
return function ( ... ) return metafunc( t, ... ) end | |||
end | end | ||
end | end | ||
-- fin des fonctions de la meta-table | |||
local TableBuilder = { | |||
new = function( ... ) | |||
local t = { ... } | |||
setmetatable( t, meta ) | |||
return t | |||
end, | |||
set = function( t ) | |||
if type( t ) == 'table' then | |||
setmetatable( t, meta ) | |||
end | end | ||
return t | |||
end, | |||
clone = function ( t ) | |||
local tableRefs = { } | |||
local function recursiveClone( val ) | |||
if type( val ) == 'table' then | |||
-- Encode circular references correctly | |||
if tableRefs[val] ~= nil then | |||
return tableRefs[val] | |||
end | |||
local retVal = { } | |||
tableRefs[val] = setmetatable( retVal, meta ) | |||
for key, elt in pairs( val ) do | |||
retVal[key] = recursiveClone( elt ) | |||
end | |||
return retVal | |||
else | |||
return val | |||
end | |||
end | end | ||
return recursiveClone( t ) | |||
end, | |||
} | |||
return | return TableBuilder |