Created
March 17, 2012 20:35
-
-
Save davidm/2064991 to your computer and use it in GitHub Desktop.
memoize.lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
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