Diff

util/array.lua @ 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
parent 7976:2a7ef5fcaa77
child 8521:6f5ce0f09f40
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];