-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluaex.lua
More file actions
58 lines (52 loc) · 1.05 KB
/
luaex.lua
File metadata and controls
58 lines (52 loc) · 1.05 KB
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
-- Patch table library
table.filter = function(t, func)
local out = {}
for k, v in pairs(t) do
if func(v, k, t) then
table.insert(out, v)
end
end
return out
end
table.includes = function(t, e)
for _, value in pairs(t) do
if value == e then
return e
end
end
return nil
end
table.reduce = function(tbl, func, initial)
local accumulator = initial
for _, value in ipairs(tbl) do
accumulator = func(accumulator, value)
end
return accumulator
end
table.length = function (T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
table.map = function (tbl, func)
local result = {}
for i, v in ipairs(tbl) do
result[i] = func(v, i, tbl) -- Apply the function to each element
end
return result
end
table.slice = function(t, first, last)
local sliced = {}
for i = first, last do
sliced[#sliced+1] = t[i]
end
return sliced
end
-- Patch string library
string.split = function(string, pattern)
local t = {}
for i in string.gmatch(string, pattern) do
t[#t + 1] = i
end
return t
end