Module:TableTools: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
remove shallowClone - hardly worth having this in here |
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. |
|||
⚫ | |||
--[[ |
--[[ |
||
Line 140: | Line 137: | ||
function p.valueUnion(...) |
function p.valueUnion(...) |
||
local lim = select('#', ...) |
local lim = select('#', ...) |
||
if lim |
if lim < 2 then |
||
error( |
error(lim .. ' argument' .. (lim == 1 and '' or 's') .. " passed to 'valueUnion' (minimum is 2)", 2) |
||
end |
end |
||
local |
local isNan = p.isNan |
||
⚫ | |||
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 |
if isNan(v) then |
||
⚫ | |||
v = nan -- NaN cannot be a table key, so use a proxy variable. |
|||
elseif not exists[v] then |
|||
ret[#ret + 1] = v |
|||
⚫ | |||
end |
end |
||
⚫ | |||
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 |
||
⚫ | |||
end |
end |
||
return ret |
return ret |