Differences From
Artifact [a1f6282434]:
35 35 function ss.filter(list, fn)
36 36 local new = {}
37 37 for i, v in ipairs(list) do
38 38 if fn(v,i) then table.insert(new, v) end
39 39 end
40 40 return new
41 41 end
42 +function ss.kmap(fn, list)
43 + local new = {}
44 + for k, v in pairs(list) do
45 + local nk,nv = fn(k,v)
46 + new[nk or k] = nv or v
47 + end
48 + return new
49 +end
50 +
51 +function ss.kfilter(list, fn)
52 + local new = {}
53 + for k, v in pairs(list) do
54 + if fn(k,v) then new[k] = v end
55 + end
56 + return new
57 +end
58 +
59 +function ss.find(tbl, pred, ...)
60 + pred = pred or function(a,b)
61 + return a == b
62 + end
63 + for k,v in pairs(tbl) do
64 + if pred(v,k,...) then return k,v end
65 + end
66 + return nil
67 +end
68 +
69 +function ss.clone(tbl) -- performs a shallow copy
70 + if tbl.clone then return tbl:clone() end
71 + local new = {}
72 + for k,v in pairs(tbl) do
73 + new[k] = v
74 + end
75 + return new
76 +end
77 +
78 +function ss.copy(tbl) -- performs a deep copy
79 + local new = {}
80 + for k,v in pairs(tbl) do
81 + if type(v) == 'table' then
82 + if v.clone then
83 + new[k] = v:clone() -- this may or may not do a deep copy!
84 + else
85 + new[k] = ss.copy(v)
86 + end
87 + else
88 + new[k] = v
89 + end
90 + end
91 + return new
92 +end
93 +
94 +function ss.delegate(tbl,tpl) -- returns a table that looks up keys it lacks from
95 + -- tbl (lightweight alternative to shallow copies)
96 + tpl = tpl or {}
97 + return setmetatable({}, {__index=tbl})
98 +end
42 99
43 100 ss.str = {}
44 101
45 102 function ss.str.begins(str, pfx)
46 103 return string.sub(str, 1, #pfx) == pfx
47 104 end
48 105
................................................................................
268 325 ss.str.exn('out of place token ā%sā', stop):throw()
269 326 end
270 327 end
271 328 end
272 329
273 330 ss.str.exn('token ā%sā expected before end of line', stop):throw()
274 331 end
332 +
333 +ss.version = ss.declare {
334 + name = 'version';
335 + mk = function(tbl) return tbl end;
336 + fns = {
337 + pre = function(self,other) end;
338 + post = function(self,other) end;
339 + string = function(self) return tostring(self) end;
340 + };
341 + cast = {
342 + string = function(vers)
343 + if not(next(vers)) then return '0.0' end
344 + local str = ''
345 + for _,v in pairs(vers) do
346 + if type(v) == 'string' then
347 + if str ~= '' then str = str .. '-' end
348 + str = str .. v
349 + else
350 + if str ~= '' then str = str .. '.' end
351 + str = str .. tostring(v)
352 + end
353 + end
354 + return str
355 + end
356 + };
357 +}
358 +
359 +function ss.classinstance(o)
360 + local g = getmetatable(o)
361 + if not o then return nil end
362 + local mm = getmetatable(g)
363 + if not o then return nil end
364 + if mm.__name == 'class' then
365 + return g
366 + else
367 + return nil
368 + end
369 +end
370 +