lua-users home
lua-l archive

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


Alternatively, if you don't care about speed/memory, you can create the signature of a tuple and use that as a unique hash. Off the top of my slow head:

local function sig(...)
local n = select('#', ...)
local s = ''
for i = 1, n do
local e = select(i, ...)
local te = e == nil and '.' or type(e) == 'thread' and 'c' or type(e):sub(1,1)
if type(e) == 'string' then te = te..#e end
s = s..te..tostring(e)
end
return s
end

local t = {}
t[sig(1, 0/0, 'abc', nil)] = true
assert(t[sig(1, 0/0, 'abc', nil)])


[Shouldn't Lua have built-in tuples by now? :) Tuples are very useful in application code, but hardly ever present in library code so I can see how they could be undervalued by low-level coders. As I move between being a lib coder and an app coder I sometimes wonder/worry about these differences in perspective and how they affect the APIs that we create for app usage. I admit that I don't always drive API design from real use cases myself but I try goddamn it.]