Created
January 28, 2014 19:07
-
-
Save abaez/8674051 to your computer and use it in GitHub Desktop.
memoize function example
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
-- example of Memoize function with color generation. Saves speed! | |
-- saves colors being used, and appends with new colors. | |
local results = {} | |
setmetatable(results, {__mode = "v"}) | |
function create_RGB(r,g,b) | |
-- defining key by the colors used | |
local key = string.format("%d-%d-%d", r,g,b) | |
local color = results[key] | |
if color == nil then | |
color = {red = r, green = g, blue = b} | |
results[key] = color | |
end | |
return color | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment