Differences From
Artifact [4e80a60df8]:
- File
lib/tbl.lua
— part of check-in
[3f6a913e4e]
at
2020-09-29 12:40:28
on branch trunk
— * remove former hacky registration system, replace with consistent and flexible API; rewrite metal/gem generation to take advantage of this new API; tweaks to init system to enable world-local tweaks to lore and sorcery behavior
* initial documentation commit
* initial steps towards calendar - add default date format, astrolabe; prepare infra for division/melding/transmutation spells, various tweaks and fixes
(user:
lexi,
size: 2543)
[annotate]
[blame]
[check-ins using]
- File
lib/tbl.lua
— part of check-in
[ea6e475e44]
at
2020-10-19 09:52:11
on branch trunk
— continue dev on celestial mechanics, add melding+division spells (resonance), refine itemclasses, add keypunch and punchcards, add paper pulp, add a shitload of visuals, add convenience scripts for working with the wiki, make the flamebolt spell actually useful instead of just a pretty lightshow, add essences, inferno crystal, and other goodies; iterate on wands, lots of shit i can't remember, various bugfixes
(user:
lexi,
size: 3607)
[annotate]
[blame]
[check-ins using]
1 1 local fn = {}
2 2
3 3 fn.shuffle = function(list)
4 4 for i = #list, 2, -1 do
5 5 local j = math.random(i)
6 6 list[i], list[j] = list[j], list[i]
7 7 end
8 + return list
9 +end
10 +
11 +fn.cshuf = function(list)
12 + return fn.shuffle(table.copy(list))
13 +end
14 +
15 +fn.urnd = function(min,max)
16 + local r = {}
17 + for i=min,max do r[1 + (i - min)] = i end
18 + fn.shuffle(r)
19 + return r
20 +end
21 +
22 +fn.uniq = function(lst)
23 + local hash = {}
24 + local new = {}
25 + for i,v in ipairs(lst) do
26 + if not hash[v] then
27 + hash[v] = true
28 + new[#new+1] = v
29 + end
30 + end
31 + return new
8 32 end
9 33
10 34 fn.scramble = function(list)
11 35 local new = table.copy(list)
12 36 fn.shuffle(new)
13 37 return new
14 38 end
................................................................................
36 60 local new = fn.copy(base)
37 61 for k,v in pairs(override) do
38 62 new[k] = v
39 63 end
40 64 return new
41 65 end
42 66
43 -fn.deepmerge = function(base,override)
67 +fn.deepmerge = function(base,override,func)
44 68 local new = {}
45 69 local keys = fn.merge(fn.keys(base),fn.keys(override))
46 70 for _,k in pairs(keys) do
47 71 if type(base[k]) == 'table' and
48 72 type(override[k]) == 'table' then
49 - new[k] = fn.deepmerge(base[k], override[k])
73 + new[k] = fn.deepmerge(base[k], override[k], func)
74 + elseif func and override[k] and base[k] then
75 + new[k] = func(base[k],override[k], k)
50 76 elseif override[k] then
51 77 new[k] = override[k]
52 78 else
53 79 new[k] = base[k]
54 80 end
55 81 end
56 82 return new
................................................................................
81 107 ks[#ks + 1] = k
82 108 end
83 109 return ks
84 110 end
85 111
86 112 fn.pick = function(lst)
87 113 local keys = fn.keys(lst)
88 - return keys[math.random(#keys)]
114 + local k = keys[math.random(#keys)]
115 + return k, lst[k]
89 116 end
90 117
91 118 fn.unpack = function(tbl,i)
92 - if i and #tbl == i then return tbl[i] end
93 119 i = i or 1
120 + if #tbl == i then return tbl[i] end
94 121 return tbl[i], fn.unpack(tbl, i+1)
95 122 end
96 123
97 -fn.each = function(tbl,fn)
124 +fn.split = function(...) return fn.unpack(sorcery.lib.str.explode(...)) end
125 +
126 +fn.each = function(tbl,f)
98 127 local r = {}
99 128 for k,v in pairs(tbl) do
100 - r[#r+1] = fn(v,k)
129 + local v, c = f(v,k)
130 + r[#r+1] = v
131 + if c == false then break end
101 132 end
102 133 return r
103 134 end
104 135
105 -fn.each_o = function(tbl,fn)
136 +fn.each_o = function(tbl,f)
106 137 local keys = fn.keys(tbl)
107 138 table.sort(keys)
108 139 return fn.each(keys, function(k,i)
109 - return fn(tbl[k],k,i)
140 + return f(tbl[k],k,i)
110 141 end)
111 142 end
112 143
113 144 fn.iter = function(tbl,fn)
114 145 for i=1,#tbl do
115 146 fn(tbl[i], i)
116 147 end
................................................................................
137 168 end
138 169 return acc
139 170 end
140 171
141 172 fn.walk = function(tbl,path)
142 173 if type(path) == 'table' then
143 174 for _,p in pairs(path) do
144 - if tbl[p] == nil then return nil end
175 + if tbl == nil or tbl[p] == nil then return nil end
145 176 tbl = tbl[p]
146 177 end
147 178 else
148 179 tbl = tbl[path]
149 180 end
150 181 return tbl
151 182 end
183 +
184 +fn.proto = function(tbl,proto)
185 + local meta = getmetatable(tbl)
186 + local nm = {__index = proto or tbl}
187 + if meta ~= nil then
188 + nm = table.copy(meta)
189 + nm[__index] = proto
190 + nm[__metatable] = meta
191 + end
192 + return setmetatable(tbl or {},nm)
193 +end
194 +
195 +fn.case = function(e, c)
196 + if type(c[e]) == 'function'
197 + then return (c[e])(e)
198 + else return c[e]
199 + end
200 +end
201 +
202 +fn.cond = function(exp, c)
203 + for i, v in ipairs(c) do
204 + if c[1](exp) then return c[2](exp) end
205 + end
206 +end
152 207
153 208 return fn