# Memoization and function calls

**URL:** <https://discourse.julialang.org/t/memoization-and-function-calls/63218>\
**Category:** New to Julia\
**Tags:** question, memoize\
**Created:** [June 20, 2021, 7:56am UTC](https://discourse.julialang.org/t/memoization-and-function-calls/63218 "2021-06-20T07:56:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Tantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_tantos/32/10636_2.png) [@Alex\_Tantos](https://discourse.julialang.org/u/Alex_Tantos)\
**Post date:** [June 20, 2021, 7:56am UTC](https://discourse.julialang.org/t/memoization-and-function-calls/63218/1 "2021-06-20T07:56:18Z")

</div>

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.

```julia
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"))
end

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

```

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [June 20, 2021, 12:07pm UTC](https://discourse.julialang.org/t/memoization-and-function-calls/63218/2 "2021-06-20T12:07:18Z")

</div>

> [@Alex\_Tantos](#):
>
> I don’t know how to use memoization in the scenario I describe;

Actually, you could try this for a start

```julia
@memoize function f()
 ...
end

```

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

---

<div class="post-metadata">

**Author:** ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)\
**Post date:** [June 20, 2021, 12:29pm UTC](https://discourse.julialang.org/t/memoization-and-function-calls/63218/3 "2021-06-20T12:29:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Alex\_Tantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_tantos/32/10636_2.png) [@Alex\_Tantos](https://discourse.julialang.org/u/Alex_Tantos)\
**Post date:** [June 20, 2021, 3:42pm UTC](https://discourse.julialang.org/t/memoization-and-function-calls/63218/4 "2021-06-20T15:42:45Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Alex\_Tantos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex_tantos/32/10636_2.png) [@Alex\_Tantos](https://discourse.julialang.org/u/Alex_Tantos)\
**Post date:** [June 20, 2021, 3:44pm UTC](https://discourse.julialang.org/t/memoization-and-function-calls/63218/5 "2021-06-20T15:44:12Z")

</div>

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