Dokumentācijas ikona Moduļa dokumentācija[izveidot]
local utils = {}

--[[

Nombre d'élément dans un dictionnaire

--]]

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


--[[

Nouveau tableau sans le premier élément

--]]

function utils.tail(list)
    return { select(2, unpack(list)) }
end


function utils.tableConcat(t1,t2)
    for i=1,#t2 do
        t1[#t1+1] = t2[i]
    end
    return t1
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)
    local obj = o or {} 
    setmetatable(obj, self)
    
    obj.value = init
    obj.prop_set = {}
    
    for _, val in pairs(init) do
    	obj.prop_set[val] = true
    end

    return obj
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

local function dump_to_console(val, indent)
	indent = indent or ""
	if type(val) == "table" then
		for a, b in pairs(val) do
			mw.log(indent .. a .. "=>")
			dump_to_console(b, indent .. "   ") 
		end
	else
		mw.log(indent .. tostring(val))
	end
end

utils.dump_to_console = dump_to_console

return utils