-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtuple.lua
51 lines (40 loc) · 780 Bytes
/
tuple.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
--[[
(...) -> tuple
wrap(t, [n]) -> tuple
]]
local setmetatable, select, table, tostring =
setmetatable, select, table, tostring
setfenv(1, {})
local meta = {__type = 'tuple'}
local function wrap(t, n)
t.n = n or t.n or #t
setmetatable(t, meta)
return t
end
local function new(...)
return wrap({n=select('#',...),...})
end
function meta:__eq(other)
if self.n ~= other.n then
return false
end
for i=1,self.n do
if self[i] ~= other[i] then
return false
end
end
return true
end
function meta:__tostring()
local t = {}
for i=1,self.n do
t[i] = tostring(self[i])
end
return '('..table.concat(t, ', ', 1, self.n)..')'
end
local M = {
meta = meta,
wrap = wrap,
new = new,
}
return setmetatable(M, {__call = function(_,...) return new(...) end})