Changeset

8017:ec7cab8e744d

util.array: Add method for filtering out duplicate values
author Kim Alvefur <zash@zash.se>
date Fri, 31 Mar 2017 17:29:08 +0200
parents 8016:9546c629289b
children 8018:9545d0a9401f
files util/array.lua
diffstat 1 files changed, 12 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/util/array.lua	Wed Mar 29 22:38:31 2017 +0200
+++ b/util/array.lua	Fri Mar 31 17:29:08 2017 +0200
@@ -92,6 +92,18 @@
 	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];