|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file:
-- https://anggtwu.net/LUA/SortedKeys2.lua.html
-- https://anggtwu.net/LUA/SortedKeys2.lua
-- (find-angg "LUA/SortedKeys2.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- (defun e () (interactive) (find-angg "LUA/SortedKeys2.lua"))
-- (find-angg "LUA/lua50init.lua" "sortedkeys")
-- (find-angg "LUA/lua50init.lua" "Code")
SortedKeys = Class {
type = "SortedKeys",
from = function (tbl,basickeys)
return SortedKeys{}:settbl(tbl):setbasic(basickeys):setlist()
end,
__index = {
sort = function (sk,list) return sorted(list, rawtostring_comp) end,
settbl = function (sk,tbl)
sk.tbl = tbl
sk.allkeys = sk:sort(keys(tbl))
sk.allkeyss = Set.from(sk.allkeys)
return sk
end,
setbasic = function (sk,basickeys)
if basickeys == nil then basickeys = "" end
if type(basickeys) == "string" then basickeys = split(basickeys) end
sk.basickeys = basickeys
sk.basickeyss = Set.from(sk.basickeys)
return sk
end,
setlist = function (sk)
sk.list = VTable {}
for _,basickey in ipairs(sk.basickeys) do
if sk.allkeyss:has(basickey) then table.insert(sk.list,basickey) end
end
for _,key in ipairs(sk.allkeys) do
if not sk.basickeyss:has(key) then table.insert(sk.list,key) end
end
return sk
end,
pairs = function (sk)
return cow(function ()
for _,k in ipairs(sk.list) do coy(k, sk.tbl[k]) end
end)
end,
},
}
-- (find-angg "LUA/PreTraceback1.lua" "DGetPairs")
--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "SortedKeys2.lua"
T = {a=1, b=2, c=3, d=4}
sk = SortedKeys.from(T)
sk = SortedKeys.from(T, "c cc b")
= sk
PPV(sk)
for k,v in sk:pairs() do print(k,v) end
--]]