-
Notifications
You must be signed in to change notification settings - Fork 1
/
table.lua
67 lines (60 loc) · 1.31 KB
/
table.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
65
66
67
local function Table(o)
return setmetatable(o or {}, {__index = function(t, k) return table[k] end})
end
function table:removeValue(value)
assert(type(value) ~= 'nil', 'arg 1 missing value')
for i=#self, 1, -1 do
if (self[i] == value) then
self:remove(i)
return true
end
end
return false
end
function table:contains(value)
assert(type(value) ~= 'nil', 'arg 1 missing value')
for i=1, #self do
if (self[i] == value) then
return true
end
end
return false
end
function table:map(func)
assert(type(func) == 'function', 'arg 1 not a function')
local new = Table()
for i=1, #self do
new[i] = func(self[i], i, self)
end
return new
end
function table:filter(func)
assert(type(func) == 'function', 'arg 1 not a function')
local new = Table()
for i=1, #self do
local value = self[i]
if func(value, i, self) then
new[#new+1] = value
end
end
return new
end
function table:reduce(func, startval)
assert(type(func) == 'function', 'arg 1 not a function')
local value = startval or 0
for i=1, #self do
value = func(value, self[i], startval)
end
return value
end
function table:copy()
local new = Table()
for k, v in pairs(self) do
if (type(v) == "table") then
v = v:copy()
end
new[k] = v
end
return new
end
return Table