-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtablepp.lua
More file actions
465 lines (396 loc) · 11.4 KB
/
tablepp.lua
File metadata and controls
465 lines (396 loc) · 11.4 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
-- MIT License
--
-- Copyright (c) 2023 LuaUtils
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
local table = require("table")
local math = require("math")
local ok, aux = pcall(require, "luautils.auxiliar")
if not ok then
ok, aux = pcall(require, "auxiliar")
end
local expected_args = aux.expected_args
---@class tablepplib: tablelib
local tablepp = {}
setmetatable(tablepp, {
__index = table
})
--- returns the largest value of `t`
---@param t table
---@return any
function tablepp.max(t)
expected_args("max", {t}, {"table"})
if math.max then
return math.max(tablepp.unpack(t))
end
local max_value = t[1]
for _, v in ipairs(t) do
if v > max_value then max_value = v end
end
return max_value
end
--- returns the smallest value of `t`
---@param t table
---@return any
function tablepp.min(t)
expected_args("min", {t}, {"table"})
if math.min then
return math.min(unpack(t))
end
local min_value = t[1]
for _, v in ipairs(t) do
if v < min_value then min_value = v end
end
return min_value
end
--- returns the index where the largest value of `t` is
---@param t table
---@return integer
function tablepp.argmax(t)
expected_args("argmax", {t}, {"table"})
local max_v, max_i = t[1], 1
for i, v in ipairs(t) do
if v > max_v then
max_i = i
max_v = v
end
end
return max_i
end
--- returns the index where the smallest value of `t` is
---@param t table
---@return integer
function tablepp.argmin(t)
expected_args("argmin", {t}, {"table"})
local min_v, min_i = t[1], 1
for i, v in ipairs(t) do
if v < min_v then
min_i = i
min_v = v
end
end
return min_i
end
--- transform `t` in string
---```lua
---tablepp.list_tostring({{1,"a"}, 1, {"\n", true, false}}) --> {{1, "a"}, 1, {"\n", true, false}}
---```
---@param t table
---@return string
function tablepp.list_tostring(t)
expected_args("list_tostring", {t}, {"table"})
local str = "{"
for i, v in ipairs(t) do
if type(v) == "string" then
str = str .. "'" .. v:gsub("\n", "\\n") .. "'"
elseif type(v) ~= "table" then
str = str .. tostring(v)
else
str = str .. tablepp.list_tostring(v)
end
str = str .. ", "
end
return str .."}"
end
function tablepp.print_table(t)
io.write("\27[30m{ \27[m")
if tablepp.type(t) == "dict" then
for k, v in pairs(t) do
if type(k) == "string" then
if not k:find("[%W ]") then
io.write(tostring(k) .. " = ")
else
io.write("[\27[32m'" .. tostring(k) .. "'\27[m] = ")
end
elseif type(k) == "number" or type(k) == "number" then
io.write("[\27[33m" .. tostring(k) .. "\27[m] = ")
elseif type(k) == "function" then
io.write("\27[35m" .. tostring(k) .. "\27[m] = ")
elseif type(k) == "table" then
io.write("[")
tablepp.print_table(k)
io.write("]")
end
if type(v) == "string" then
io.write("\27[32m'" .. tostring(v) .. "'\27[m")
elseif type(v) == "number" or type(v) == "number" then
io.write("\27[33m" .. tostring(v) .. "\27[m")
elseif type(v) == "function" then
io.write("\27[35m" .. tostring(v) .. "\27[m")
elseif type(v) == "table" then
tablepp.print_table(v)
end
io.write(", ")
end
else
for i, v in ipairs(t) do
if type(v) == "string" then
io.write("\27[32m'" .. tostring(v) .. "'\27[m")
elseif type(v) == "number" or type(v) == "number" then
io.write("\27[33m" .. tostring(v) .. "\27[m")
elseif type(v) == "function" then
io.write("\27[35m" .. tostring(v) .. "\27[m")
elseif type(v) == "table" then
tablepp.print_table(v)
end
io.write(", ")
end
end
io.write("\27[30m }\27[m")
end
tablepp.unpack = unpack or table.unpack
---Executes the given f over all elements of table. For each element, f is called with the index and respective value as arguments. If f returns a non-nil value, then the loop is broken, and this value is returned as the final value of foreach.
---@generic T
---@param list T[]
---@param callback fun(key: T, value: T): T | nil
---@return T | nil
function tablepp.foreach(list, callback)
expected_args("foreach", {list, callback}, {"table", "function"})
for k, v in pairs(list) do
local res = callback(k, v)
if res ~= nil then return res end
end
return nil
end
--- Executes the given f over the numerical indices of table. For each index, f is called with the index and respective value as arguments. Indices are visited in sequential order, from 1 to n, where n is the size of the table. If f returns a non-nil value, then the loop is broken and this value is returned as the result of foreachi.
---@generic T
---@param list T[]
---@param callback fun(key: T, value: T): T | nil
---@return T | nil
function tablepp.foreachi(list, callback)
expected_args("foreachi", {list, callback}, {"table", "function"})
for k, v in ipairs(list) do
local res = callback(k, v)
if res ~= nil then return res end
end
return nil
end
--- returns all the original table but only where the function returned true
---@generic T
---@param list T[]
---@param callback fun(key: T, value: T):boolean
---@return table
function tablepp.filter(list, callback)
expected_args("filter", {list, callback}, {"table", "function"})
local result = {}
for k, v in pairs(list) do
if callback(k, v) then
result[k] = v
end
end
return result
end
--- similar to filter but iterates like an array, thus maintaining the sequence correctly, unlike filter
---@generic T
---@param list T[]
---@param callback fun(key: T, value: T):boolean
---@return table
function tablepp.filteri(list, callback)
expected_args("filteri", {list, callback}, {"table", "function"})
local result = {}
for k, v in ipairs(list) do
if callback(k, v) then
table.insert(result, v)
end
end
return result
end
--- returns `#list`
---@generic T
---@param list T[]
---@return integer
function tablepp.getn(list)
expected_args("getn", {list}, {"table"})
return #list
end
---@generic T
---@param list T[]
---@param value T
function tablepp.append(list, value)
expected_args("append", {list}, {"table"})
list[#list+1] = value
end
--- returns how many times `value` appears in `t`
---@generic T
---@param list T[]
---@param value any
---@return integer
function tablepp.count(list, value)
expected_args("count", {list}, {"table"})
local res = 0
for k, v in pairs(list) do
if v == value then res = res + 1 end
end
return res
end
-- auxiliar function for copy
local function recursive_copy(t)
local copy = {}
for k, v in pairs(t) do
if type(t) == "table" then
copy[k] = tablepp.copy(v)
else
copy[k] = v
end
end
return copy
end
--- returns an identical copy of `t`
---@generic T
---@param t T[]
---@return T[]
function tablepp.copy(t)
expected_args("copy", {t}, {"table"})
return recursive_copy(t)
end
--- returns the reverse order of T
---@generic T
---@param t T[]
---@return T[]
function tablepp.reverse(t)
expected_args("reverse", {t}, {"table"})
local reverse = {}
local pos = 1
for i = #t, 1, -1 do
reverse[pos] = t[i]
pos = pos + 1
end
return reverse
end
--- returns `t` starting at `start` to `stop` skipping at `step`
---@generic T
---@param t T []
---@param start integer
---@param stop? integer
---@param step? integer
---@return table
function tablepp.slice(t, start, stop, step)
step = step or 1
if start and not stop then
stop, start = start, 1
end
expected_args("slice", {t, start, stop, step}, {"table", "integer", "integer", "integer"})
local result = {}
local pos = 1
for i = start, stop, step do
result[pos] = t[i]
pos = pos + 1
end
return result
end
--- returns at which index `value` appears
---@generic T
---@param t T[]
---@param value any
---@return T | nil
function tablepp.index(t, value)
expected_args("index", {t}, {"table"})
for k, v in pairs(t) do
if v == value then return k end
end
return nil
end
---
---Moves elements from table `a1` to table `a2`.
---```lua
---a2[t],··· =
---a1[f],···,a1[e]
---return a2
---```
---@param a1 table
---@param f integer
---@param e integer
---@param t integer
---@param a2? table
---@return table a2
function tablepp.move(a1, f, e, t, a2)
return table.move(a1, f, e, t, a2)
end
--- returns if `t` is list or dict
---@param t table
---@return "dict" | "list"
function tablepp.type(t)
expected_args("type", {t}, {"table"})
local len = 0
for k, v in pairs(t) do
len = len + 1
end
if len ~= #t then return "dict" else return "list" end
end
--- returns the keys of `t`
---@param t table
---@return table
function tablepp.keys(t)
expected_args("keys", {t}, {"table"})
local keys = {}
local p = 1
for k in pairs(t) do
keys[p] = k
p = p + 1
end
return keys
end
--- returns the values of `t`
---@param t table
---@return table
function tablepp.values(t)
expected_args("values", {t}, {"table"})
local values = {}
local p = 1
for _, v in pairs(t) do
values[p] = v
p = p + 1
end
return values
end
local function recursive_tolist(dict)
local res = {}
local p = 1
for k, v in pairs(dict) do
if type(v) == "table" then
res[p] = {k, recursive_tolist(v)}
else
res[p] = {k, v}
end
p = p + 1
end
return res
end
--- transforms `dict` to `list`
---```
---local dict = {a = 1, b = 2, c = 3}
---tablepp.tolist(dict) --> {{a, 1}, {b, 2}, {c, 3}}
---```
---@param dict table
---@return table
function tablepp.tolist(dict)
expected_args("tolist", {dict}, {"table"})
return recursive_tolist(dict)
end
---@param t table
---@return any
function tablepp.pop(t)
return table.remove(t)
end
function tablepp.push(t, v)
expected_args("push", {t}, {"table"})
table.insert(t, v)
return v
end
return tablepp