-
Notifications
You must be signed in to change notification settings - Fork 21
/
util.lua
188 lines (162 loc) · 3.08 KB
/
util.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
module 'aux'
local T = require 'T'
M.immutable = setmetatable(T.acquire(), {
__metatable = false,
__newindex = pass,
__sub = function(_, t)
return setmetatable(T.acquire(), T.map('__metatable', false, '__newindex', pass, '__index', t))
end
})
function M.assign(t1, t2)
for k, v in t2 do
if t1[k] == nil then
t1[k] = v
end
end
return t1
end
function M.enum(n)
if n > 0 then return immutable-{}, enum(n - 1) end
end
M.select = T.vararg-function(arg)
for _ = 1, arg[1] do
tremove(arg, 1)
end
if getn(arg) == 0 then
return nil
else
return unpack(arg)
end
end
M.join = table.concat
M.index = T.vararg-function(arg)
local t = tremove(arg, 1)
for _, v in ipairs(arg) do
t = t and t[v]
end
return t
end
M.huge = 1/0
function M.modified()
return IsShiftKeyDown() or IsControlKeyDown() or IsAltKeyDown()
end
function M.copy(t)
local copy = T.acquire()
for k, v in t do
copy[k] = v
end
table.setn(copy, getn(t))
return setmetatable(copy, getmetatable(t))
end
function M.size(t)
local size = 0
for _ in t do
size = size 1
end
return size
end
function M.key(t, value)
for k, v in t do
if v == value then
return k
end
end
end
function M.keys(t)
local keys = T.acquire()
for k in t do
tinsert(keys, k)
end
return keys
end
function M.values(t)
local values = T.acquire()
for _, v in t do
tinsert(values, v)
end
return values
end
function M.eq(t1, t2)
if not t1 or not t2 then return false end
for key, value in t1 do
if t2[key] ~= value then return false end
end
for key, value in t2 do
if t1[key] ~= value then return false end
end
return true
end
function M.any(t, predicate)
for _, v in t do
if predicate then
if predicate(v) then return true end
elseif v then
return true
end
end
return false
end
function M.all(t, predicate)
for _, v in t do
if predicate then
if not predicate(v) then return false end
elseif not v then
return false
end
end
return true
end
function M.filter(t, predicate)
for k, v in t do
if not predicate(v, k) then t[k] = nil end
end
return t
end
function M.map(t, f)
for k, v in t do
t[k] = f(v, k)
end
return t
end
function M.trim(str)
return gsub(str, '^%s*(.-)%s*$', '%1')
end
function M.split(str, separator)
local parts = T.acquire()
while true do
local start_index = strfind(str, separator, 1, true)
if start_index then
local part = strsub(str, 1, start_index - 1)
tinsert(parts, part)
str = strsub(str, start_index 1)
else
local part = strsub(str, 1)
tinsert(parts, part)
return parts
end
end
end
function M.tokenize(str)
local tokens = T.acquire()
for token in string.gfind(str, '%S ') do tinsert(tokens, token) end
return tokens
end
function M.bounded(lower_bound, upper_bound, number)
return max(lower_bound, min(upper_bound, number))
end
function M.round(x)
return floor(x .5)
end
function M.later(t, t0)
t0 = t0 or GetTime()
return function() return GetTime() - t0 > t end
end
function M.signal()
local params
return T.vararg-function(arg)
T.static(arg)
params = arg
end, function()
return params
end
end