« Module:TableBuilder » : différence entre les versions

De Lagny-sur-Marne Wiki
Aller à la navigation Aller à la recherche
0x010D (discussion | contributions)
new : la table est crée avec les éléments transmis à la fonction
0x010D (discussion | contributions)
m A protégé « Module:TableBuilder » : Modèle très utilisé : à la demande de son créateur ([Modifier=Autoriser uniquement les administrateurs] (infini) [Renommer=Autoriser uniquement les administrateurs] (infini))
 
(3 versions intermédiaires par le même utilisateur non affichées)
Ligne 1 : Ligne 1 :
--Module appliquant aux fonctions de la librairie Table une inteface fluide.
--Module appliquant aux fonctions de la librairie Table une inteface fluide.


local meta = {}
local meta = {


meta.insert = function(t, ...)
    insert = function ( t, ... )
    table.insert(t, ...)
        table.insert( t, ... )
    return t
        return t
end
    end,
 
   
meta.remove = function(t, ...)
    remove = function ( t, ... )
    table.remove(t, ...)
        table.remove( t, ... )
     return t
        return t
end
    end,
 
      
meta.sort = function(t, ...)
    sort = function ( t, ... )
    table.sort(t, ...)
        table.sort( t, ... )
     return t
        return t
end
    end,
   
    maxn = function ( t )
        return table.maxn( t )
    end,
   
    concat = function ( t, ... )
        return table.concat( t, ... )
    end,
   
    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,
      
    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,


meta.maxn = function(t)
meta.__index = function ( t, key )  
    return table.maxn(t)
end
 
meta.concat = function(t, ...)
    return table.concat(t, ...)
end
 
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 metafunc(t, ...) end)
         return function ( ... ) return metafunc( t, ... ) end
     end
     end
end
end
       
-- fin des fonctions de la meta-table


local TableBuilder = {
local TableBuilder = {
Ligne 38 : Ligne 62 :
         setmetatable( t, meta )
         setmetatable( t, meta )
         return t
         return t
     end
     end,
   
    set = function( t )
        if type( t ) == 'table' then
            setmetatable( t, meta )
        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
        return recursiveClone( t )
    end,
}
}


return TableBuilder
return TableBuilder

Dernière version du 30 septembre 2013 à 19:18

--Module appliquant aux fonctions de la librairie Table une inteface fluide.

local meta = {

   insert = function ( t, ... )
       table.insert( t, ... )
       return t
   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, ... )
       local sel
       for i = 1, select( '#', ... ) do
           sel = select( i, ... )
           if sel then
               table.insert( t, sel )
           end
       end
       return t
   end,
   
   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,

}

meta.__index = function ( t, key )

   local metafunc = meta[key]
   if type( metafunc ) == 'function' then
       return function ( ... ) return metafunc( t, ... ) 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
       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
       return recursiveClone( t )
   end,

}

return TableBuilder