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