For faster navigation, this Iframe is preloading the Wikiwand page for Module:Utilitaire.

Module:Utilitaire

 Documentation[créer] [purger]

Voir les statistiques d'utilisation du module sur l'outil wstat.

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


-- 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/77779d80d6668e8eeb3043dad215575a

local 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 out
end

utils.filter = filter 

--[[
	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
utils.dump = dump_to_console

-- some functions useful in lua


local function splitStr(val) -- transforme en table les chaînes venant du Wikitexte qui utilisent des virgules de séparatin
	if type(val) == 'string' then
		val = mw.text.split(val, ",")
	end
	return val
end

utils.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=1
	if list[0] then
		ind = 0
	end
	local elem = list[ind]
	table.remove(list, ind)
	return elem, list
end

utils.pop = pop

local function push(list, elem)
	if elem[0] then
		table.insert(list, 0, elem)
	else
		table.insert(list, 1, elem)
	end
end

utils.push = push


utils.append = function(list, elem)
	table.insert(list, #list+1, elem)
end

utils.shove_off = function(list, elem)
	table.remove(list, elem, #list+1)
end
utils.remove_last = utils.shove_off

return utils
{{bottomLinkPreText}} {{bottomLinkText}}
Module:Utilitaire
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.