Skip to content

Instantly share code, notes, and snippets.

@davidm
Created March 17, 2012 20:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidm/2064991 to your computer and use it in GitHub Desktop.
Save davidm/2064991 to your computer and use it in GitHub Desktop.
memoize.lua
--[[
memoize.lua
http://lua-users.org/wiki/FuncTables
http://www.lua.org/pil/17.1.html
Code is public domain.
D.Manura.
--]]
local M = {}
function M.memoize(f)
local mt = {}
local t = setmetatable({}, mt)
function mt:__index(k)
local v = f(k); t[k] = v
return v
end
return t
end
return M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment