From 8d5da0a9ff2d8f2b8ac4f0e1c47df88a3981fa76 Mon Sep 17 00:00:00 2001 From: Max Cahill <1bardesign@gmail.com> Date: Wed, 8 Apr 2020 20:50:51 +1000 Subject: [PATCH] [modified] moved append and append_inplace from functional to tablex --- functional.lua | 20 ++------------ tablex.lua | 71 +++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 58 insertions(+), 33 deletions(-) diff --git a/functional.lua b/functional.lua index 15723b9..d855a62 100644 --- a/functional.lua +++ b/functional.lua @@ -126,22 +126,6 @@ function functional.dedupe(t) end) end ---append sequence t2 into t1, modifying t1 -function functional.append_inplace(t1, t2) - for i,v in ipairs(t2) do - table.insert(t1, v) - end - return t1 -end - ---return a new sequence with the elements of both t1 and t2 -function functional.append(t1, t2) - local r = {} - functional.append_inplace(r, t1) - functional.append_inplace(r, t2) - return r -end - ----------------------------------------------------------- --common queries and reductions ----------------------------------------------------------- @@ -278,8 +262,8 @@ functional.find_best = functional.find_max --return the element of the table that results in the value nearest to the passed value --todo: optimise as this generates a closure each time function functional.find_nearest(t, f, v) - return functional.find_best(t, function(e) - return -math.abs(f(e) - v) + return functional.find_min(t, function(e) + return math.abs(f(e) - v) end) end diff --git a/tablex.lua b/tablex.lua index 213335d..0a4059a 100644 --- a/tablex.lua +++ b/tablex.lua @@ -127,22 +127,36 @@ function tablex.values(t) return r end +--append sequence t2 into t1, modifying t1 +function tablex.append_inplace(t1, t2) + for i,v in ipairs(t2) do + table.insert(t1, v) + end + return t1 +end + +--return a new sequence with the elements of both t1 and t2 +function tablex.append(t1, t2) + local r = {} + tablex.append_inplace(r, t1) + tablex.append_inplace(r, t2) + return r +end + --(might already exist depending on luajit) -if tablex.clear == nil then - if tablex ~= table and table.clear then - --import from global if it exists - tablex.clear = table.clear - else - --remove all values from a table - --useful when multiple references are floating around - --so you cannot just pop a new table out of nowhere - function tablex.clear(t) - assert(type(t) == "table", "table.clear - argument 't' must be a table") - local k = next(t) - while k ~= nil do - t[k] = nil - k = next(t) - end +if table.clear then + --import from global if it exists + tablex.clear = table.clear +else + --remove all values from a table + --useful when multiple references are being held + --so you cannot just create a new table + function tablex.clear(t) + assert(type(t) == "table", "table.clear - argument 't' must be a table") + local k = next(t) + while k ~= nil do + t[k] = nil + k = next(t) end end end @@ -198,6 +212,33 @@ function tablex.overlay(to, from) return to end +--turn a table into a vaguely easy to read string +--which is also able to be parsed by lua in most cases +function tablex.stringify(t) + if type(t) ~= "table" then + return tostring(t) + end + local chunks = {} + local seen = {} + --sequential part first + for i, v in ipairs(t) do + seen[i] = true + table.insert(chunks, tablex.stringify(v)) + end + --non sequential follows + for k, v in pairs(t) do + if not seen[k] then + --encapsulate anything that's not a string + --todo: also keywords + if type(k) ~= "string" then + k = "[" .. tostring(k) .. "]" + end + table.insert(chunks, k .. " = " .. tablex.stringify(v)) + end + end + return "{" .. table.concat(chunks, ", ") .. "}" +end + --faster unpacking for known-length tables up to 8 --gets around nyi in luajit --note: you can use a larger unpack than you need as the rest