Module:Utilitaire

 Documentation
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 countend--[[Nouveau tableau sans le premier élément--]]function utils.tail(list)    return { select(2, unpack(list)) }endfunction utils.tableConcat(t1,t2)    for i=1,#t2 do        t1[#t1+1] = t2[i]    end    return t1end-- trouve l’index d’une valeur si présente dans la table (index numérique)function utils.table_ifind(t, v)for i, val in ipairs(t) doif v == val thenreturn iendendreturn nilend-- trouve l’index d’une valeur si présente dans la table, index numérique ou chainefunction utils.table_find(t, v)for i, val in pairs(t) doif v == val thenreturn iendendreturn nilend--[[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 instancesSet.__index = Set -- failed table lookups on the instances should fallback to the class table, to get methodsfunction 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 objendfunction Set:is_in(key)    return self.prop_set[key] ~= nilendutils.Set = Set-- table.filter({"a", "b", "c", "d"}, function(o, k, i) return o >= "c" end)  --> {"c","d"}---- @FGRibreau - Francois-Guillaume Ribreau-- @Redsmin - A full-feature client for Redis http://redsmin.com-- https://gist.github.com/fnchooft/77779d80d6668e8eeb3043dad215575alocal function filter (t, filterIter)  local out = {}  for k, v in pairs(t) do    if filterIter(v, k, t) then out[k] = v end  end  return outendutils.filter = filter --[[Programmation fonctionnelle, application d'une fonction sur chaque élément d'un tableaumap(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_arrayendutils.map = mapfunction utils.formatTableWithLastSep(vector, sep, lastsep)local descr = table.concat(vector, sep, 1, #vector-1)if #vector > 1 thendescr = descr .. lastsep .. vector[#vector]else descr = vector[1]endreturn descrendlocal function dump_to_console(val, indent)indent = indent or ""if type(val) == "table" thenfor a, b in pairs(val) domw.log(indent .. a .. "=>")dump_to_console(b, indent .. "   ") endelsemw.log(indent .. tostring(val))endendutils.dump_to_console = dump_to_consoleutils.dump = dump_to_console-- some functions useful in lualocal function splitStr(val) -- transforme en table les chaînes venant du Wikitexte qui utilisent des virgules de séparatinif type(val) == 'string' thenval = mw.text.split(val, ",")endreturn valendutils.splitStr = splitStr-- utility : stack manipulation functions-- Console tests : -- plop = {} ; p.append(plop, "a") ; p.append(plop, "a") ; p.push(plop, "b")  ; p.append(plop, "c") ; p.push(plop, "a") ; mw.log(p. dump_to_console(plop)) ; p.shove_off(plop) ; p.pop(plop) ; mw.log(plop)local function pop(list)local ind=1if list[0] thenind = 0endlocal elem = list[ind]table.remove(list, ind)return elem, listendutils.pop = poplocal function push(list, elem)if elem[0] thentable.insert(list, 0, elem)elsetable.insert(list, 1, elem)endendutils.push = pushutils.append = function(list, elem)table.insert(list, #list+1, elem)endutils.shove_off = function(list, elem)table.remove(list, elem, #list+1)endutils.remove_last = utils.shove_offreturn utils