|
Am 14.12.2013 12:45 schröbte Sir Pogsalot:
Hoy.
Hi!
Writing this because I have seen a lot of inflexible attempts at memoizing function calls out there. :> Mostly I wanted to tackle these two problems: - preserving nil in a call with multiple arguments - preserving nil in a function returning multiple values This is what I came up with (to be used as a module): -- a table to track all objects passed to any memoized functions local guids = setmetatable({}, { __mode = 'k' })
You probably already know this, but weak tables only work for garbage collected reference types with explicit constructions[1], e.g. tables and userdata, but not numbers, booleans, strings, and light C functions.
[...] Of the memoize attempts I've seen supporting multiple-argument calls, I usually see people converting each argument to a string and just concatenating them
The last two implementations listed here[2] don't do that. One uses object equality and some form of lookup tree, while the other uses object identity and nested tables. You should add your implementation to that page after you have fixed the following bug: `func()` and `func(nil)` are mapped to the same signature.
Toodles :-)
Philipp [1]: http://www.lua.org/manual/5.2/manual.html#2.5.2 [2]: http://lua-users.org/wiki/FuncTables