پودمان: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 tostring(v) == '-nan'
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(t)
function p.removeDuplicates(arr)
checkType('removeDuplicates', 1, t, 'table')
checkType('removeDuplicates', 1, arr, 'table')
local isNan = p.isNan
local isNan = p.isNan
local ret, exists = {}, {}
local ret, exists = {}, {}
for _, v in ipairs(t) do
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 mw.ustring.gsub(s, '([%(%)%%%.%[%]%*%+%-%?%^%$])', '%%%1')
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(t)
function p.listToSet(arr)
checkType("listToSet", 1, t, "table")
checkType("listToSet", 1, arr, "table")
 
local isNan = p.isNan
local set = {}
local set = {}
for _, item in ipairs(t) do
for _, v in ipairs(arr) do
set[item] = true
if not isNan(v) then
set[v] = true
end
end
end


کاربر ناشناس

منوی ناوبری