@memoize macro

A.U.M
Fri, 22 May 2020 13:43:24 +0000
General Usage

Hello. I am working on implement macro @memoize. it works well but i want to generalize it and make it more robust (still for functions with a single argument) by checking whether the argument and return types are specified or not If they are not, I want to use the Any type instead of the argument in the Dict that acts as the cache. any help or tips?

macro memoize(fun::Expr)
local call = fun.args[1]
local name = call.args[1].args[1]
local arg1 = call.args[1].args[2]
local arg1_name = arg1.args[1]
local arg1_type = arg1.args[2]
local return_type = call.args[2]
local body = fun.args[2]
    quote
let cache = Dict{$(esc(arg1_type)), $(esc(return_type))}()
global function $(esc(name))($(esc(arg1_name))::$(esc(arg1_type)))::$(esc(return_type))
if haskey(cache, $(esc(arg1_name)))
cache[$(esc(arg1_name))]
else
cache[$(esc(arg1_name))] = $(esc(body))
end
end
end
end
end

4 posts - 3 participants