« Module:TableBuilder » : différence entre les versions
Contenu supprimé Contenu ajouté
m orthographe |
Aucun résumé des modifications |
||
Ligne 2 : | Ligne 2 : | ||
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.__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 (function |
return (function(...) return metafunc(t, ...) end) |
||
end |
end |
||
end |
end |
||
local TableBuilder = { |
local TableBuilder = { |
||
new = function() |
new = function() |
||
local t = { |
local t = {} |
||
setmetatable(t, meta) |
setmetatable(t, meta) |
||
return t |
return t |
||
end |
end |
||
} |
} |
||
return TableBuilder |
return TableBuilder |
Version du 31 août 2013 à 12:05
--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
local TableBuilder = {
new = function()
local t = {}
setmetatable(t, meta)
return t
end
}
return TableBuilder