Difference between revisions of "Module:TableTools"

297 bytes removed ,  15:11, 19 December 2013
fix the valueUnion function for NaNs (and make it a lot simpler to boot)
>Mr. Stradivarius
(remove shallowClone - hardly worth having this in here)
>Mr. Stradivarius
(fix the valueUnion function for NaNs (and make it a lot simpler to boot))
Line 17: Line 17:
local infinity = math.huge
local infinity = math.huge
local checkType = libraryUtil.checkType
local checkType = libraryUtil.checkType
-- Define a unique value to represent NaN. This is because NaN cannot be used as a table key.
local nan = {}


--[[
--[[
Line 140: Line 137:
function p.valueUnion(...)
function p.valueUnion(...)
local lim = select('#', ...)  
local lim = select('#', ...)  
if lim == 0 then
if lim < 2 then
error("no arguments passed to 'valueUnion'", 2)
error(lim .. ' argument' .. (lim == 1 and '' or 's') .. " passed to 'valueUnion' (minimum is 2)", 2)
end
end
local vals, ret = {}, {}
local isNan = p.isNan
local ret, exists = {}, {}
for i = 1, lim do
for i = 1, lim do
local t = select(i, ...)
local t = select(i, ...)
checkType('valueUnion', i, t, 'table')
checkType('valueUnion', i, t, 'table')
for k, v in pairs(t) do
for k, v in pairs(t) do
if type(v) == 'number' and tostring(v) == '-nan' then
if isNan(v) then
v = nan -- NaN cannot be a table key, so use a proxy variable.
ret[#ret + 1] = v
elseif not exists[v] then
ret[#ret + 1] = v
exists[v] = true
end
end
vals[v] = true
end
end
for val in pairs(vals) do
if val == nan then
-- This ensures that we output a NaN when we had one as input, although
-- they may have been generated in a completely different way.
val = 0/0
end
end
ret[#ret + 1] = val
end
end
return ret
return ret
Anonymous user