Memoization and function calls

Hi there.

This is a newbie question. I need to call the following function, lexicalgravitypair(), within a for loop, such as the one below the function definition of lexicalgravitypair(), that depends on the output of 3 other function calls, f(x,y,text), g(x, text) and g(x, text, mode = "backward"). Computing each of these 3 outputs are costly in terms of time and I would like to use memoization and call their cached output. I saw that Memoize.jl is one of the options. However, since am inexperienced with using memoization in general and since there are no example of usage like the one am describing, I don’t know how to use memoization in the scenario I describe; meaning I don’t know where and how to use the memoize_cache() function. Any hint or advice will be greatly appreciated.

function lexicalgravitypair(x::String, y::String, text::String) 
    log(f(x,y,text)/g(x, text)) + log(f(x,y,text)/g(x, text, mode = "backward"))

for ngram in ngrams:
   if lexicalgravitypair(ngram[1], ngram[2], text) >= 5.5

Actually, you could try this for a start

@memoize function f()

Basicallly just add @memoize to your definition of f, g and that’s it.


The memoize_cache method was added to allow for deleting the cache, but, indeed, @memoize is all one needs for most use-cases.

1 Like

I am not used to using the @memoize macro and thought that there was something more needed to add in order for memoization to take place each and every time I would have to call the lexicalgravitypair() function.

Thanks for your comment and the info about the memoize_cache() method that now starts to make sense to me.