• Subject: Lua tuples. was Re: Lua Tutor? Porting Python code to Lua.
• From: Björn De Meyer <bjorn.demeyer@...>
• Date: Sun, 22 Sep 2002 17:49:09 +0200

```Don't do tomorrow what you can do today. Here is
my short,  incomplete, but iuuseful tuple library written
for LUA 5 alpha. It's freely usable for all. Comments and
improvements are welcome. Maybe someone could
upload it at the wiki as well?

Oh, and I hope the "__eq" metamethod will remain supported.
It's much easier to do equality when you have that.

-- begin lua.ntuple

--
--  ntuple.lua Generic n-tuple support in LUA 5 alpha.
--  Written by Bjorn De Meyer, 2002.
--  I hereby release this source coude to the public domain.

tuple = {};

tuple.COMPARE_SHORTER = -2
tuple.COMPARE_SMALLER = -1
tuple.COMPARE_EQUAL   = 0
tuple.COMPARE_BIGGER  = 1
tuple.COMPARE_LONGER  = 2

-- zero means equal, positive means t1 is bigger,
-- negative means t1 is smaller

function tuple.compare(t1,t2)
local n1, n2, index;
n1 = table.getn(t1);
n2 = table.getn(t2);
if (n1>n2) then return tuple.COMPARE_LONGER;
elseif (n1<n2) then return tuple.COMPARE_SHORTER;
end
for index =1, n1 do
if(t1[index] > t2[index]) then return tuple.COMPARE_BIGGER
elseif(t1[index] < t2[index]) then return tuple.COMPARE_SMALLER
end
end
return tuple.COMPARE_EQUAL;
end

-- a "walk" function.
-- handy for printing and such...

function tuple:walk(f, ...)
local index;
for index = 1, table.getn(self) do
f(self[index], unpack(arg))
end
end

tuple.meta  = { } ; -- new metatable, but empty, for now.

-- set up a table to be a tuple

function tuple.maketuple(t)
t.walk = tuple.walk
-- copy some methods.
setmetatable(t, tuple.meta)
-- set correct metatable.
return t
end

-- Arihmetic. Does not do any checking.

local result = {};
print(t1, t2)
for index = 1, table.getn(t1)  do
result[index] = t1[index] + t2[index]
end
return(tuple.maketuple(result))
end

function tuple.sub(t1, t2)
local result = {};
for index = 1,table.getn(t1) do
result[index] = t1[index] - t2[index]
end
return(tuple.maketuple(result))
end

function tuple.mul(t1, t2)
local result = {};
for index = 1,table.getn(t1) do
result[index] = t1[index] * t2[index]
end
return(tuple.maketuple(result))
end

function tuple.div(t1, t2)
local result = {};
for index = 1,table.getn(t1) do
result[index] = t1[index] / t2[index]
end
return(tuple.maketuple(result))
end

function tuple.pow(t1, t2)
local result = {};
for index = 1,table.getn(t1) do
result[index] = t1[index] ^ t2[index]
end
return(tuple.maketuple(result))
end

function tuple.unm(t1, t2)
local result = {};
for index = 1,table.getn(t1) do
result[index] = - t1[index];
end
return(tuple.maketuple(result))
end

-- Comparison

function tuple.lt(t1, t2)
local result;
return tuple.compare(t1,t2) < tuple.COMPARE_EQUAL;
end

function tuple.le(t1, t2)
local result;
return tuple.compare(t1,t2) < tuple.COMPARE_BIGGER;
end

function tuple.eq(t1, t2)
local result;
return tuple.compare(t1,t2) == tuple.COMPARE_EQUAL;
end

-- Construction

function tuple.new(...)
local result, k, v, index;
index = 0; result = {};
for k, v in ipairs(arg) do
index = index + 1
rawset(result,index,v);
-- store values of the arguments in a table, indexed as array
end
table.setn(result, index)
tuple.maketuple(result)
return result;
end

-- Type checking.

function tuple.istuple(t)
return getmetatable(t) == tuple.meta;
end

tuple.meta.__mul = tuple.mul;
tuple.meta.__div = tuple.div;
tuple.meta.__sub = tuple.sub;
tuple.meta.__pow = tuple.pow;
tuple.meta.__unm = tuple.unm;
tuple.meta.__lt = tuple.lt;
tuple.meta.__eq = tuple.eq;
-- set up metamethods.

-- finally, make a shortcut
NTUPLE = tuple.new;

-- Cut here, the rest are examples.

tt= NTUPLE(1, 2 ,5);
t2= NTUPLE(7, 5 ,2);
t3 = NTUPLE(1 ,2, 5)
res = tt ^ t2
res2 = -tt;
res:walk(io.write,",");
res2:walk(io.write,",");
print("");
print(tuple.istuple(res));
print(tt == t3);

--
"No one knows true heroes, for they speak not of their greatness." --
Daniel Remar.
Björn De Meyer
bjorn.demeyer@pandora.be

```

• References: