module Memoize
Overview
Module to create memoized functions using macros.
How it works
Memoize.memoize get_number, NamedTuple(n: Int32), String do
puts "Computed"
n.to_s
end
The above usage will generate
@CACHE_get_number = {} of {Int32} => String
def _get_number(n : Int32) : String
puts "Computed"
n.to_s
end
def get_number(n : Int32) : String
if @CACHE_get_number.has_key?({n})
@CACHE_get_number[{n}]
else
@CACHE_get_number[{n}] = _get_number(n)
end
end
Defined in:
memoize.crConstant Summary
-
VERSION =
"1.0.0"
Macro Summary
-
memoize(method_name, param_tuple, return_type, &block)
Macro which creates a memoized function without any space bounds (infinitely large cache).
Macro Detail
macro memoize(method_name, param_tuple, return_type, &block)
#
Macro which creates a memoized function without any space bounds (infinitely large cache).
Memoize.memoize add_two, NamedTuple(n: Int32), Int32 do
puts "Computed"
n + 2
end
add_two(5) # Prints "Computed" and returns 7
add_two(5) # returns 7
add_two(5) # returns 7