-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.lua
More file actions
198 lines (174 loc) · 3.65 KB
/
table.lua
File metadata and controls
198 lines (174 loc) · 3.65 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
local min <const> = math.min
local max <const> = math.max
local random <const> = math.random
function table.contains(list, needle)
for i, elem in ipairs(list) do
if elem == needle then
return true, i
end
end
return false
end
function table.copy(list)
local res = {}
for i = 1, #list do
res[i] = list[i]
end
return res
end
function table.drop(list, n)
local res = {}
for i = n + 1, #list do
res[i - n] = list[i]
end
return res
end
function table.enum(list)
local res = {}
for i = 1, #list do
res[list[i]] = i
end
return res
end
function table.equals(a, b)
local na, nb = #a, #b
if na ~= nb then
return false
end
for i = 1, na do
if a[i] ~= b[i] then
return false
end
end
return true
end
function table.every(list, cb)
for i, elem in ipairs(list) do
if not cb(elem, i, list) then
return false
end
end
return true
end
function table.filter(list, callback)
local res = {}
for i, elem in ipairs(list) do
if callback(elem, i, list) then
res[#res + 1] = elem
end
end
return res
end
function table.find(list, callback)
for i, elem in ipairs(list) do
if callback(elem, i, list) then
return elem, i
end
end
end
function table.map(list, callback)
local res = {}
for i, elem in ipairs(list) do
res[i] = callback(elem, i, list)
end
return res
end
function table.pluck(list, key)
local res = {}
for i = 1, #list do
res[i] = list[i][key]
end
return res
end
function table.random(list)
local i = random(1, #list)
return list[i], i
end
function table.reduce(list, callback, initial)
local res = initial
for i, elem in ipairs(list) do
res = callback(res, elem, i, list)
end
return res
end
function table.reject(list, callback)
local res = {}
for i, elem in ipairs(list) do
if not callback(elem, i, list) then
res[#res + 1] = elem
end
end
return res
end
function table.reversed(list)
local res = {}
local len = #list
for i = len, 1, -1 do
res[len - i + 1] = list[i]
end
return res
end
-- in-place version of shuffled()
function table.shuffle(list)
for i = #list, 2, -1 do
local j = random(i)
list[i], list[j] = list[j], list[i]
end
end
-- returns a copy
function table.shuffled(list)
local res = {}
local len = #list
for i = len, 2, -1 do
local j = random(i)
res[len - i + 1] = list[j]
list[j] = list[i]
end
return res
end
function table.some(list, callback)
for i, elem in ipairs(list) do
if callback(elem, i, list) then
return true
end
end
return false
end
function table.sorted(list, comp)
local res = table.copy(list)
table.sort(res, comp)
return res
end
function table.sub(list, from, till)
if not from then
from = 1
elseif from < 0 then
from = max(1, #list + from + 1)
end
if not till then
till = #list
elseif till < 0 then
till = min(#list, #list + till + 1)
end
local res = {}
for i = from, till do
res[i - from + 1] = list[ix]
end
return res
end
function table.take(list, n)
local res = {}
local last = min(#list, n)
for i = 1, last do
res[i] = list[i]
end
return res
end
function table.zipWith(a, b, callback)
local res = {}
local len = min(#a, #b)
for i = 1, len do
res[i] = callback(a[i], b[i], i)
end
return res
end