Skip to content

Commit cf379f7

Browse files
committed
local-ize tables.lua functions
1 parent 8aa137e commit cf379f7

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

pico8lib/tables.lua

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,21 @@ end
4444
--- Delete and return the first item from array
4545
-- @tparam array a
4646
-- @return value of a[1] prior to deletion
47-
function shift(a)
47+
local function shift(a)
4848
return deli(a, 1)
4949
end
5050

5151
--- Add value to the beginning of array
5252
-- @tparam array a
5353
-- @param v value to add
54-
function unshift(a, v)
54+
local function unshift(a, v)
5555
return add(a,v,1)
5656
end
5757

5858
--- Select a random value from an array
5959
-- @tparam array a array to choose from
6060
-- @return randomly chosen item from array
61-
function rand_array(a)
61+
local function rand_array(a)
6262
return a[flr(rnd(#a)) + 1]
6363
end
6464

@@ -103,7 +103,7 @@ end
103103
--- Create a deep copy of a table
104104
-- @tparam table o original table
105105
-- @treturn table copy of o
106-
function table_copy(o)
106+
local function table_copy(o)
107107
local c = {}
108108
for k,v in pairs(o) do
109109
c[k] = type(v) == "table" and copy_table(v) or v
@@ -152,7 +152,7 @@ end
152152

153153
--- Randomize the order of a table using fisher-yates shuffle
154154
-- @tparam array a array to shuffle in place
155-
function shuffle(a)
155+
local function shuffle(a)
156156
for i = #a, 1, -1 do
157157
local j = flr(rnd(i)) + 1
158158
a[i], a[j] = a[j], a[i]
@@ -161,7 +161,7 @@ end
161161

162162
--- Sort an array in place, the slowest way
163163
-- @tparam array a array to sort in place
164-
function sort_slow(a)
164+
local function sort_slow(a)
165165
for i,v in inext,a do
166166
for j,w in inext,a do
167167
if (v<w) v,a[i],a[j]=w,w,v
@@ -171,7 +171,7 @@ end
171171

172172
--- Sort an array in place, in descending order, the slowest way
173173
-- @tparam array a array to sort in place
174-
function sort_slow_reverse(a)
174+
local function sort_slow_reverse(a)
175175
for i,v in inext,a do
176176
for j,w in inext,a do
177177
if (v>w) v,a[i],a[j]=w,w,v
@@ -182,7 +182,7 @@ end
182182
--- Sort an array in place, the slowest way, with a key function
183183
-- @tparam array a array to sort in place
184184
-- @tparam function f function to extract or calculate a comparison key for each value, run once per value
185-
function sort_slow_func(a, f)
185+
local function sort_slow_func(a, f)
186186
local k = {}
187187
for i = 1, #a do
188188
k[i] = f(a[i])
@@ -197,7 +197,7 @@ end
197197
--- Sort an array in place, the slowest way, with a key function
198198
-- @tparam array a array to sort in place
199199
-- @tparam function f function to extract or calculate a comparison key for each valuem run multiple times per value
200-
function sort_slow_func_unsafe(a, f)
200+
local function sort_slow_func_unsafe(a, f)
201201
for i,v in inext,a do
202202
for j,w in inext,a do
203203
if (f(v)<f(w)) v,a[i],a[j]=w,w,v
@@ -208,7 +208,7 @@ end
208208
--- Sort an array in place, the slowest way, with a comparison function
209209
-- @tparam array a array to sort in place
210210
-- @tparam function f function to compare two values
211-
function sort_slow_cmp(a, f)
211+
local function sort_slow_cmp(a, f)
212212
for i,v in inext,a do
213213
for j,w in inext,a do
214214
if (f(v,w)) v,a[i],a[j]=w,w,v
@@ -221,7 +221,7 @@ end
221221
-- @tparam array a the table to sort
222222
-- @tparam[opt] number i starting index of slice to sort
223223
-- @tparam[opt] number j ending index of slice to sort
224-
function sort_quick(a, i, j)
224+
local function sort_quick(a, i, j)
225225
i, j = i or 1, j or #a
226226
if (i>=j) return
227227
-- p points to the pivot, initially the first item
@@ -245,7 +245,7 @@ end
245245
-- @tparam function f function to extract or calculate a comparison key for each value
246246
-- @tparam[opt] number i starting index of slice to sort
247247
-- @tparam[opt] number j ending index of slice to sort
248-
function sort_quick_func(a, f, i, j)
248+
local function sort_quick_func(a, f, i, j)
249249
i, j = i or 1, j or #a
250250
if (i>=j) return
251251
local p,r = i,j
@@ -261,7 +261,7 @@ end
261261
-- @tparam function f function to compare two values
262262
-- @tparam[opt] number i starting index of slice to sort
263263
-- @tparam[opt] number j ending index of slice to sort
264-
function sort_quick_cmp(a, f, i, j)
264+
local function sort_quick_cmp(a, f, i, j)
265265
i, j = i or 1, j or #a
266266
if (i>=j) return
267267
local p,r = i,j
@@ -281,7 +281,7 @@ local sort = sort_quick
281281

282282
--- Filter a table, keeping only key,value pairs that pass a check function
283283
-- return a new table with the same keys/indices kept from the original
284-
function filter(t, check)
284+
local function filter(t, check)
285285
o = {}
286286
for k, v in pairs(t) do
287287
if (check(k,v)) o[k] = v
@@ -292,7 +292,7 @@ end
292292

293293
--- Filter an array, keeping only values that pass check function
294294
-- return a new array with sequential indices
295-
function filter_array(t, check)
295+
local function filter_array(t, check)
296296
o = {}
297297
for n in all(t) do
298298
if (check(v)) o[#o+1] = n
@@ -305,7 +305,7 @@ end
305305
-- @section Predicates
306306

307307
--- Determine if any value in a table is truthy
308-
function pred_any(t)
308+
local function pred_any(t)
309309
for k, v in pairs(t) do
310310
if (v) return true
311311
end
@@ -314,15 +314,15 @@ end
314314

315315

316316
--- Determine if any value in a table passes check function
317-
function pred_any_func(t, check)
317+
local function pred_any_func(t, check)
318318
for k, v in pairs(t) do
319319
if (check(v)) return true
320320
end
321321
return false -- optional if nil is acceptable
322322
end
323323

324324
--- Determine if every value in a table is truthy
325-
function pred_all(t)
325+
local function pred_all(t)
326326
for k, v in pairs(t) do
327327
if (not v) return false
328328
end
@@ -331,7 +331,7 @@ end
331331

332332

333333
--- Determine if every value in a table is truthy or passes check function
334-
function pred_all_func(t, check)
334+
local function pred_all_func(t, check)
335335
for k, v in pairs(t) do
336336
if (not check(v)) return false
337337
end
@@ -344,7 +344,7 @@ end
344344

345345
--- Iterate an array in reverse
346346
-- used just like all()
347-
function all_reverse(t)
347+
local function all_reverse(t)
348348
local i = #t
349349
return function()
350350
if (t==0) return nil
@@ -355,7 +355,7 @@ end
355355

356356
--- Reverse the items in an array, in place
357357
-- @tparam array a
358-
function reverse(a)
358+
local function reverse(a)
359359
for i=1,#a\2 do
360360
a[i],a[#a-i+1]=a[#a-i+1],a[i]
361361
end

0 commit comments

Comments
 (0)