Differenze tra le versioni di "Modulo:Wikilib/tables"

Added recursiveMerge, fold, any, all and slice *** sovrascritto il testo esistente ***
m (Bug latenti (speriamo di aver finito la catena discendente))
(Added recursiveMerge, fold, any, all and slice *** sovrascritto il testo esistente ***)
t.tableKeysAlias, t.table_keys_alias, t.keysAlias, t.keys_alias =
table.tableKeysAlias, table.tableKeysAlias, table.tableKeysAlias, table.tableKeysAlias
 
--[[
 
Classic fold on lists: applies a binary
function to an accumulator and a value-key
pair taken from the list and returns the
final result. The iterator determines the
order elements are scanned, and therefore
the final result.
 
The function takes the accumulator as the
first argument, then the value and finally
the key; usual key-value order is reversed
because the key is often unnecessary, and
having it last leads to nicer syntax in callers.
 
--]]
table.fold = function(tab, zero, func, iter)
iter = iter or pairs
 
for key, value in iter(tab) do
zero = func(zero, value, key)
end
 
return zero
end
 
t.fold = table.fold
 
--[[
= table.mapToNum, table.mapToNum, table.mapToNum,
table.mapToNum
 
--[[
 
Returns true if at least one of the
elements in the list satisties a
predicate.
 
The predicate function takes the value as the
first argument, then the key; usual key-value
order is reversed because the key is often
unnecessary, and having it last leads to nicer
syntax in callers.
 
--]]
table.any = function(tab, funct, iter)
return table.fold(tab, false,
function(acc, value, key)
return funct(value, key) or acc
end, iter)
end
 
t.any = table.any
 
--[[
 
Returns true if all of the elements
in the list satisties a predicate.
 
The predicate function takes the value as the
first argument, then the key; usual key-value
order is reversed because the key is often
unnecessary, and having it last leads to nicer
syntax in callers.
 
--]]
table.all = function(tab, funct, iter)
return table.fold(tab, true,
function(acc, value, key)
return funct(valye, key) and acc
end, iter)
end
 
t.all = table.all
 
--[[
seguiranno quelli della prima; gli altri sono
lasciati invariati, ed in caso di uguaglianza
quelli della seconda sovrascrivono quellii valori della
prima.
 
 
local dest = mw.clone(tab1)
 
--[[
È necessario il doppio ciclo per avere
end
for key, value in table.nonIntPairs(tab2) do
dest[key] = value
end
return dest
 
t.merge = table.merge
 
--[[
 
Ritorna una table risultato della fusione delle
due passate. Gli indici numerici della seconda
seguiranno quelli della prima; gli altri sono
lasciati invariati, ed in caso di uguaglianza
quelli della seconda sovrascrivono i valori della
prima. Fanno però eccezione le tables, che sono
fuse a loro volta.
 
--]]
table.recursiveMerge = function(tab1, tab2)
 
local dest = mw.clone(tab1)
--[[
È necessario il doppio ciclo per avere
le chiavi intere in ordine
--]]
for key, value in ipairs(tab2) do
table.insert(dest, value)
end
for key, value in table.nonIntPairs(tab2) do
if dest[key]
and type(dest[key]) == 'table'
and type(value) == 'table'
then
dest[key] = table.merge(dest[key], value)
else
dest[key] = value
end
end
return dest
end
 
table.recursive_merge = table.recursiveMerge
t.recursive_merge, t.recursive_merge =
table.recursiveMerge, table.recursiveMerge
 
--[[
 
t.keys = table.keys
 
--[[
 
Given a table with numeric indexes,
returns another table containing its
elements in the specified indexes
range. End index defaults to largest
index in the table.
 
--]]
table.slice = function(tab, from, to)
to = to or #tab
return table.filter(tab, function(_, key)
return key >= from and key <= to
end, ipairs)
end
 
t.slice = table.slice
 
--[[
106 665

contributi