blob: 0c327fd194c6b7f62ec289367666ef2a7f0b301a (
plain) (
blame)
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
|
------------------------------------------------------------------------------
-- util.lua
-- Lua utilities.
------------------------------------------------------------------------------
util = { }
function util.catlist(...)
local res = { }
local tables = { ... }
if #tables == 1 then
return tables[1]
else
for _, tab in ipairs(tables) do
for _, val in ipairs(tab) do
table.insert(res, val)
end
end
end
return res
end
function util.cathash(...)
local res = { }
local tables = { ... }
if #tables == 1 then
return tables[1]
else
for _, tab in ipairs(tables) do
for key, val in ipairs(tab) do
res[key] = val
end
end
end
return res
end
function util.map(fn, ...)
local lists = { ... }
local res = { }
if #lists == 0 then
return res
elseif #lists == 1 then
for _, val in ipairs(lists[1]) do
table.insert(res, fn(val))
end
else
for i = 1, #lists[1] do
local args = { }
for _, list in ipairs(lists) do
if not list[i] then
break
end
table.insert(args, list[i])
end
if #args < #lists then
break
end
table.insert(res, fn(unpack(args)))
end
end
return res
end
function util.random_from(list)
return list[ crawl.random2(#list) + 1 ]
end
|