diff options
Diffstat (limited to '.config/awesome/quarrel/table.lua')
-rw-r--r-- | .config/awesome/quarrel/table.lua | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/.config/awesome/quarrel/table.lua b/.config/awesome/quarrel/table.lua new file mode 100644 index 0000000..13ccbce --- /dev/null +++ b/.config/awesome/quarrel/table.lua @@ -0,0 +1,59 @@ +local qtable = {} + +function qtable.map(t, f) + local nt = {} + for k,v in pairs(t) do + nt[k] = f(v) + end + return nt +end + +function qtable.filter(t, f, dict) + local nt = {} + for k,v in pairs(t) do + if not f(v) then goto continue end + if dict then + nt[k] = v + else + table.insert(nt, v) + end + ::continue:: + end + return nt +end + +-- This is taken from http://lua-users.org/wiki/SortedIteration +-- This version is stripped of comments and empty lines + some stuff is renamed + +local function __gen_oindex(t) + local oindex = {} + for key in pairs(t) do + table.insert(oindex, key) + end + table.sort(oindex) + return oindex +end + +function qtable.onext(t, state) + local key = nil + if state == nil then + t.__oindex = __gen_oindex(t) + key = t.__oindex[1] + else + for i = 1,#t.__oindex do + if t.__oindex[i] == state then + key = t.__oindex[i+1] + end + end + end + if key then + return key, t[key] + end + t.__oindex = nil +end + +function qtable.opairs(t) + return qtable.onext, t, nil +end + +return qtable |