lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Dec 14, 2013, at 10:04 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:

> 
> That's why memoizing tends to be inflexible, and why the simplest and
> most inflexible solution of all (this version is by Steve Donovan):
> 
> function ml.memoize(func)
>    return setmetatable({}, {
>        __index = function(self, k, ...)
>            local v = func(k,...)
>            self[k] = v
>            return v
>        end,
>        __call = function(self, k) return self[k] end
>    })
> end
> 
> is IMHO the only useful way to do it.
> 

Translation: Only pure functions should be candidates for memoization.

—Tim