1 local List, Dict, Undict, _undict_mt, _dict_mt
2 local insert, remove, concat
5 insert, remove, concat = _obj_0.insert, _obj_0.remove, _obj_0.concat
8 as_nomsu = function(self)
9 if type(self) == 'number' then
13 local mt = getmetatable(self)
16 local _as_nomsu = mt.as_nomsu
18 return _as_nomsu(self)
26 as_lua = function(self)
27 if type(self) == 'number' then
31 local mt = getmetatable(self)
34 local _as_lua = mt.as_lua
44 nth_to_last = function(self, n)
45 return self[#self - n + 1]
49 __eq = function(self, other)
50 if not (type(other) == 'table' and getmetatable(other) == getmetatable(self) and #other == #self) then
53 for i, x in ipairs(self) do
54 if not (x == other[i]) then
60 __tostring = function(self)
61 return "[" .. concat((function()
64 for _index_0 = 1, #self do
65 local b = self[_index_0]
66 _accum_0[_len_0] = as_nomsu(b)
72 as_nomsu = function(self)
73 return "[" .. concat((function()
76 for _index_0 = 1, #self do
77 local b = self[_index_0]
78 _accum_0[_len_0] = as_nomsu(b)
84 as_lua = function(self)
85 return "a_List{" .. concat((function()
88 for _index_0 = 1, #self do
89 local b = self[_index_0]
90 _accum_0[_len_0] = as_lua(b)
96 __lt = function(self, other)
97 assert(type(self) == 'table' and type(other) == 'table', "Incompatible types for comparison")
98 for i = 1, math.max(#self, #other) do
99 if not self[i] and other[i] then
101 elseif self[i] and not other[i] then
103 elseif self[i] < other[i] then
105 elseif self[i] > other[i] then
111 __le = function(self, other)
112 assert(type(self) == 'table' and type(other) == 'table', "Incompatible types for comparison")
113 for i = 1, math.max(#self, #other) do
114 if not self[i] and other[i] then
116 elseif self[i] and not other[i] then
118 elseif self[i] < other[i] then
120 elseif self[i] > other[i] then
126 __add = function(self, other)
127 local ret = List((function()
130 for _index_0 = 1, #self do
131 local x = self[_index_0]
137 for _index_0 = 1, #other do
138 local x = other[_index_0]
146 add_1_at_index = function(t, x, i)
147 return insert(t, i, x)
149 at_index_1_add = insert,
151 remove_last = remove,
152 remove_index = remove,
153 last = (function(self)
156 first = (function(self)
159 _1_st_to_last = nth_to_last,
160 _1_nd_to_last = nth_to_last,
161 _1_rd_to_last = nth_to_last,
162 _1_th_to_last = nth_to_last,
163 joined = function(self)
164 return table.concat((function()
167 for _index_0 = 1, #self do
168 local x = self[_index_0]
169 _accum_0[_len_0] = tostring(x)
175 joined_with = function(self, glue)
176 return table.concat((function()
179 for _index_0 = 1, #self do
180 local x = self[_index_0]
181 _accum_0[_len_0] = tostring(x)
187 has = function(self, item)
188 for _index_0 = 1, #self do
189 local x = self[_index_0]
196 remove = function(self, item)
197 for i, x in ipairs(self) do
203 index_of = function(self, item)
204 for i, x in ipairs(self) do
211 from_1_to = function(self, start, stop)
214 start = (n + 1 - start)
217 stop = (n + 1 - stop)
219 return List((function()
222 for i = start, stop do
223 _accum_0[_len_0] = self[i]
229 from = function(self, start)
230 return self:from_1_to(start, -1)
232 up_to = function(self, stop)
233 return self:from_1_to(1, stop)
235 copy = function(self)
236 return List((function()
240 _accum_0[_len_0] = self[i]
246 reverse = function(self)
248 for i = 1, math.floor(n / 2) do
249 self[i], self[n - i + 1] = self[n - i + 1], self[i]
252 reversed = function(self)
253 return List((function()
256 for i = #self, 1, -1 do
257 _accum_0[_len_0] = self[i]
263 sort = function(self)
264 return table.sort(self)
266 sort_by = function(self, fn)
267 local keys = setmetatable({ }, {
268 __index = function(self, k)
274 return table.sort(self, function(a, b)
275 return keys[a] <= keys[b]
278 sorted = function(self)
279 local c = self:copy()
283 sorted_by = function(self, fn)
284 local c = self:copy()
288 filter_by = function(self, keep)
291 if not (keep(self[i])) then
292 deleted = deleted + 1
293 elseif deleted > 0 then
294 self[i - deleted] = self[i]
297 for i = #self - deleted + 1, #self do
301 filtered_by = function(self, keep)
302 local c = self:copy()
307 __newindex = function(self, k, v)
308 assert(type(k) == 'number', "List indices must be numbers")
309 return rawset(self, k, v)
312 _list_mt.__index.as_lua = _list_mt.as_lua
313 _list_mt.__index.as_nomsu = _list_mt.as_nomsu
314 List = function(t, ...)
316 if type(t) == 'table' then
317 l = setmetatable(t, _list_mt)
318 elseif type(t) == 'function' then
319 l = setmetatable({ }, _list_mt)
322 for i = 1, select('#', ...) do
323 l[#l + 1] = select(i, ...)
327 elseif type(t) == 'thread' then
328 l = setmetatable({ }, _list_mt)
329 for val in coroutine.wrap(t) do
333 error("Unsupported List type: " .. type(t))
335 if select(1, ...) then
336 local _list_0 = List(...)
337 for _index_0 = 1, #_list_0 do
338 local x = _list_0[_index_0]
344 local compliments = setmetatable({ }, {
348 __type = "an Inverse Dict",
349 __index = function(self, k)
350 return not compliments[self][k] and true or nil
352 __newindex = function(self, k, v)
354 compliments[self][k] = nil
356 compliments[self][k] = true
359 __eq = function(self, other)
360 if not (type(other) == 'table' and getmetatable(other) == getmetatable(self)) then
363 return compliments[self] == compliments[other]
365 __len = function(self)
368 __tostring = function(self)
369 return "~" .. _dict_mt.__tostring(compliments[self])
371 as_nomsu = function(self)
372 return "~" .. _dict_mt.as_nomsu(compliments[self])
374 as_lua = function(self)
375 return "~" .. __dict_mt.as_lua(compliments[self])
377 __band = function(self, other)
378 if getmetatable(other) == _undict_mt then
379 return Undict(_dict_mt.__bor(compliments[self], compliments[other]))
381 return _dict_mt.__band(other, self)
384 __bor = function(self, other)
385 if getmetatable(other) == _undict_mt then
386 return Undict(_dict_mt.__band(compliments[self], compliments[other]))
388 return Undict((function()
390 for k, v in pairs(compliments[self]) do
399 __bxor = function(self, other)
400 if getmetatable(other) == _undict_mt then
401 return _dict_mt.__bxor(compliments[self], compliments[other])
403 return Undict(_dict_mt.__band(other, self))
406 __bnot = function(self)
407 return Dict((function()
409 for k, v in pairs(compliments[self]) do
417 local u = setmetatable({ }, _undict_mt)
418 compliments[u] = Dict((function()
420 for k, v in pairs(d) do
431 __eq = function(self, other)
432 if not (type(other) == 'table' and getmetatable(other) == getmetatable(self)) then
435 for k, v in pairs(self) do
436 if not (v == other[k]) then
440 for k, v in pairs(other) do
441 if not (v == self[k]) then
447 __len = function(self)
449 for _ in pairs(self) do
454 __tostring = function(self)
455 return "{" .. concat((function()
458 for k, v in pairs(self) do
459 _accum_0[_len_0] = v == true and "." .. as_nomsu(k) or "." .. tostring(k) .. " = " .. tostring(v)
465 as_nomsu = function(self)
466 return "{" .. concat((function()
469 for k, v in pairs(self) do
470 _accum_0[_len_0] = v == true and "." .. as_nomsu(k) or "." .. tostring(as_nomsu(k)) .. " = " .. tostring(as_nomsu(v))
476 as_lua = function(self)
477 return "a_Dict{" .. concat((function()
480 for k, v in pairs(self) do
481 _accum_0[_len_0] = "[ " .. tostring(as_lua(k)) .. "]= " .. tostring(as_lua(v))
487 __inext = function(self, key)
488 local nextkey, value = next(self, key)
489 if nextkey ~= nil then
490 return nextkey, Dict({
496 __band = function(self, other)
497 return Dict((function()
499 for k, v in pairs(self) do
507 __bor = function(self, other)
508 if getmetatable(other) == _undict_mt then
509 return _undict_mt.__bor(other, self)
511 local ret = Dict((function()
513 for k, v in pairs(self) do
518 for k, v in pairs(other) do
519 if ret[k] == nil then
525 __bxor = function(self, other)
526 if getmetatable(other) == _undict_mt then
527 return _undict_mt.__bxor(other, self)
529 local ret = Dict((function()
531 for k, v in pairs(self) do
536 for k, v in pairs(other) do
537 if ret[k] == nil then
546 __add = function(self, other)
547 local ret = Dict((function()
549 for k, v in pairs(self) do
554 for k, v in pairs(other) do
555 if ret[k] == nil then
563 __sub = function(self, other)
564 local ret = Dict((function()
566 for k, v in pairs(self) do
571 for k, v in pairs(other) do
572 if ret[k] == nil then
581 Dict = function(t, more, ...)
583 if type(t) == 'table' then
584 d = setmetatable(t, _dict_mt)
585 elseif type(t) == 'function' then
586 d = setmetatable({ }, _dict_mt)
589 for i = 1, select('#', ...) do
590 d[select(i, ...)] = true
594 add_1_eq_2 = function(k, v)
598 elseif type(t) == 'thread' then
599 d = setmetatable({ }, _dict_mt)
600 for k, v in coroutine.wrap(t) do
604 error("Unsupported Dict type: " .. type(t))
607 for k, v in pairs(Dict(more, ...)) do