Comparison

util/set.lua @ 11542:c358537c0878 0.11

util.set: Add is_set() to test if an object is a set
author Matthew Wild <mwild1@gmail.com>
date Mon, 10 May 2021 16:41:56 +0100
parent 9488:a96a2fbcc6c0
child 11560:3bbb1af92514
comparison
equal deleted inserted replaced
11541:13b84682518e 11542:c358537c0878
4 -- 4 --
5 -- This project is MIT/X11 licensed. Please see the 5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information. 6 -- COPYING file in the source package for more information.
7 -- 7 --
8 8
9 local ipairs, pairs, setmetatable, next, tostring = 9 local ipairs, pairs, getmetatable, setmetatable, next, tostring =
10 ipairs, pairs, setmetatable, next, tostring; 10 ipairs, pairs, getmetatable, setmetatable, next, tostring;
11 local t_concat = table.concat; 11 local t_concat = table.concat;
12 12
13 local _ENV = nil; 13 local _ENV = nil;
14 -- luacheck: std none 14 -- luacheck: std none
15 15
27 local a, i = {}, 1; 27 local a, i = {}, 1;
28 for item in self._items do 28 for item in self._items do
29 a[i], i = item, i+1; 29 a[i], i = item, i+1;
30 end 30 end
31 return a; 31 return a;
32 end
33
34 local function is_set(o)
35 local mt = getmetatable(o);
36 return mt == set_mt;
32 end 37 end
33 38
34 local function new(list) 39 local function new(list)
35 local items = setmetatable({}, items_mt); 40 local items = setmetatable({}, items_mt);
36 local set = { _items = items }; 41 local set = { _items = items };
169 return t_concat(s, ", "); 174 return t_concat(s, ", ");
170 end 175 end
171 176
172 return { 177 return {
173 new = new; 178 new = new;
179 is_set = is_set;
174 union = union; 180 union = union;
175 difference = difference; 181 difference = difference;
176 intersection = intersection; 182 intersection = intersection;
177 xor = xor; 183 xor = xor;
178 }; 184 };