Popularity
175 Stars
Updated Last
4 Months Ago
Started In
May 2013

Memoize.jl

Build Status Coverage Status

Easy memoization for Julia.

Usage

using Memoize
@memoize function x(a)
    println("Running")
    2a
end
julia> x(1)
Running
2

julia> memoize_cache(x)
IdDict{Any,Any} with 1 entry:
  (1,) => 2

julia> x(1)
2

julia> empty!(memoize_cache(x))
IdDict{Any,Any}()

julia> x(1)
Running
2

julia> x(1)
2

By default, Memoize.jl uses an IdDict as a cache, but it's also possible to specify the type of the cache. If you want to cache vectors based on the values they contain, you probably want this:

using Memoize
@memoize Dict function x(a)
    println("Running")
    a
end

You can also specify the full function call for constructing the dictionary. For example, to use LRUCache.jl:

using Memoize
using LRUCache
@memoize LRU{Tuple{Any,Any},Any}(maxsize=2) function x(a, b)
    println("Running")
    a + b
end
julia> x(1,2)
Running
3

julia> x(1,2)
3

julia> x(2,2)
Running
4

julia> x(2,3)
Running
5

julia> x(1,2)
Running
3

julia> x(2,3)
5

Notes

Note that the @memoize macro treats the type argument differently depending on its syntactical form: in the expression

@memoize CacheType function x(a, b)
    # ...
end

the expression CacheType must be either a non-function-call that evaluates to a type, or a function call that evaluates to an instance of the desired cache type. Either way, the methods Base.get! and Base.empty! must be defined for the supplied cache type.

Required Packages