1
2
3
4
5
6
7
8
9
10
11
12
13
14
..
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
..
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
...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
local fn = {}
fn.shuffle = function(list)
for i = #list, 2, -1 do
local j = math.random(i)
list[i], list[j] = list[j], list[i]
end
end
fn.scramble = function(list)
local new = table.copy(list)
fn.shuffle(new)
return new
end
................................................................................
local new = fn.copy(base)
for k,v in pairs(override) do
new[k] = v
end
return new
end
fn.deepmerge = function(base,override)
local new = {}
local keys = fn.merge(fn.keys(base),fn.keys(override))
for _,k in pairs(keys) do
if type(base[k]) == 'table' and
type(override[k]) == 'table' then
new[k] = fn.deepmerge(base[k], override[k])
elseif override[k] then
new[k] = override[k]
else
new[k] = base[k]
end
end
return new
................................................................................
ks[#ks + 1] = k
end
return ks
end
fn.pick = function(lst)
local keys = fn.keys(lst)
return keys[math.random(#keys)]
end
fn.unpack = function(tbl,i)
if i and #tbl == i then return tbl[i] end
i = i or 1
return tbl[i], fn.unpack(tbl, i+1)
end
fn.each = function(tbl,fn)
local r = {}
for k,v in pairs(tbl) do
r[#r+1] = fn(v,k)
end
return r
end
fn.each_o = function(tbl,fn)
local keys = fn.keys(tbl)
table.sort(keys)
return fn.each(keys, function(k,i)
return fn(tbl[k],k,i)
end)
end
fn.iter = function(tbl,fn)
for i=1,#tbl do
fn(tbl[i], i)
end
................................................................................
end
return acc
end
fn.walk = function(tbl,path)
if type(path) == 'table' then
for _,p in pairs(path) do
if tbl[p] == nil then return nil end
tbl = tbl[p]
end
else
tbl = tbl[path]
end
return tbl
end
return fn
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
>
>
|
>
<
>
>
>
|
>
|
>
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
..
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
...
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
...
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
|
local fn = {}
fn.shuffle = function(list)
for i = #list, 2, -1 do
local j = math.random(i)
list[i], list[j] = list[j], list[i]
end
return list
end
fn.cshuf = function(list)
return fn.shuffle(table.copy(list))
end
fn.urnd = function(min,max)
local r = {}
for i=min,max do r[1 + (i - min)] = i end
fn.shuffle(r)
return r
end
fn.uniq = function(lst)
local hash = {}
local new = {}
for i,v in ipairs(lst) do
if not hash[v] then
hash[v] = true
new[#new+1] = v
end
end
return new
end
fn.scramble = function(list)
local new = table.copy(list)
fn.shuffle(new)
return new
end
................................................................................
local new = fn.copy(base)
for k,v in pairs(override) do
new[k] = v
end
return new
end
fn.deepmerge = function(base,override,func)
local new = {}
local keys = fn.merge(fn.keys(base),fn.keys(override))
for _,k in pairs(keys) do
if type(base[k]) == 'table' and
type(override[k]) == 'table' then
new[k] = fn.deepmerge(base[k], override[k], func)
elseif func and override[k] and base[k] then
new[k] = func(base[k],override[k], k)
elseif override[k] then
new[k] = override[k]
else
new[k] = base[k]
end
end
return new
................................................................................
ks[#ks + 1] = k
end
return ks
end
fn.pick = function(lst)
local keys = fn.keys(lst)
local k = keys[math.random(#keys)]
return k, lst[k]
end
fn.unpack = function(tbl,i)
i = i or 1
if #tbl == i then return tbl[i] end
return tbl[i], fn.unpack(tbl, i+1)
end
fn.split = function(...) return fn.unpack(sorcery.lib.str.explode(...)) end
fn.each = function(tbl,f)
local r = {}
for k,v in pairs(tbl) do
local v, c = f(v,k)
r[#r+1] = v
if c == false then break end
end
return r
end
fn.each_o = function(tbl,f)
local keys = fn.keys(tbl)
table.sort(keys)
return fn.each(keys, function(k,i)
return f(tbl[k],k,i)
end)
end
fn.iter = function(tbl,fn)
for i=1,#tbl do
fn(tbl[i], i)
end
................................................................................
end
return acc
end
fn.walk = function(tbl,path)
if type(path) == 'table' then
for _,p in pairs(path) do
if tbl == nil or tbl[p] == nil then return nil end
tbl = tbl[p]
end
else
tbl = tbl[path]
end
return tbl
end
fn.proto = function(tbl,proto)
local meta = getmetatable(tbl)
local nm = {__index = proto or tbl}
if meta ~= nil then
nm = table.copy(meta)
nm[__index] = proto
nm[__metatable] = meta
end
return setmetatable(tbl or {},nm)
end
fn.case = function(e, c)
if type(c[e]) == 'function'
then return (c[e])(e)
else return c[e]
end
end
fn.cond = function(exp, c)
for i, v in ipairs(c) do
if c[1](exp) then return c[2](exp) end
end
end
return fn
|