-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathvararg.lua
64 lines (51 loc) · 1.3 KB
/
vararg.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
52
53
54
55
56
57
58
59
60
61
62
63
64
local select, setmetatable, unpack = select, setmetatable, unpack
local vararg = {}
function vararg.aggregate(binary_func, initial, ...)
local total = initial
for i = 1, select("#", ...) do
total = binary_func(total, select(i, ...))
end
return total
end
local metatable = {__index = {}}
function vararg.pack(...)
return setmetatable({["#"] = select("#", ...); ...}, metatable)
end
local va = metatable.__index
function va:unpack()
return unpack(self, 1, self["#"])
end
function va:select(n)
if n > self["#"] then return end
return self[n]
end
local function inext(self, i)
i = i + 1
if i > self["#"] then return end
return i, self[i]
end
function va:ipairs()
return inext, self, 0
end
function va:concat(other)
local self_len, other_len = self["#"], other["#"]
local res = {["#"] = self_len + other_len}
for i = 1, self_len do
res[i] = self[i]
end
for i = 1, other_len do
res[self_len + i] = other[i]
end
return setmetatable(res, metatable)
end
metatable.__concat = va.concat
function va:equals(other)
if self["#"] ~= other["#"] then return false end
for i = 1, self["#"] do if self[i] ~= other[i] then return false end end
return true
end
metatable.__eq = va.equals
function va:aggregate(binary_func, initial)
return vararg.aggregate(binary_func, initial, self:unpack())
end
return vararg