پودمان:TableTools: تفاوت میان نسخهها
پرش به ناوبری
پرش به جستجو
updates/fixes requested by User:Uzume
جز (۱ نسخه واردشده) |
en.wikipedia>MSGJ (updates/fixes requested by User:Uzume) |
||
خط ۳۸: | خط ۳۸: | ||
------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ||
function p.isNan(v) | function p.isNan(v) | ||
return type(v) == 'number' and | return type(v) == 'number' and v ~= v | ||
end | end | ||
خط ۶۴: | خط ۶۴: | ||
-- 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. | ||
خط ۱۱۶: | خط ۱۱۶: | ||
local function cleanPattern(s) | local function cleanPattern(s) | ||
-- Cleans a pattern so that the magic characters ()%.[]*+-?^$ are interpreted literally. | -- Cleans a pattern so that the magic characters ()%.[]*+-?^$ are interpreted literally. | ||
return | return s:gsub('([%(%)%%%.%[%]%*%+%-%?%^%$])', '%%%1') | ||
end | end | ||
خط ۳۳۷: | خط ۳۳۷: | ||
-- | -- | ||
-- 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 | ||
خط ۳۵۵: | خط ۳۵۸: | ||
-- 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 | ||