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

Une page de Wikipédia, l'encyclopédie libre.
Contenu supprimé Contenu ajouté
TomT0m (discuter | contributions)
Aucun résumé des modifications
TomT0m (discuter | contributions)
Aucun résumé des modifications
Ligne 15 : Ligne 15 :


local Set = {} -- the table representing the class, which will double as the metatable for the instances
local Set = {} -- the table representing the class, which will double as the metatable for the instances
Set.__index = PropClass -- failed table lookups on the instances should fallback to the class table, to get methods
Set.__index = Set -- failed table lookups on the instances should fallback to the class table, to get methods


function Set:new(init, o)
function Set:new(init, o)
o = {}
o = {}
setmetatable(o, PropClass)
setmetatable(o, Set)
o.value = init
o.value = init

Version du 15 août 2015 à 15:11

 Documentation[créer] [purger]
local utils = {}

function utils.tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end


--[[

Une classe destinée à servir d'ensemble pour tester rapidement l'appartenance d'un élément à une liste ou un ensemble
--]]

local Set = {} -- the table representing the class, which will double as the metatable for the instances
Set.__index = Set -- failed table lookups on the instances should fallback to the class table, to get methods

function Set:new(init, o)
    o = {} 
    setmetatable(o, Set)
    
    o.value = init
    o.prop_set = {}
    
    for _, val in pairs(init) do
    	o.prop_set[val] = true
    end

    return o
end

function Set:is_in(key)
    return self.prop_set[key] ~= nil
end

utils.Set = Set

--[[
	Programmation fonctionnelle, application d'une fonction sur chaque élément d'un tableau
	map(f,{a, b, c, ...}) = {f(a), f(b), f(c), ...} 
--]]


local function map(func, array)
  local new_array = {}
  for i,v in ipairs(array) do
    new_array[i] = func(v)
  end
  return new_array
end

utils.map = map

function utils.formatTableWithLastSep(vector, sep, lastsep)
	local descr = table.concat(vector, sep, 1, #vector-1)
	if #vector > 1 then
		descr = descr .. lastsep .. vector[#vector]
	else 
		descr = vector[1]
	end
	return descr
end


return utils