lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wed, Dec 23, 2009 at 11:18 AM, Frank Meier-Dörnberg <frank@md-web.de> wrote:
> see SimpleTuples <http://lua-users.org/wiki/SimpleTuples> and friends.

interesting, here's my shot at (roughly) the same approach, but
without all the metatables:

---------- tuples.lua ---------
local c = {}

local function tuples(a,b)
	local c2 = c[a]
	if not c2 then
		c2 = {}
		c[a] = c2
	end
	local c3 = c2[b]
	if not c3 then
		c3 = function () return a,b end
		c2[b] = c3
	end
	return c3
end

return tuples
------------------------------

---------- test ----------

tuple = require "tuples"

t1=tuple(1,2)
t2=tuple(2,3)
t3=tuple(1,2)

print (t1())
=> 1    2
print (t2())
=> 2    3
print (t3())
=> 1    3

print (t1==t2, t2==t3, t3==t1)
=> false   false   true

---------------------------------

-- 
Javier