-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtables.lua
More file actions
203 lines (190 loc) · 4.23 KB
/
tables.lua
File metadata and controls
203 lines (190 loc) · 4.23 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
-- Copyright 2025 The zb Authors
-- SPDX-License-Identifier: MIT
---Reports whether a value equal to x occurs in the table t.
---@generic T
---@param x T
---@param t table<any, T>
---@return boolean
function elem(x, t)
for _, v in pairs(t) do
if v == x then return true end
end
return false
end
---Returns a copy of the table where each value is transformed by the given function.
---@generic K, T, U
---@param f fun(T, K): U
---@param list T[]
---@return U[]
function map(f, list)
local result = {}
for k, x in pairs(list) do
result[k] = f(x, k)
end
return result
end
---Returns a shallow copy of t.
---@generic K, V
---@param t table<K, V>
---@return table<K, V>
function clone(t)
return map(function (x) return x end, t)
end
---Copy the pairs from each argument into the first argument.
---@generic T: table
---@param t T
---@param ... table
---@return T
function update(t, ...)
local n <const> = select("#", ...)
for i = 1, n do
for k, v in pairs(select(i, ...)) do
t[k] = v
end
end
return t
end
---Concatenate the array tables given as arguments into a new table.
---@generic T
---@param ... T[]
---@return T[]
function concatLists(...)
local result = {}
for i = 1, select("#", ...) do
local t = select(i, ...)
table.move(t, 1, #t, #result + 1, result)
end
return result
end
---@param x any
---@return boolean
local function isLazyKey(x)
local tp = type(x)
return tp == "number" or tp == "string" or tp == "boolean"
end
---A plug-compatible pure Lua implementation of lazy table
---(as specified in https://github.com/256lights/zb/issues/83).
---It does not perform memoization, so it may be frozen.
---@generic K: string|boolean|number
---@param f fun(t: table<K, any>, k: K): any
---@param init? table<K, any>
---@return table<K, any>
function lazy(f, init)
local obj = {}
local ff = function(_, k)
if isLazyKey(k) then
return f(obj, k)
else
return nil
end
end
local mt = {
__index = ff;
__newindex = function()
error("cannot modify lazy table")
end;
__metatable = false;
}
if init then
local t = {}
for k, v in pairs(init) do
if isLazyKey(k) then
t[k] = v
end
end
mt.__index = setmetatable(t, { __index = ff })
end
return setmetatable(obj, mt)
end
---Returns a lazy table derived from t
---where all function values in t are treated as getters.
---@generic K: string|boolean|number
---@param t table<K, any>
---@return table<K, any>
function lazyModule(t)
---@type table<any, any>
local init = {}
---@type table<any, fun(): any>
local accessors = {}
for k, v in pairs(t) do
if isLazyKey(k) then
if type(v) == "function" then
accessors[k] = v
else
init[k] = v
end
end
end
local function lazyNext(_, k)
local v
if k == nil or init[k] ~= nil then
k, v = next(init, k)
if k == nil then
k, v = next(accessors, k)
if v then v = v() end
end
else
k, v = next(accessors, k)
if v then v = v() end
end
if k == nil then
return nil
end
return k, v
end
return setmetatable({}, {
__metatable = false;
__index = lazy(function(_, k)
local f = accessors[k]
if f then
return f()
else
return nil
end
end, init);
__setindex = function()
error("cannot modify lazy module")
end;
__pairs = function(obj)
return lazyNext, obj, nil
end;
})
end
---Returns an object whose fields apply f to t as they are accessed.
---@generic K: string|boolean|number
---@generic U, V
---@param f fun(U, K): V
---@param t table<K, U>
---@return table<K, V>
function lazyMap(f, t)
local function lazyMapNext(obj, k)
local v
repeat
k = next(t, k)
if k == nil then
return nil
end
if isLazyKey(k) then
v = obj[k]
end
until v ~= nil
return k, v
end
return setmetatable({}, {
__metatable = false;
__index = lazy(function(_, k)
local x = t[k]
if x then
return f(x, k)
else
return nil
end
end);
__setindex = function()
error("cannot modify lazy module")
end;
__pairs = function(obj)
return lazyMapNext, obj, nil
end;
})
end