This repository has been archived by the owner on Dec 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhelpers.lua
executable file
·657 lines (464 loc) · 12 KB
/
helpers.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
#!/usr/bin/env lua
---- string-related
local sgsub = string.gsub
local sfind = string.find
local smatch = string.match
local sformat = string.format
local tos = tostring
---- table-related
local next = next
local getmeta = getmetatable
local setmeta = setmetatable -- can only use on tables
local dsetmeta = debug.setmetatable
local tcat = table.concat
local tins = table.insert
local trem = table.remove
local rget = rawget
local rset = rawset
---- debug-related
local dgetinfo = debug.getinfo
---- generator-related
local pairs = pairs
local ipairs = ipairs
-- coroutine-related
local cwrap = coroutine.wrap
local cyield = coroutine.yield
---- file-related
local ioutput = io.output
---- random...-related
local type = type
local pcall = pcall
local assert = assert
local select = select
local unpack = unpack
-- {{{ scriptname() --
-- get the script name
scriptname =
function ()
-- 2 stack level of the function that called scriptname()
return smatch(dgetinfo(2).short_src, '[^/]*$')
end
-- }}}
-- {{{ fprintf()
fprintf =
function (fh, ...)
-- fh:write() does have a return value!
return fh:write(sformat(...))
end
-- }}}
-- {{{ printf()
printf =
function (...)
return fprintf(ioutput(), ...)
end
-- }}}
-- {{{ fprintln()
fprintln =
function (fh, ...)
for v in each({ ... }) do
local s, e = fh:write(v, '\r\n')
if not s then
return s, e
end
end
return true
end
-- }}}
-- {{{ println()
println =
function (...)
return fprintln(ioutput(), ...)
end
-- }}}
-- {{{ range()
-- range(start) returns an iterator from 1 to a (step = 1)
-- range(start, stop) returns an iterator from a to b (step = 1)
-- range(start, stop, step) returns an iterator from a to b, counting by step.
range =
function (i, to, inc)
do
local i_type = type(i)
if i_type ~= 'number' then
-- behave like a call to ipairs() with no arguments
error(sformat([[bad argument #1 to 'range' (number expected, got %s)]], i_type == 'nil' and 'no value' or i_type))
end
end
if not to then
to = i
i = to == 0 and 0 or (to > 0 and 1 or -1)
end
-- we don't have to do the to == 0 check
-- 0 -> 0 with any inc would never iterate
inc = inc or (i < to and 1 or -1)
-- step back (once) before we start
i = i - inc
return function () if i == to then return nil end i = i + inc return i, i end
end
-- }}}
-- {{{ string.squote()
-- single-quote a string and escape single-quotes in that string
local squote =
function (s)
return [[']] .. sgsub(tos(s), [[']], [[\']]) .. [[']]
end
string.squote = squote
-- }}}
-- {{{ string.count_matches()
-- Use string.gsub() for match counting (example: helpers.count_matches('aaaa', 'a') -> 4
local count_matches =
function (self, pattern)
return select(2, sgsub(self, pattern, '%0'))
end
string.count_matches = count_matches
-- }}}
-- {{{ getmetatable('').__mod() (str_mod())
local str_mod =
function (fmt, args)
return sformat(fmt, unpack(type(args) == 'table' and args or { args }))
end
getmeta('').__mod = str_mod
-- }}}
-- {{{ getmetatable(0).each() (num_each())
-- make numbers a little more interesting:
-- (5):each(print)
local num_each =
function (self, f, ...)
for i = 1, self do f(i, ...) end
end
-- }}}
-- {{{ getmetatable(0).times() (times())
-- like :each() but doesn't provide the current
-- number as the first arg on each iteration
local num_times =
function (self, f, ...)
-- arg shaving :D
num_each(self, function (_, ...) return f(...) end, ...)
-- for i = 1, self do f(...) end
end
-- }}}
do
local tmp = {}
tmp.__index = tmp
tmp.each = num_each
tmp.times = num_times
dsetmeta(0, tmp)
end
-- {{{ getmetatable(function () end).chain() (func_chain())
local func_chain =
function (self)
-- note the arg shaving
-- everything from self(...) SHOULD be discarded
return function (...) self(...) return (...) end
end
-- }}}
-- {{{ getmetatable(function () end).wrap() (func_wrap())
local func_wrap =
function (self, outer)
return
function (...)
return outer(self(...))
end
end
-- }}}
do
local tmp = {}
tmp.__index = tmp
tmp.chain = func_chain
tmp.wrap = func_wrap
dsetmeta(function () end, tmp)
end
-- {{{ each(),reach(),every(),pairs(),ipairs() -- iterator/generator functions
-- iterators
local _each = function (t, f, ...) for i = 1, #t do f(t[i], i, ...) end end -- all 3 are value-first, key 2nd
local _reach = function (t, f, ...) for i = #t, 1, -1 do f(t[i], i, ...) end end
local _every = function (t, f, ...) for k, v in pairs(t) do f(v, k, ...) end end
-- these public-facing functions either generate an iterator or iterate if a function and args are passed
-- TODO: polymorphic each()/reach() that can generate iterators of number ranges and string splits
each = function (t, ...) if ... ~= nil then _each(t, ...) return t end return cwrap( _each), t, cyield end
reach = function (t, ...) if ... ~= nil then _reach(t, ...) return t end return cwrap(_reach), t, cyield end
every = function (t, ...) if ... ~= nil then _every(t, ...) return t end return cwrap(_every), t, cyield end
-- localise our globals
local each = each
local reach = reach
local every = every
-- make pairs/ipairs generators also act as directly started iterators if a function and args are passed
do
local orig_pairs = pairs
local orig_ipairs = ipairs
pairs = function (t, f, ...) if f ~= nil then for k, v in orig_pairs(t) do f(k, v, ...) end return t end return orig_pairs(t) end
ipairs = function (t, f, ...) if f ~= nil then for k, v in orig_ipairs(t) do f(k, v, ...) end return t end return orig_ipairs(t) end
-- globalise our locals (see top)
_G.pairs = pairs
_G.ipairs = ipairs
end
-- }}}
-- {{{ to_table() -- returns a table of the collected values from the args returned by a generator
-- example: to_table(ipairs({ 'a', 'b', 'c' }))
-- this is not redundant, believe me ^
to_table =
function (...)
local ret = {}
for k, v in ... do
ret[k] = v
end
return ret
end
-- }}}
-- {{{ table.is_empty() (tis_empty())
-- Use this instead of #(some_table) ~= 0
-- fetching the length is much more expensive than
-- seeing if it has an initial index to iterate with
local tis_empty =
function (self)
return next(self) == nil
end
table.is_empty = tis_empty
-- }}}
-- {{{ table.append() (tappend())
local tappend =
function (t, ...)
-- for each table
for t2 in each({ ... }) do
-- insert each element
for v in each(t2) do
tins(t, v)
end
end
return t
end
table.append = tappend
-- }}}
-- {{{ table.prepend() (tprepend())
local tprepend =
function (t, ...)
for t2 in reach({ ... }) do
for v in reach(t2) do
tins(t, 1, v)
end
end
return t
end
table.prepend = tprepend
-- }}}
-- {{{ table.sort() (tsort())
-- I feel sexy.
local tsort = table.sort:chain()
table.sort = tsort
-- }}}
-- {{{ table.copy() (tcopy())
-- makes a copy of the table (a shallow copy)
-- also makes a shallow copy if the original table's metatable (if available)
local tcopy = nil -- forward declaration
do
-- dummy func that fetches values while respecting metamethods (no rawget)
local mget = function (s, k) return s[k] end
tcopy =
function (self, mode)
local tmode = type(mode)
local ret = {}
local get = tmode == 'string' and sfind(mode, 'r') and rget or mget
for k in pairs(self) do
ret[k] = get(self, k)
end
if tmode == 'string' and sfind(mode, 'm') then
local tmp = getmeta(self)
tmp = tmp and tcopy(tmp, 'rm') or nil
-- sets a metatable if it's available
setmeta(ret, tmp)
end
return ret
end
end
table.copy = tcopy
-- }}}
-- {{{ table.reverse() (treverse())
-- works with the sequence part of the table only
local treverse =
function (self)
local len = #self
-- we don't actually need math.floor(len / 2)
-- since we step by 1
for i = 1, len / 2 do
self[i], self[len] = self[len], self[i]
len = len - 1
end
return self
end
table.reverse = treverse
-- }}}
-- {{{ table.map() (tmap())
local tmap =
function (self, f, ...)
local ret = {}
for v, i in each(self) do
local tmp = f(v, i, ...)
if tmp ~= nil then
tins(ret, tmp)
end
end
return ret
end
table.map = tmap
-- }}}
-- {{{ table.inject() (tinject())
local tinject =
function (self, f, ...)
local args = { ... }
for i = 1, #self do
args = { f(self[i], unpack(args)) }
end
return unpack(args)
end
table.inject = tinject
-- }}}
-- {{{ table.reduce() (treduce())
-- accepts a table, a binary function, and (optionally) 1 initial argument
local treduce =
function (self, f, ...)
-- arg number restriction, c wut i did thar?
-- tinject() actually allows for more flexibility
return tinject(self, function (x, y) return f(x, y) end, (...))
end
table.reduce = treduce
-- }}}
-- {{{ table.join() (tjoin())
-- A table.join() that respects __tostring metamethods on the table elements it's joining.
local tjoin =
function (self, ...)
return tcat(tmap(tcopy(self), tos), ...)
end
table.join = tjoin
-- }}}
-- {{{ table.clear() (tclear())
-- Clear an existing table. (DO NOT CREATE A NEW TABLE)
local tclear =
function (self)
for k in pairs(self) do
rset(self, k, nil)
end
return self
end
table.clear = tclear
-- }}}
-- {{{ table.keys() (tkeys())
local tkeys =
function (self)
local ret = {}
for k in pairs(self) do
tins(ret, k)
end
return ret
end
table.keys = tkeys
-- }}}
-- {{{ table.vals() (tvals())
local tvals =
function (self)
local ret = {}
for v in every(self) do
tins(ret, v)
end
return ret
end
table.vals = tvals
-- }}}
-- {{{ table.maxn() (tmaxn()) -- get highest numeric key, not the same as the length operator
local tmaxn =
function (self)
local tmp = nil
for k in pairs(self) do
if type(k) == 'number' and (tmp == nil or k > tmp) then
tmp = k
end
end
return tmp
end
table.maxn = tmaxn
-- }}}
-- {{{ table.remove_if() (tremove_if()) -- in-place removal of elements if they fail the predicate
local tremove_if =
function (self, f, ...)
-- iterate backward for removals
for v, i in reach(self) do
if f(v, ...) then
trem(self, i) -- implicit table.compact() :-)
end
end
return self
end
table.remove_if = tremove_if
-- }}}
-- {{{ table.filter() (tfilter()) -- create a new table from those that succeed the predicate
local tfilter =
function (self, f, ...)
local ret = {}
for v in each(self) do
if f(v, ...) then
tins(ret, v)
end
end
return ret
end
table.filter = tfilter
-- }}}
-- {{{ table.compact() (tcompact())
-- create a new table with the numeric keys and associated values 'compacted' down into a constiguous sequence
local tcompact =
function (self)
local keys = tfilter(tkeys(self), function (x) return type(x) == 'number' end)
local ret = {}
for k in each(keys) do
tins(ret, self[k])
end
return ret
end
table.compact = tcompact
-- }}}
-- {{{ table.stripe() (tstripe())
local tstripe =
function (self, stripe)
-- start after the last element
-- table.insert() pushes existing values to the end
for i = #self + 1, 2, -1 do
tins(self, i, stripe)
end
return self
end
table.stripe = tstripe
-- }}}
-- {{{ is_callable()
-- lua doesn't allow for:
-- (setmetatable({}, { __call = setmetatable({}, { __call = function () return 4 end }) }))() -- no chaining __call()'s :'(
-- as a result, is_callable() is much simpler, but used to be incredibly clever. :'(
is_callable =
function (what)
if type(what) == 'function' then
return true
end
local what_mt = getmeta(what)
if what_mt ~= nil and type(what_mt.__call) == 'function' then
return true
end
return false
end
-- }}}
-- {{{ valof()
-- call `what' if it is callable until it is not
-- great for chained functions
valof =
function (what)
while is_callable(what) do
what = what()
end
return what
end
-- }}}
-- {{{ lit() & identity() (same function)
-- lit(something) returns something exactly as it was passed
-- lit() -> literal, you wouldn't imagine how useful this is
lit =
function (what)
return what
end
identity = lit
-- }}}