|
On Sun, Dec 15, 2013 at 7:55 AM, Sir Pogsalot <sir.pogsalot@gmail.com> wrote:
On Sat, Dec 14, 2013 at 10:53 PM, Tim Hill <drtimhill@gmail.com> wrote:
In which case you are essentially asserting to yourself that, under acceptable constraints, that expensive network call IS (in effect) a pure function.
On Dec 14, 2013, at 2:01 PM, Petite Abeille <petite.abeille@gmail.com> wrote:
>
> On Dec 14, 2013, at 10:19 PM, Tim Hill <drtimhill@gmail.com> wrote:
>
>> Translation: Only pure functions should be candidates for memoization.
>
> Eh? Since when? I, for one, see great, concrete, tangible benefit in memoizing, say, that very expensive network call for whatever period of time I personally see fit.
>
>
—Tim
Teeeechnically it would be impure every time because networking means I/O. :>Though I'd still be okay with that if it's fetching a static page and the call succeeded (verified from what got returned)... or you know the period between updates to the page so you can memoized.forget() it and rerun. :>Anyway, this got me to thinking that it would be useful to remove the cached return values for a specific call -- if the function is impure and you know the cache needs updating.So then, `clear' in the module I posted becomes:local clear =function (f, ...)-- reset everything if called without argsif not f then-- replace/clear guid table, guid counter, drop function call cachesguids = setmetatable({}, { __mode = 'k' })counter = 0funcs = {}returnend-- only the function was provided, forget the-- cached returns for every call made to `f'if select('#', ...) == 0 thenfuncs[f] = nilreturnend-- an arglist was provided, remove-- this specific call signature cacheif funcs[f] thenfuncs[f][args_to_str(...)] = nilendendIt *might* be useful to forget a specific call while keeping the memoized returns of all others.It allows you to just write: memoize.forget(table.pack, 1, 2, 3)The next memoize(table.pack, 1, 2, 3) will re-cache the returned value.(Let's pretend this was a better example with an impure function)Toodles :-)