From 170f67dd5801afe8ea60ced18500ee50ba10b28b Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Fri, 31 Mar 2017 17:29:08 +0200 Subject: util.array: Add method for filtering out duplicate values --- util/array.lua | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/util/array.lua b/util/array.lua index 05fa97ca..150b4355 100644 --- a/util/array.lua +++ b/util/array.lua @@ -92,6 +92,18 @@ function array_base.sort(outa, ina, ...) return outa; end +function array_base.unique(outa, ina) + local seen = {}; + return array_base.filter(outa, ina, function (item) + if seen[item] then + return false; + else + seen[item] = true; + return true; + end + end); +end + function array_base.pluck(outa, ina, key) for i = 1, #ina do outa[i] = ina[i][key]; -- cgit v1.2.3