Module:Memoizer

local nil_tag = {}

local memoizer = function (func, keyfn) local cache = setmetatable({}, {__mode = 'k'}) if not keyfn then return function (...) local key = ... key = key == nil and nil_tag or key local v = rawget(cache, key) if rawequal(v, nil_tag) then return nil elseif not rawequal(v, nil) then return v			end v = func(...) rawset(cache, key, rawequal(v, nil) and nil_tag or v)			return v		end end

return function (...) local key = keyfn(...) key = key == nil and nil_tag or key local v = rawget(cache, key) if rawequal(v, nil_tag) then return nil elseif not rawequal(v, nil) then return v		end v = func(...) rawset(cache, key, rawequal(v, nil) and nil_tag or v)		return v	end end

return { memoizer = memoizer, }