Difference between revisions of "Module:TableTools"
Jump to navigation
Jump to search
use fooBar for function names instead of getFooBar
en>Mr. Stradivarius (remove an unnecessary table.sort from compressSparseArray) |
en>Mr. Stradivarius (use fooBar for function names instead of getFooBar) |
||
Line 35: | Line 35: | ||
--[[ | --[[ | ||
------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ||
-- | -- union | ||
-- | -- | ||
-- This returns the union of the values of n tables, as an array. For example, for | -- This returns the union of the values of n tables, as an array. For example, for | ||
-- the tables {1, 3, 4, 5, foo = 7} and {2, bar = 3, 5, 6}, | -- the tables {1, 3, 4, 5, foo = 7} and {2, bar = 3, 5, 6}, union will return | ||
-- {1, 2, 3, 4, 5, 6, 7}. | -- {1, 2, 3, 4, 5, 6, 7}. | ||
------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ||
--]] | --]] | ||
function p. | function p.union(...) | ||
local tables = {...} | local tables = {...} | ||
local vals, ret = {}, {} | local vals, ret = {}, {} | ||
Line 59: | Line 59: | ||
--[[ | --[[ | ||
------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ||
-- | -- intersection | ||
-- | -- | ||
-- This returns the intersection of the values of n tables, as an array. For | -- This returns the intersection of the values of n tables, as an array. For | ||
-- example, for the tables {1, 3, 4, 5, foo = 7} and {2, bar = 3, 5, 6}, | -- example, for the tables {1, 3, 4, 5, foo = 7} and {2, bar = 3, 5, 6}, | ||
-- | -- intersection will return {3, 5}. | ||
------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ||
--]] | --]] | ||
function p. | function p.intersection(...) | ||
local tables = {...} | local tables = {...} | ||
local vals, ret = {}, {} | local vals, ret = {}, {} | ||
Line 87: | Line 87: | ||
--[[ | --[[ | ||
------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ||
-- | -- numKeys | ||
-- | -- | ||
-- This takes a table and returns an array containing the numbers of any numerical | -- This takes a table and returns an array containing the numbers of any numerical | ||
Line 93: | Line 93: | ||
------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ||
--]] | --]] | ||
function p. | function p.numKeys(t) | ||
local isPositiveInteger = p.isPositiveInteger | local isPositiveInteger = p.isPositiveInteger | ||
local nums = {} | local nums = {} | ||
Line 107: | Line 107: | ||
--[[ | --[[ | ||
------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ||
-- | -- affixNums | ||
-- | -- | ||
-- This takes a table and returns an array containing the numbers of keys with the | -- This takes a table and returns an array containing the numbers of keys with the | ||
-- specified prefix and suffix. For example, for the table | -- specified prefix and suffix. For example, for the table | ||
-- {a1 = 'foo', a3 = 'bar', a6 = 'baz'} and the prefix "a", | -- {a1 = 'foo', a3 = 'bar', a6 = 'baz'} and the prefix "a", affixNums will | ||
-- return {1, 3, 6}. | -- return {1, 3, 6}. | ||
------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ||
--]] | --]] | ||
function p. | function p.affixNums(t, prefix, suffix) | ||
prefix = prefix or '' | prefix = prefix or '' | ||
suffix = suffix or '' | suffix = suffix or '' | ||
Line 142: | Line 142: | ||
function p.compressSparseArray(t) | function p.compressSparseArray(t) | ||
local ret = {} | local ret = {} | ||
local nums = p. | local nums = p.numKeys(t) | ||
for _, num in ipairs(nums) do | for _, num in ipairs(nums) do | ||
ret[#ret + 1] = t[num] | ret[#ret + 1] = t[num] | ||
Line 158: | Line 158: | ||
--]] | --]] | ||
function p.sparseIpairs(t) | function p.sparseIpairs(t) | ||
local nums = p. | local nums = p.numKeys(t) | ||
local i = 0 | local i = 0 | ||
local lim = #nums | local lim = #nums |