-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvararg.lua
189 lines (169 loc) · 3.23 KB
/
vararg.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
189
local math = require "math"
local table = require "table"
local error, assert, select = error, assert, select
local max, unpack = math.max, table.unpack or unpack
local setmetatable = setmetatable
local tinsert2 = function(t, n, i, v)
-- lua 5.2 rise error if index out of range
-- assert(type(t) =='table')
-- assert(type(n) =='number')
-- assert(type(i) =='number')
if i > n then
t[i] = v
return i
end
for j = n, i, -1 do
t[j + 1] = t[j]
end
t[i] = v
return n+1
end
local tremove2 = function(t, n, i)
-- lua 5.2 rise error if index out of range
-- assert(type(t) =='table')
-- assert(type(n) =='number')
-- assert(type(i) =='number')
if i > n then
for j = n+1, i do
t[j] = nil
end
return n
end
for j = i, n do
t[j] = t[j+1]
end
return n-1
end
local function idx(i, n, d)
if i == nil then
if not d then
return error("number expected, got nil", 2)
end
return d
end
if i < 0 then
i = n+i+1
end
if i <= 0 then
return error("index out of bounds", 2)
end
return i
end
local function pack(...)
local n = select("#", ...)
local v = {...}
return function(...)
if (...) == "#" then
return n
else
local argc = select("#", ...)
if argc == 0 then
return unpack(v, 1, n)
else
local i, j = ...
if i == nil then
if j == nil then j = 0 end
i = j+1
if i > 0 and i <= n then
return i, v[i]
end
else
i = idx(i, n, 1)
j = idx(j, n, i)
return unpack(v, i, j)
end
end
end
end
end
local function range(i, j, ...)
local n = select("#", ...)
i, j = idx(i,n), idx(j,n)
if i > j then return end
return unpack({...}, i, j)
end
local function remove(i, ...)
local n = select("#", ...)
local t = {...}
i = idx(i, n)
assert(i>0, "index out of bounds")
if i<=n then
n = tremove2(t, n, i)
end
return unpack(t, 1, n)
end
local function insert(v, i, ...)
local n = select("#", ...)
local t = {...}
i = idx(i, n)
assert(i > 0, "index out of bounds")
n = tinsert2(t, n, i, v)
return unpack(t, 1, n)
end
local function replace(v, i, ...)
local n = select("#", ...)
local t = {...}
i = idx(i, n)
assert(i > 0, "index out of bounds")
t[i] = v
n = max(n, i)
return unpack(t, 1, n)
end
local function append(...)
local n = select("#",...)
if n <= 1 then return ... end
local t = {select(2, ...)}
t[n] = (...)
return unpack(t, 1, n)
end
local function map(...)
local n = select("#", ...)
assert(n > 0)
local f = ...
local t = {}
for i = 2, n do
t[i-1] = f((select(i, ...)))
end
return unpack(t, 1, n-1)
end
local function packinto(n, t, ...)
local c = select("#", ...)
for i = 1, c do
t[n+i] = select(i, ...)
end
return n+c
end
local function concat(...)
local n = 0
local t = {}
for i = 1, select("#", ...) do
local f = select(i, ...)
n = packinto(n, t, f())
end
return unpack(t, 1, n)
end
local function count(...)
return select("#", ...)
end
local function at(i, ...)
local n = select("#", ...)
i = idx(i,n)
if i > n then return end
return (select(i, ...))
end
return setmetatable({
pack = pack,
range = range,
insert = insert,
remove = remove,
replace = replace,
append = append,
map = map,
concat = concat,
count = count,
at = at,
},{
__call = function(_, ...)
return pack(...)
end
})