# How to define a function that remembers values?

**URL:** https://discourse.julialang.org/t/how-to-define-a-function-that-remembers-values/6667
**Category:** General Usage
**Tags:** question
**Created:** [October 24, 2017, 8:51pm UTC](https://discourse.julialang.org/t/how-to-define-a-function-that-remembers-values/6667 "2017-10-24T20:51:38Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [October 24, 2017, 8:51pm UTC](https://discourse.julialang.org/t/how-to-define-a-function-that-remembers-values/6667/1 "2017-10-24T20:51:38Z")

</div>

I have a function that is very expensive to compute, `fun(x)`. But I might be calling it on the same argument multiple times. Therefore it would be helpful if the function was able to store somehwere the result it obtained for a given value of `x`, instead of having to compute it again when it is called with the same value of `x` twice or more.

What is the best way to achieve this?

---

<div class="post-metadata">

### Author: ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)
#### Post date: [October 24, 2017, 8:55pm UTC](https://discourse.julialang.org/t/how-to-define-a-function-that-remembers-values/6667/2 "2017-10-24T20:55:29Z")

</div>

Yep.  
[https://github.com/simonster/Memoize.jl](https://github.com/simonster/Memoize.jl)

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [October 24, 2017, 9:01pm UTC](https://discourse.julialang.org/t/how-to-define-a-function-that-remembers-values/6667/3 "2017-10-24T21:01:11Z")

</div>

Memoize, that’s the word I was looking for. Thanks.

---

<div class="post-metadata">

### Author: ![harven](https://avatars.discourse-cdn.com/v4/letter/h/3da27b/32.png) [@harven](https://discourse.julialang.org/u/harven)
#### Post date: [October 26, 2017, 7:01am UTC](https://discourse.julialang.org/t/how-to-define-a-function-that-remembers-values/6667/4 "2017-10-26T07:01:36Z")

</div>

```
  memoize(f) = 
    let d = Dict()
       (x...) -> get!(d, x, f(x...))
    end

```

https://www.youtube.com/embed/7KGZ_9D_DbI?feature=oembed&wmode=opaque&list=PLP8iPy9hna6QpP6vqZs408etJVECPKIev

---

<div class="post-metadata">

### Author: ![anon61610682](https://avatars.discourse-cdn.com/v4/letter/a/ad7895/32.png) [@anon61610682](https://discourse.julialang.org/u/anon61610682)
#### Post date: [October 26, 2017, 7:44am UTC](https://discourse.julialang.org/t/how-to-define-a-function-that-remembers-values/6667/5 "2017-10-26T07:44:13Z")

</div>

Why not a simple functor in the form of a struct?

```julia
type ExpensiveFunctor{T<:AbstractFloat}
  x::Matrix{T}
  function (::Type{ExpensiveFunctor})(m1::Matrix, m2::Matrix)
    # expensive operations to initialize x
    new{promote_type(eltype(m1),eltype(m2))}(m1+m2)
  end

  function (m::ExpensiveFunctor){T<:Real}(x::AbstractVector{T})
    return m.x*x
  end
end

my_f = ExpensiveFunctor(eye(5,5), ones(5,5))
my_f(rand(1:10,5))

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 26, 2017, 8:39am UTC](https://discourse.julialang.org/t/how-to-define-a-function-that-remembers-values/6667/6 "2017-10-26T08:39:12Z")

</div>

_Memoization_ and _precomputation_ have different purposes. You memoize a function if the arguments are not necessarily known in advance (or you don’t want to bother thinking about them), while precomputation helps you when a calculation can be broken down to stages, and one of these stages is reusable (and expensive). In some contexts, both are helpful.
