Module:TableTools: Difference between revisions
updates/fixes requested by User:Uzume
m (1 revision imported) |
(updates/fixes requested by User:Uzume) |
||
Line 38: | Line 38: | ||
------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ||
function p.isNan(v) | function p.isNan(v) | ||
return type(v) == 'number' and | return type(v) == 'number' and v ~= v | ||
end | end | ||
Line 64: | Line 64: | ||
-- removed, but otherwise the array order is unchanged. | -- removed, but otherwise the array order is unchanged. | ||
------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ||
function p.removeDuplicates( | function p.removeDuplicates(arr) | ||
checkType('removeDuplicates', 1, | checkType('removeDuplicates', 1, arr, 'table') | ||
local isNan = p.isNan | local isNan = p.isNan | ||
local ret, exists = {}, {} | local ret, exists = {}, {} | ||
for _, v in ipairs( | for _, v in ipairs(arr) do | ||
if isNan(v) then | if isNan(v) then | ||
-- NaNs can't be table keys, and they are also unique, so we don't need to check existence. | -- NaNs can't be table keys, and they are also unique, so we don't need to check existence. | ||
Line 337: | Line 337: | ||
-- | -- | ||
-- Transposes the keys and values in an array. For example, {"a", "b", "c"} -> | -- Transposes the keys and values in an array. For example, {"a", "b", "c"} -> | ||
-- {a = 1, b = 2, c = 3}. | -- {a = 1, b = 2, c = 3}. Duplicates are not supported (result values refer to | ||
-- the index of the last duplicate) and NaN values are ignored. | |||
------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ||
function p.invert(arr) | function p.invert(arr) | ||
checkType("invert", 1, arr, "table") | checkType("invert", 1, arr, "table") | ||
local isNan = p.isNan | |||
local map = {} | local map = {} | ||
for i, v in ipairs(arr) do | for i, v in ipairs(arr) do | ||
map[v] = i | if not isNan(v) then | ||
map[v] = i | |||
end | |||
end | end | ||
Line 355: | Line 358: | ||
-- Creates a set from the array part of the table. Indexing the set by any of the | -- Creates a set from the array part of the table. Indexing the set by any of the | ||
-- values of the array returns true. For example, {"a", "b", "c"} -> | -- values of the array returns true. For example, {"a", "b", "c"} -> | ||
-- {a = true, b = true, c = true}. | -- {a = true, b = true, c = true}. NaN values are ignored as Lua considers them | ||
-- never equal to any value (including other NaNs or even themselves). | |||
------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ||
function p.listToSet( | function p.listToSet(arr) | ||
checkType("listToSet", 1, | checkType("listToSet", 1, arr, "table") | ||
local isNan = p.isNan | |||
local set = {} | local set = {} | ||
for _, | for _, v in ipairs(arr) do | ||
set[ | if not isNan(v) then | ||
set[v] = true | |||
end | |||
end | end | ||