Content deleted Content added
improved module formatting; improved isArray; added isArrayLike; fixed _deepCopy; and improved defaultKeySort, code by User:Alexiscoutinho |
Update from sandbox per request |
||
(One intermediate revision by one other user not shown) | |||
Line 38:
------------------------------------------------------------------------------------
function p.isNan(v)
return type(v) == 'number' and
end
Line 64:
-- removed, but otherwise the array order is unchanged.
------------------------------------------------------------------------------------
function p.removeDuplicates(
checkType('removeDuplicates', 1,
local isNan = p.isNan
local ret, exists = {}, {}
for _, v in ipairs(
if isNan(v) then
-- NaNs can't be table keys, and they are also unique, so we don't need to check existence.
ret[#ret + 1] = v
▲ exists[v] = true
end▼
end
end
Line 337 ⟶ 335:
--
-- Transposes the keys and values in an array. For example, {"a", "b", "c"} ->
-- {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)
checkType("invert", 1, arr, "table")
local isNan = p.isNan
local map = {}
for i, v in ipairs(arr) do
map[v] = i
end
Line 355 ⟶ 356:
-- 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"} ->
-- {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(
checkType("listToSet", 1,
local isNan = p.isNan
local set = {}
for _,
if not isNan(v) then
set[
end
Line 374 ⟶ 378:
------------------------------------------------------------------------------------
local function _deepCopy(orig, includeMetatable, already_seen)
-- Stores copies of tables indexed by the original table.▼
end
local copy = already_seen[orig]
if copy ~= nil then
return copy
end
▲ if type(orig) == 'table' then
already_seen[orig] = copy -- memoize before any recursion, to avoid infinite loops
▲ copy = {}
copy[_deepCopy(orig_key, includeMetatable, already_seen)] = _deepCopy(orig_value, includeMetatable, already_seen) local mt = getmetatable(orig)
▲ if includeMetatable then
▲ if mt ~= nil then
▲ local mt_copy = _deepCopy(mt, includeMetatable, already_seen)
▲ end
end
▲ copy = orig
end
return copy
end
Line 405 ⟶ 407:
function p.deepCopy(orig, noMetatable, already_seen)
checkType("deepCopy", 3, already_seen, "table", true)
return _deepCopy(orig, not noMetatable, already_seen or {})
end
Line 470 ⟶ 472:
end
return false
end
------------------------------------------------------------------------------------
-- merge
--
-- Given the arrays, returns an array containing the elements of each input array
-- in sequence.
------------------------------------------------------------------------------------
function p.merge(...)
local arrays = {...}
local ret = {}
for i, arr in ipairs(arrays) do
checkType('merge', i, arr, 'table')
for _, v in ipairs(arr) do
ret[#ret + 1] = v
end
end
return ret
end
------------------------------------------------------------------------------------
-- extend
--
-- Extends the first array in place by appending all elements from the second
-- array.
------------------------------------------------------------------------------------
function p.extend(arr1, arr2)
checkType('extend', 1, arr1, 'table')
checkType('extend', 2, arr2, 'table')
for _, v in ipairs(arr2) do
arr1[#arr1 + 1] = v
end
end
|