mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-25 23:24:35 +00:00
[modified] moved dedupe from functional to tablex
This commit is contained in:
parent
f214be9f59
commit
a87380f43c
@ -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
|
||||
-----------------------------------------------------------
|
||||
|
14
tablex.lua
14
tablex.lua
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user