lua-users home
lua-l archive

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



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:

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.
>
>

In which case you are essentially asserting to yourself that, under acceptable constraints, that expensive network call IS (in effect) a pure function.

—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 args
        if not f then
            -- replace/clear guid table, guid counter, drop function call caches
            guids   = setmetatable({}, { __mode = 'k' })
            counter = 0
            funcs   = {}
            return  
        end 
        
        -- only the function was provided, forget the
        -- cached returns for every call made to `f'
        if select('#', ...) == 0 then
            funcs[f] = nil
            return
        end 
        
        -- an arglist was provided, remove
        -- this specific call signature cache
        if funcs[f] then
            funcs[f][args_to_str(...)] = nil
        end 
    end 

It *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 :-)