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.2.0"
Macro Summary
-
memoize(method_name, param_tuple, return_type, &block)
Like
memoize_methodbut can be used in any scope (class, module, global). -
memoize_method(method_name, param_tuple, return_type, &block)
Macro which creates a memoized function without any space bounds (infinitely large cache).
-
memoize_only(method_name, rest_tuple, only_tuple, return_type, &block)
Like
memoize_only_methodbut can be used in any scope (module, class, global). -
memoize_only_method(method_name, rest_tuple, only_tuple, return_type, &block)
Only memoizes certain parameters of a method.
Macro Detail
Like memoize_method but can be used in any scope (class, module, global).
If using in class, the memoization will happen at class level meaning, memoized objects can
be accessed between different instances of the class.
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
Like memoize_only_method but can be used in any scope (module, class, global).
If using in class, the memoization will happen at class level meaning, memoized objects can
be accessed between different instances of the class.
Only memoizes certain parameters of a method.
Useful when certain parameters do not have equals and hash implemented,
but memoization is still necessary on the other parameters.
rest_tuple should be a NamedTuple containing parameters that should not be memoized.
only_tuple should be a NamedTuple containing parameters that need to be memoized.