44
44
--- Delete and return the first item from array
45
45
-- @tparam array a
46
46
-- @return value of a[1] prior to deletion
47
- function shift (a )
47
+ local function shift (a )
48
48
return deli (a , 1 )
49
49
end
50
50
51
51
--- Add value to the beginning of array
52
52
-- @tparam array a
53
53
-- @param v value to add
54
- function unshift (a , v )
54
+ local function unshift (a , v )
55
55
return add (a ,v ,1 )
56
56
end
57
57
58
58
--- Select a random value from an array
59
59
-- @tparam array a array to choose from
60
60
-- @return randomly chosen item from array
61
- function rand_array (a )
61
+ local function rand_array (a )
62
62
return a [flr (rnd (# a )) + 1 ]
63
63
end
64
64
103
103
--- Create a deep copy of a table
104
104
-- @tparam table o original table
105
105
-- @treturn table copy of o
106
- function table_copy (o )
106
+ local function table_copy (o )
107
107
local c = {}
108
108
for k ,v in pairs (o ) do
109
109
c [k ] = type (v ) == " table" and copy_table (v ) or v
152
152
153
153
--- Randomize the order of a table using fisher-yates shuffle
154
154
-- @tparam array a array to shuffle in place
155
- function shuffle (a )
155
+ local function shuffle (a )
156
156
for i = # a , 1 , - 1 do
157
157
local j = flr (rnd (i )) + 1
158
158
a [i ], a [j ] = a [j ], a [i ]
161
161
162
162
--- Sort an array in place, the slowest way
163
163
-- @tparam array a array to sort in place
164
- function sort_slow (a )
164
+ local function sort_slow (a )
165
165
for i ,v in inext ,a do
166
166
for j ,w in inext ,a do
167
167
if (v < w ) v ,a [i ],a [j ]= w ,w ,v
171
171
172
172
--- Sort an array in place, in descending order, the slowest way
173
173
-- @tparam array a array to sort in place
174
- function sort_slow_reverse (a )
174
+ local function sort_slow_reverse (a )
175
175
for i ,v in inext ,a do
176
176
for j ,w in inext ,a do
177
177
if (v > w ) v ,a [i ],a [j ]= w ,w ,v
182
182
--- Sort an array in place, the slowest way, with a key function
183
183
-- @tparam array a array to sort in place
184
184
-- @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 )
186
186
local k = {}
187
187
for i = 1 , # a do
188
188
k [i ] = f (a [i ])
197
197
--- Sort an array in place, the slowest way, with a key function
198
198
-- @tparam array a array to sort in place
199
199
-- @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 )
201
201
for i ,v in inext ,a do
202
202
for j ,w in inext ,a do
203
203
if (f (v )< f (w )) v ,a [i ],a [j ]= w ,w ,v
208
208
--- Sort an array in place, the slowest way, with a comparison function
209
209
-- @tparam array a array to sort in place
210
210
-- @tparam function f function to compare two values
211
- function sort_slow_cmp (a , f )
211
+ local function sort_slow_cmp (a , f )
212
212
for i ,v in inext ,a do
213
213
for j ,w in inext ,a do
214
214
if (f (v ,w )) v ,a [i ],a [j ]= w ,w ,v
221
221
-- @tparam array a the table to sort
222
222
-- @tparam [opt] number i starting index of slice to sort
223
223
-- @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 )
225
225
i , j = i or 1 , j or # a
226
226
if (i >= j ) return
227
227
-- p points to the pivot, initially the first item
245
245
-- @tparam function f function to extract or calculate a comparison key for each value
246
246
-- @tparam [opt] number i starting index of slice to sort
247
247
-- @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 )
249
249
i , j = i or 1 , j or # a
250
250
if (i >= j ) return
251
251
local p ,r = i ,j
261
261
-- @tparam function f function to compare two values
262
262
-- @tparam [opt] number i starting index of slice to sort
263
263
-- @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 )
265
265
i , j = i or 1 , j or # a
266
266
if (i >= j ) return
267
267
local p ,r = i ,j
@@ -281,7 +281,7 @@ local sort = sort_quick
281
281
282
282
--- Filter a table, keeping only key,value pairs that pass a check function
283
283
-- return a new table with the same keys/indices kept from the original
284
- function filter (t , check )
284
+ local function filter (t , check )
285
285
o = {}
286
286
for k , v in pairs (t ) do
287
287
if (check (k ,v )) o [k ] = v
292
292
293
293
--- Filter an array, keeping only values that pass check function
294
294
-- return a new array with sequential indices
295
- function filter_array (t , check )
295
+ local function filter_array (t , check )
296
296
o = {}
297
297
for n in all (t ) do
298
298
if (check (v )) o [# o + 1 ] = n
305
305
-- @section Predicates
306
306
307
307
--- Determine if any value in a table is truthy
308
- function pred_any (t )
308
+ local function pred_any (t )
309
309
for k , v in pairs (t ) do
310
310
if (v ) return true
311
311
end
@@ -314,15 +314,15 @@ end
314
314
315
315
316
316
--- 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 )
318
318
for k , v in pairs (t ) do
319
319
if (check (v )) return true
320
320
end
321
321
return false -- optional if nil is acceptable
322
322
end
323
323
324
324
--- Determine if every value in a table is truthy
325
- function pred_all (t )
325
+ local function pred_all (t )
326
326
for k , v in pairs (t ) do
327
327
if (not v ) return false
328
328
end
331
331
332
332
333
333
--- 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 )
335
335
for k , v in pairs (t ) do
336
336
if (not check (v )) return false
337
337
end
344
344
345
345
--- Iterate an array in reverse
346
346
-- used just like all()
347
- function all_reverse (t )
347
+ local function all_reverse (t )
348
348
local i = # t
349
349
return function ()
350
350
if (t == 0 ) return nil
355
355
356
356
--- Reverse the items in an array, in place
357
357
-- @tparam array a
358
- function reverse (a )
358
+ local function reverse (a )
359
359
for i = 1 ,# a \2 do
360
360
a [i ],a [# a - i + 1 ]= a [# a - i + 1 ],a [i ]
361
361
end
0 commit comments