17 214
contributi
(Bugfix *** sovrascritto il testo esistente ***) |
(Adding flatMap) |
||
-- luacheck: new_globals table
-- Library table functions
local t = {}
-- Stateless iterator
local nextNonInt = function(tab, key)
end
local next_non_int = nextNonInt -- luacheck: no unused
-- Stateless iterator
table.nonIntPairs = function(tab)
end
table.non_int_pairs = t.nonIntPairs
t.nonIntPairs, t.non_int_pairs =
-- Returns true only if a is lexigographically greater than b
local minor = function(a, b)
end
table.search = function(tab, value)
for k, v in pairs(tab) do
end
-- No explicit return, thus nil
table.linearSearch, table.linear_search =
t.search, t.linearSearch, t.linear_search =
--[[
--]]
table.deepSearch = function(tab, value)
end
end
table.deep_search, table.recursiveSearch, table.recursive_search =
t.deepSearch, t.deep_search, t.recursiveSearch, t.recursive_search =
--[[
--]]
table.getn = function(self, count)
local iterator
end
--]]
table.tableKeysAlias = function(tab, source, dest)
end
table.table_keys_alias, table.keysAlias, table.keys_alias =
t.tableKeysAlias, t.table_keys_alias, t.keysAlias, t.keys_alias =
--[[
--]]
table.map = function(tab, funct, iter)
end
t.map = table.map
--[[
Applies a function over all of the elements of a table returned by the passed
iterator. The function is expected to return a list. The lists produced from
the elements are merged together in the same way as table.merge would do. The
iterator defaults to pairs.
The function takes as input an element and its ke, in this order: in fact, the
key is often unnecessary, and would just clutter the code in these cases.
--]]
table.flatMap = function(tab, funct, iter)
iter = iter or pairs
local dest = {}
for k, v in iter(tab) do
dest = table.merge(dest, funct(v, k))
end
return dest
end
table.flat_map = table.flatMap
t.flatMap, t.fat_map = table.flatMap, table.flatMap
--[[
--]]
table.mapToNum = function(tab, funct, iter)
end
table.map_to_num, table.mapToNumeric, table.map_to_numeric
t.mapToNum, t.map_to_num, t.mapToNumeric, t.map_to_numeric
--[[
Applies a function over all of the elements of a table returned by the passed
iterator. The function is expected to return a list. The lists produced from
the elements are converted to numeric lists: the order of the elements is
determined by the given iterator. These lists are then concatenated together,
in the order the elements of the initial list are returned by the iterator.
The function takes as input an element and its ke, in this order: in fact, the
key is often unnecessary, and would just clutter the code in these cases.
--]]
table.flatMapToNum = function(tab, funct, iter)
iter = iter or pairs
local dest = {}
for k, v in iter(tab) do
for _, value in iter(funct(v, k)) do
table.insert(dest, value)
end
end
return dest
end
table.flat_map_to_num, table.flatMapToNumeric, table.flat_map_to_numeric
= table.flatMapToNum, table.flatMapToNum, table.flatMapToNum
t.flatMapToNum, t.flat_map_to_num, t.flatMapToNumeric, t.flat_map_to_numeric
= table.flatMapToNum, table.flatMapToNum, table.flatMapToNum,
table.flatMapToNum
--[[
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 the call site.
--]]
--[[
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 the call site.
--]]
return table.fold(tab, true,
function(acc, value, key)
return funct(
end, iter)
end
table.equal = function(tab1, tab2)
--[[
Se il numero di elementi è diverso le due
tables non possono essere uguali. Inoltre
gestisce anche il caso in cui tab1 sia vuota
e tab2 no, che porterebbe a tornare true
poiché il loop viene skippato, e quello in
cui tab1 sia un sottoinsieme di tab2 poiché
si controllano solo gli indici della prima.
--]]
if table.getn(tab1) ~= table.getn(tab2) then
return false
end
for key, value in pairs(tab1) do
--[[
Stante la type safety di table.equal, è
più comodo fare così
--]]
if not table.equal(value, tab2[key]) then
return false
end
end
return true
end
table.eq = table.equal
t.equal, t.eq = table.
--[[
table.merge = function(tab1, tab2)
dest[key] = value
end
table.recursiveMerge = function(tab1, tab2)
if dest[key]
and type(dest[key]) == 'table'
dest[key] = value
end
end
--]]
table.filter = function(tab, cond)
end
--]]
table.flip = function(tab)
end
--]]
table.noDuplicates = function(tab)
--[[
Il doppio ciclo si rende necessario per
mantenere l'ordine delle chiavi numeriche.
La variabile n serve per non avere buchi.
Non si usa minor poiché le chiavi vengono
già tornate in ordine crescente da ipairs.
--]]
for _, value in ipairs(tab) do
if not check[value] then
check[value] = n
n = n + 1
end
end
for key, value in table.nonIntPairs(tab) do
if minor(key, check[value]) then
check[value] = key
end
end
return table.flip(check)
end
table.no_duplicates, table.unique =
t.noDuplicates, t.no_duplicates, t.unique =
--[[
--]]
table.keys = function(tab)
end
--]]
table.cloneLoadData = function(value)
end
table.clone_load_data = table.cloneLoadData
t.cloneLoadData, t.clone_load_data =
return t
|
contributi