From a87380f43c314fbaa534efa44e51c473a570156f Mon Sep 17 00:00:00 2001 From: Max Cahill <1bardesign@gmail.com> Date: Wed, 8 Apr 2020 21:18:52 +1000 Subject: [PATCH] [modified] moved dedupe from functional to tablex --- functional.lua | 14 -------------- tablex.lua | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 14 deletions(-) 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