[modified] moved dedupe from functional to tablex

This commit is contained in:
Max Cahill 2020-04-08 21:18:52 +10:00
parent f214be9f59
commit a87380f43c
2 changed files with 14 additions and 14 deletions

View File

@ -112,20 +112,6 @@ function functional.zip(t1, t2, f)
return ret
end
--return a copy of a sequence with all duplicates removed
-- causes a little "extra" gc churn; one table and one closure
-- as well as the copied deduped table
function functional.dedupe(t)
local seen = {}
return functional.filter(t, function(v)
if seen[v] then
return false
end
seen[v] = true
return true
end)
end
-----------------------------------------------------------
--common queries and reductions
-----------------------------------------------------------

View File

@ -146,6 +146,20 @@ function tablex.append(t1, t2)
return r
end
--return a copy of a sequence with all duplicates removed
-- causes a little "extra" gc churn of one table to track the duplicates internally
function tablex.dedupe(t)
local seen = {}
local r = {}
for i,v in ipairs(t) do
if not seen[v] then
seen[v] = true
table.insert(r, v)
end
end
return r
end
--(might already exist depending on luajit)
if table.clear then
--import from global if it exists