diff --git a/functional.lua b/functional.lua index d855a62..b8d9062 100644 --- a/functional.lua +++ b/functional.lua @@ -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 ----------------------------------------------------------- diff --git a/tablex.lua b/tablex.lua index 7344168..3027517 100644 --- a/tablex.lua +++ b/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