[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [ANN] Penlight 0.7 Beta
- From: David Manura <dm.lua@...>
- Date: Wed, 13 May 2009 23:41:36 -0400
On Tue, May 12, 2009 at 10:24 AM, steve donovan wrote:
> http://penlight.luaforge.net/ ...
> tablex.difference() set difference
> tablex.set() explicit set generator given a list of values
A generalization of List:extend is the following function [1] which
inserts the elements at any position:
tinsertvalues(t, [pos,] t2)
It takes a form similar to table.insert(t, [pos,] v), except that it
inserts all elements of v rather than v itself.
A function that can often (probably not always) be used as an
alternative to tablex.set is the function that swaps keys with values,
i.e. "inverts" the table in analogous fashion to the mathematical
concept of function inverse (i.e. t[x] == f(x)). It's a very
fundamental operation. The Metalua standard library calls this
table.transpose, but I think that may be a misnomer [2]. Your
tablex.index_map is also related though assumes the inverse of the
function represented by the table is multivalued, so the result is
more complicated (table of tables).
We also lack a function that clears a table (both array and hash
parts) while preserving the table's identity (not creating a new
table). List:clear just clears the array part.
function tablex.clear(t) for k in pairs(t) do t[k] = nil end end
The Metalua standard library has a table.override(t1,t2) function that
overwrites table t1 with table t2. This could be thought of as a
clear followed by an overwrite (union). clear can be expressed as
table.override(t, {}) though. As noted in the manual, this
table.override operation "often comes handy to modify an AST." Here's
the version I've used:
local function tablex.override(dst, src)
for k in pairs(dst) do dst[k] = src[k] end -- or nil (i.e. clear)
for k, v in pairs(src) do dst[k] = v end
return dst
end
[1] http://lua-users.org/wiki/TableUtils
[2] http://en.wikipedia.org/wiki/Transposition_(mathematics)